From b3da635b63d66785b8a10b0ffb5ade9b8fb426d8 Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Tue, 13 Jul 2010 18:03:25 -0600 Subject: [PATCH] add java.nio.channels.Channels implementation --- classpath/java/nio/channels/Channels.java | 75 +++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 classpath/java/nio/channels/Channels.java diff --git a/classpath/java/nio/channels/Channels.java b/classpath/java/nio/channels/Channels.java new file mode 100644 index 0000000000..3f7fe63eaa --- /dev/null +++ b/classpath/java/nio/channels/Channels.java @@ -0,0 +1,75 @@ +/* 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(); + } + } +}