/* Copyright (c) 2010, Avian Contributors

   Permission to use, copy, modify, and/or distribute this software
   for any purpose with or without fee is hereby granted, provided
   that the above copyright notice and this permission notice appear
   in all copies.

   There is NO WARRANTY for this software.  See license.txt for
   details. */

package java.nio.channels;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;

public class Channels {
  public static InputStream newInputStream(ReadableByteChannel channel) {
    return new MyInputStream(channel);
  }

  public static OutputStream newOutputStream(WritableByteChannel channel) {
    return new MyOutputStream(channel);
  }

  private static class MyInputStream extends InputStream {
    private final ReadableByteChannel channel;

    public MyInputStream(ReadableByteChannel channel) {
      this.channel = channel;
    }

    public int read() throws IOException {
      byte[] buffer = new byte[1];
      int r = read(buffer);
      if (r == -1) {
        return -1;
      } else {
        return buffer[0] & 0xFF;
      }
    }

    public int read(byte[] buffer, int offset, int length) throws IOException {
      return channel.read(ByteBuffer.wrap(buffer, offset, length));
    }

    public void close() throws IOException {
      channel.close();
    }
  }

  private static class MyOutputStream extends OutputStream {
    private final WritableByteChannel channel;

    public MyOutputStream(WritableByteChannel channel) {
      this.channel = channel;
    }

    public void write(int v) throws IOException {
      byte[] buffer = new byte[] { (byte) (v & 0xFF) };
      write(buffer);
    }

    public void write(byte[] buffer, int offset, int length)
      throws IOException
    {
      channel.write(ByteBuffer.wrap(buffer, offset, length));
    }

    public void close() throws IOException {
      channel.close();
    }
  }
}

@@ -740,24 +740,260 @@
 nnel);%0A  %7D%0A%0A
+  public static ReadableByteChannel newChannel(InputStream stream) %7B%0A    return new InputStreamChannel(stream);%0A  %7D%0A%0A  public static WritableByteChannel newChannel(OutputStream stream) %7B%0A    return new OutputStreamChannel(stream);%0A  %7D%0A%0A
   private st
@@ -2198,11 +2198,1284 @@
    %7D%0A  %7D
+%0A%0A  private static class InputStreamChannel implements ReadableByteChannel %7B%0A    private InputStream stream;%0A%0A    public InputStreamChannel(InputStream stream) %7B%0A      this.stream = stream;%0A    %7D%0A%0A    public void close() throws IOException %7B%0A      if (stream != null) %7B%0A        stream.close();%0A        stream = null;%0A      %7D%0A    %7D%0A%0A    public boolean isOpen() %7B%0A      return stream != null;%0A    %7D%0A%0A    public int read(ByteBuffer b) throws IOException %7B%0A      int c = stream.read%0A        (b.array(), b.arrayOffset() + b.position(), b.remaining());%0A      %0A      if (c %3E 0) %7B%0A        b.position(b.position() + c);%0A      %7D%0A%0A      return c;%0A    %7D%0A  %7D%0A%0A  private static class OutputStreamChannel implements WritableByteChannel %7B%0A    private OutputStream stream;%0A%0A    public OutputStreamChannel(OutputStream stream) %7B%0A      this.stream = stream;%0A    %7D%0A%0A    public void close() throws IOException %7B%0A      if (stream != null) %7B%0A        stream.close();%0A        stream = null;%0A      %7D%0A    %7D%0A%0A    public boolean isOpen() %7B%0A      return stream != null;%0A    %7D%0A%0A    public int write(ByteBuffer b) throws IOException %7B%0A      stream.write(b.array(), b.arrayOffset() + b.position(), b.remaining());%0A%0A      int c = b.remaining();%0A%0A      b.position(b.limit());%0A      %0A      return c;%0A    %7D%0A  %7D
 %0A%7D%0A

add Channels.newChannel methods