Frames | No Frames |
1: /* Channels.java -- 2: Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc. 3: 4: This file is part of GNU Classpath. 5: 6: GNU Classpath is free software; you can redistribute it and/or modify 7: it under the terms of the GNU General Public License as published by 8: the Free Software Foundation; either version 2, or (at your option) 9: any later version. 10: 11: GNU Classpath is distributed in the hope that it will be useful, but 12: WITHOUT ANY WARRANTY; without even the implied warranty of 13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14: General Public License for more details. 15: 16: You should have received a copy of the GNU General Public License 17: along with GNU Classpath; see the file COPYING. If not, write to the 18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19: 02110-1301 USA. 20: 21: Linking this library statically or dynamically with other modules is 22: making a combined work based on this library. Thus, the terms and 23: conditions of the GNU General Public License cover the whole 24: combination. 25: 26: As a special exception, the copyright holders of this library give you 27: permission to link this library with independent modules to produce an 28: executable, regardless of the license terms of these independent 29: modules, and to copy and distribute the resulting executable under 30: terms of your choice, provided that you also meet, for each linked 31: independent module, the terms and conditions of the license of that 32: module. An independent module is a module which is not derived from 33: or based on this library. If you modify this library, you may extend 34: this exception to your version of the library, but you are not 35: obligated to do so. If you do not wish to do so, delete this 36: exception statement from your version. */ 37: 38: 39: package java.nio.channels; 40: 41: import gnu.java.nio.ChannelInputStream; 42: import gnu.java.nio.ChannelOutputStream; 43: import gnu.java.nio.ChannelReader; 44: import gnu.java.nio.InputStreamChannel; 45: import gnu.java.nio.OutputStreamChannel; 46: import gnu.java.nio.channels.FileChannelImpl; 47: 48: import java.io.FileInputStream; 49: import java.io.FileOutputStream; 50: import java.io.InputStream; 51: import java.io.OutputStream; 52: import java.io.Reader; 53: import java.io.Writer; 54: import java.nio.charset.Charset; 55: import java.nio.charset.CharsetDecoder; 56: import java.nio.charset.CharsetEncoder; 57: 58: 59: /** 60: * @since 1.4 61: */ 62: public final class Channels 63: { 64: /** 65: * This class isn't intended to be instantiated. 66: */ 67: private Channels() 68: { 69: // Do nothing here. 70: } 71: 72: /** 73: * Constructs a stream that reads bytes from the given channel. 74: */ 75: public static InputStream newInputStream(ReadableByteChannel ch) 76: { 77: if (ch instanceof FileChannelImpl) 78: return newInputStream((FileChannelImpl) ch); 79: return new ChannelInputStream(ch); 80: } 81: 82: /** 83: * Constructs a stream that writes bytes to the given channel. 84: */ 85: public static OutputStream newOutputStream(WritableByteChannel ch) 86: { 87: if (ch instanceof FileChannelImpl) 88: return newOutputStream((FileChannelImpl) ch); 89: return new ChannelOutputStream(ch); 90: } 91: 92: static native FileInputStream newInputStream(FileChannelImpl ch); 93: 94: static native FileOutputStream newOutputStream(FileChannelImpl ch); 95: 96: /** 97: * Constructs a channel that reads bytes from the given stream. 98: */ 99: public static ReadableByteChannel newChannel(InputStream in) 100: { 101: return new InputStreamChannel(in); 102: } 103: 104: /** 105: * Constructs a channel that writes bytes to the given stream. 106: */ 107: public static WritableByteChannel newChannel(OutputStream out) 108: { 109: return new OutputStreamChannel(out); 110: } 111: 112: /** 113: * Constructs a reader that decodes bytes from the given channel using the 114: * given decoder. 115: */ 116: public static Reader newReader(ReadableByteChannel ch, CharsetDecoder dec, 117: int minBufferCap) 118: { 119: return new ChannelReader(ch, dec, minBufferCap); 120: } 121: 122: /** 123: * Constructs a reader that decodes bytes from the given channel according to 124: * the named charset. 125: * 126: * @exception UnsupportedCharsetException If no support for the named charset 127: * is available in this instance of the Java virtual machine. 128: */ 129: public static Reader newReader(ReadableByteChannel ch, String csName) 130: { 131: return newReader(ch, Charset.forName(csName).newDecoder(), -1); 132: } 133: 134: /** 135: * Constructs a writer that encodes characters using the given encoder and 136: * writes the resulting bytes to the given channel. 137: */ 138: public static Writer newWriter(WritableByteChannel ch, CharsetEncoder enc, 139: int minBufferCap) 140: { 141: // FIXME: implement java.nio.channels.Channel.newWriter(WritableByteChannel, CharsetEncoder, int) 142: throw new Error("not implemented"); 143: } 144: 145: /** 146: * Constructs a writer that encodes characters according to the named charset 147: * and writes the resulting bytes to the given channel. 148: * 149: * @exception UnsupportedCharsetException If no support for the named charset 150: * is available in this instance of the Java virtual machine. 151: */ 152: public static Writer newWriter(WritableByteChannel ch, String csName) 153: { 154: return newWriter(ch, Charset.forName(csName).newEncoder(), -1); 155: } 156: }