jdk/src/share/classes/java/nio/channels/Channels.java
author alanb
Fri, 02 Nov 2012 15:50:11 +0000
changeset 14342 8435a30053c1
parent 14014 da3648e13e67
permissions -rw-r--r--
7197491: update copyright year to match last edit in jdk8 jdk repository Reviewed-by: chegar, ksrini
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 14014
diff changeset
     2
 * Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3631
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3631
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3631
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3631
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3631
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.nio.channels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.FileInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.FileOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.Reader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.Writer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.nio.charset.Charset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.nio.charset.CharsetDecoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.nio.charset.CharsetEncoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.nio.charset.UnsupportedCharsetException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.nio.channels.spi.AbstractInterruptibleChannel;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
    41
import java.util.concurrent.ExecutionException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import sun.nio.ch.ChannelInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import sun.nio.cs.StreamDecoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import sun.nio.cs.StreamEncoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * Utility methods for channels and streams.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <p> This class defines static methods that support the interoperation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * stream classes of the <tt>{@link java.io}</tt> package with the channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * classes of this package.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @author Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @author Mike McCloskey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @author JSR-51 Expert Group
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
public final class Channels {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private Channels() { }              // No instantiation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
1637
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 715
diff changeset
    65
    private static void checkNotNull(Object o, String name) {
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 715
diff changeset
    66
        if (o == null)
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 715
diff changeset
    67
            throw new NullPointerException("\"" + name + "\" is null!");
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 715
diff changeset
    68
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
66
eac97080ce1e 6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents: 2
diff changeset
    70
    /**
eac97080ce1e 6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents: 2
diff changeset
    71
     * Write all remaining bytes in buffer to the given channel.
eac97080ce1e 6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents: 2
diff changeset
    72
     * If the channel is selectable then it must be configured blocking.
eac97080ce1e 6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents: 2
diff changeset
    73
     */
eac97080ce1e 6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents: 2
diff changeset
    74
    private static void writeFullyImpl(WritableByteChannel ch, ByteBuffer bb)
eac97080ce1e 6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents: 2
diff changeset
    75
        throws IOException
eac97080ce1e 6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents: 2
diff changeset
    76
    {
eac97080ce1e 6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents: 2
diff changeset
    77
        while (bb.remaining() > 0) {
eac97080ce1e 6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents: 2
diff changeset
    78
            int n = ch.write(bb);
eac97080ce1e 6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents: 2
diff changeset
    79
            if (n <= 0)
eac97080ce1e 6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents: 2
diff changeset
    80
                throw new RuntimeException("no bytes written");
eac97080ce1e 6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents: 2
diff changeset
    81
        }
eac97080ce1e 6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents: 2
diff changeset
    82
    }
eac97080ce1e 6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents: 2
diff changeset
    83
eac97080ce1e 6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents: 2
diff changeset
    84
    /**
eac97080ce1e 6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents: 2
diff changeset
    85
     * Write all remaining bytes in buffer to the given channel.
eac97080ce1e 6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents: 2
diff changeset
    86
     *
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 5506
diff changeset
    87
     * @throws  IllegalBlockingModeException
66
eac97080ce1e 6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents: 2
diff changeset
    88
     *          If the channel is selectable and configured non-blocking.
eac97080ce1e 6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents: 2
diff changeset
    89
     */
eac97080ce1e 6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents: 2
diff changeset
    90
    private static void writeFully(WritableByteChannel ch, ByteBuffer bb)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        if (ch instanceof SelectableChannel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            SelectableChannel sc = (SelectableChannel)ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            synchronized (sc.blockingLock()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                if (!sc.isBlocking())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                    throw new IllegalBlockingModeException();
66
eac97080ce1e 6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents: 2
diff changeset
    98
                writeFullyImpl(ch, bb);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        } else {
66
eac97080ce1e 6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents: 2
diff changeset
   101
            writeFullyImpl(ch, bb);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    // -- Byte streams from channels --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Constructs a stream that reads bytes from the given channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * <p> The <tt>read</tt> methods of the resulting stream will throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * {@link IllegalBlockingModeException} if invoked while the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * channel is in non-blocking mode.  The stream will not be buffered, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * it will not support the {@link InputStream#mark mark} or {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * InputStream#reset reset} methods.  The stream will be safe for access by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * multiple concurrent threads.  Closing the stream will in turn cause the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * channel to be closed.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @param  ch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *         The channel from which bytes will be read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @return  A new input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public static InputStream newInputStream(ReadableByteChannel ch) {
1637
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 715
diff changeset
   124
        checkNotNull(ch, "ch");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        return new sun.nio.ch.ChannelInputStream(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * Constructs a stream that writes bytes to the given channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * <p> The <tt>write</tt> methods of the resulting stream will throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * {@link IllegalBlockingModeException} if invoked while the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * channel is in non-blocking mode.  The stream will not be buffered.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * stream will be safe for access by multiple concurrent threads.  Closing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * the stream will in turn cause the channel to be closed.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @param  ch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *         The channel to which bytes will be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @return  A new output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public static OutputStream newOutputStream(final WritableByteChannel ch) {
1637
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 715
diff changeset
   143
        checkNotNull(ch, "ch");
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 715
diff changeset
   144
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        return new OutputStream() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                private ByteBuffer bb = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                private byte[] bs = null;       // Invoker's previous array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                private byte[] b1 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                public synchronized void write(int b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                   if (b1 == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                        b1 = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    b1[0] = (byte)b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    this.write(b1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                public synchronized void write(byte[] bs, int off, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                    if ((off < 0) || (off > bs.length) || (len < 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                        ((off + len) > bs.length) || ((off + len) < 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                        throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    } else if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                    ByteBuffer bb = ((this.bs == bs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                                     ? this.bb
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                                     : ByteBuffer.wrap(bs));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                    bb.limit(Math.min(off + len, bb.capacity()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    bb.position(off);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    this.bb = bb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    this.bs = bs;
66
eac97080ce1e 6448457: (ch) Channels.newOutputStream().write() does not write all data
alanb
parents: 2
diff changeset
   174
                    Channels.writeFully(ch, bb);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                    ch.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   184
    /**
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   185
     * Constructs a stream that reads bytes from the given channel.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   186
     *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   187
     * <p> The stream will not be buffered, and it will not support the {@link
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   188
     * InputStream#mark mark} or {@link InputStream#reset reset} methods.  The
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   189
     * stream will be safe for access by multiple concurrent threads.  Closing
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   190
     * the stream will in turn cause the channel to be closed.  </p>
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   191
     *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   192
     * @param  ch
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   193
     *         The channel from which bytes will be read
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   194
     *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   195
     * @return  A new input stream
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   196
     *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   197
     * @since 1.7
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   198
     */
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   199
    public static InputStream newInputStream(final AsynchronousByteChannel ch) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   200
        checkNotNull(ch, "ch");
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   201
        return new InputStream() {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   202
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   203
            private ByteBuffer bb = null;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   204
            private byte[] bs = null;           // Invoker's previous array
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   205
            private byte[] b1 = null;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   206
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   207
            @Override
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   208
            public synchronized int read() throws IOException {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   209
                if (b1 == null)
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   210
                    b1 = new byte[1];
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   211
                int n = this.read(b1);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   212
                if (n == 1)
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   213
                    return b1[0] & 0xff;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   214
                return -1;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   215
            }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   216
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   217
            @Override
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   218
            public synchronized int read(byte[] bs, int off, int len)
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   219
                throws IOException
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   220
            {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   221
                if ((off < 0) || (off > bs.length) || (len < 0) ||
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   222
                    ((off + len) > bs.length) || ((off + len) < 0)) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   223
                    throw new IndexOutOfBoundsException();
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   224
                } else if (len == 0)
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   225
                    return 0;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   226
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   227
                ByteBuffer bb = ((this.bs == bs)
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   228
                                 ? this.bb
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   229
                                 : ByteBuffer.wrap(bs));
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   230
                bb.position(off);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   231
                bb.limit(Math.min(off + len, bb.capacity()));
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   232
                this.bb = bb;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   233
                this.bs = bs;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   234
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   235
                boolean interrupted = false;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   236
                try {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   237
                    for (;;) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   238
                        try {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   239
                            return ch.read(bb).get();
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   240
                        } catch (ExecutionException ee) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   241
                            throw new IOException(ee.getCause());
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   242
                        } catch (InterruptedException ie) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   243
                            interrupted = true;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   244
                        }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   245
                    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   246
                } finally {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   247
                    if (interrupted)
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   248
                        Thread.currentThread().interrupt();
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   249
                }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   250
            }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   251
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   252
            @Override
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   253
            public void close() throws IOException {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   254
                ch.close();
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   255
            }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   256
        };
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   257
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   258
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   259
    /**
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   260
     * Constructs a stream that writes bytes to the given channel.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   261
     *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   262
     * <p> The stream will not be buffered. The stream will be safe for access
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   263
     * by multiple concurrent threads.  Closing the stream will in turn cause
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   264
     * the channel to be closed.  </p>
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   265
     *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   266
     * @param  ch
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   267
     *         The channel to which bytes will be written
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   268
     *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   269
     * @return  A new output stream
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   270
     *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   271
     * @since 1.7
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   272
     */
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   273
    public static OutputStream newOutputStream(final AsynchronousByteChannel ch) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   274
        checkNotNull(ch, "ch");
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   275
        return new OutputStream() {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   276
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   277
            private ByteBuffer bb = null;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   278
            private byte[] bs = null;   // Invoker's previous array
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   279
            private byte[] b1 = null;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   280
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   281
            @Override
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   282
            public synchronized void write(int b) throws IOException {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   283
               if (b1 == null)
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   284
                    b1 = new byte[1];
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   285
                b1[0] = (byte)b;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   286
                this.write(b1);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   287
            }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   288
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   289
            @Override
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   290
            public synchronized void write(byte[] bs, int off, int len)
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   291
                throws IOException
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   292
            {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   293
                if ((off < 0) || (off > bs.length) || (len < 0) ||
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   294
                    ((off + len) > bs.length) || ((off + len) < 0)) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   295
                    throw new IndexOutOfBoundsException();
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   296
                } else if (len == 0) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   297
                    return;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   298
                }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   299
                ByteBuffer bb = ((this.bs == bs)
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   300
                                 ? this.bb
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   301
                                 : ByteBuffer.wrap(bs));
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   302
                bb.limit(Math.min(off + len, bb.capacity()));
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   303
                bb.position(off);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   304
                this.bb = bb;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   305
                this.bs = bs;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   306
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   307
                boolean interrupted = false;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   308
                try {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   309
                    while (bb.remaining() > 0) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   310
                        try {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   311
                            ch.write(bb).get();
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   312
                        } catch (ExecutionException ee) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   313
                            throw new IOException(ee.getCause());
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   314
                        } catch (InterruptedException ie) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   315
                            interrupted = true;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   316
                        }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   317
                    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   318
                } finally {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   319
                    if (interrupted)
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   320
                        Thread.currentThread().interrupt();
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   321
                }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   322
            }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   323
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   324
            @Override
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   325
            public void close() throws IOException {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   326
                ch.close();
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   327
            }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   328
        };
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   329
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1637
diff changeset
   330
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    // -- Channels from streams --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * Constructs a channel that reads bytes from the given stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * <p> The resulting channel will not be buffered; it will simply redirect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * its I/O operations to the given stream.  Closing the channel will in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * turn cause the stream to be closed.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * @param  in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     *         The stream from which bytes are to be read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * @return  A new readable byte channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    public static ReadableByteChannel newChannel(final InputStream in) {
1637
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 715
diff changeset
   347
        checkNotNull(in, "in");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        if (in instanceof FileInputStream &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            FileInputStream.class.equals(in.getClass())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            return ((FileInputStream)in).getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        return new ReadableByteChannelImpl(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    private static class ReadableByteChannelImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        extends AbstractInterruptibleChannel    // Not really interruptible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        implements ReadableByteChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        InputStream in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        private static final int TRANSFER_SIZE = 8192;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        private byte buf[] = new byte[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        private boolean open = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        private Object readLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        ReadableByteChannelImpl(InputStream in) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            this.in = in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        public int read(ByteBuffer dst) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            int len = dst.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            int totalRead = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            int bytesRead = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            synchronized (readLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                while (totalRead < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                    int bytesToRead = Math.min((len - totalRead),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                                               TRANSFER_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                    if (buf.length < bytesToRead)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                        buf = new byte[bytesToRead];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                    if ((totalRead > 0) && !(in.available() > 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                        break; // block at most once
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                        begin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                        bytesRead = in.read(buf, 0, bytesToRead);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                    } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                        end(bytesRead > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                    if (bytesRead < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                        totalRead += bytesRead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                    dst.put(buf, 0, bytesRead);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                if ((bytesRead < 0) && (totalRead == 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                return totalRead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        protected void implCloseChannel() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            open = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * Constructs a channel that writes bytes to the given stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * <p> The resulting channel will not be buffered; it will simply redirect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * its I/O operations to the given stream.  Closing the channel will in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * turn cause the stream to be closed.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * @param  out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     *         The stream to which bytes are to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * @return  A new writable byte channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    public static WritableByteChannel newChannel(final OutputStream out) {
1637
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 715
diff changeset
   422
        checkNotNull(out, "out");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        if (out instanceof FileOutputStream &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            FileOutputStream.class.equals(out.getClass())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                return ((FileOutputStream)out).getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        return new WritableByteChannelImpl(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    private static class WritableByteChannelImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        extends AbstractInterruptibleChannel    // Not really interruptible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        implements WritableByteChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        OutputStream out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        private static final int TRANSFER_SIZE = 8192;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        private byte buf[] = new byte[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        private boolean open = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        private Object writeLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        WritableByteChannelImpl(OutputStream out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            this.out = out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        public int write(ByteBuffer src) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            int len = src.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            int totalWritten = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            synchronized (writeLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                while (totalWritten < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                    int bytesToWrite = Math.min((len - totalWritten),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                                                TRANSFER_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                    if (buf.length < bytesToWrite)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                        buf = new byte[bytesToWrite];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                    src.get(buf, 0, bytesToWrite);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                        begin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                        out.write(buf, 0, bytesToWrite);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                    } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                        end(bytesToWrite > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                    totalWritten += bytesToWrite;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                return totalWritten;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        protected void implCloseChannel() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            open = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    // -- Character streams from channels --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * Constructs a reader that decodes bytes from the given channel using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * given decoder.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * <p> The resulting stream will contain an internal input buffer of at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * least <tt>minBufferCap</tt> bytes.  The stream's <tt>read</tt> methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * will, as needed, fill the buffer by reading bytes from the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * channel; if the channel is in non-blocking mode when bytes are to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * read then an {@link IllegalBlockingModeException} will be thrown.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * resulting stream will not otherwise be buffered, and it will not support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * the {@link Reader#mark mark} or {@link Reader#reset reset} methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * Closing the stream will in turn cause the channel to be closed.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * @param  ch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     *         The channel from which bytes will be read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * @param  dec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     *         The charset decoder to be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * @param  minBufferCap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     *         The minimum capacity of the internal byte buffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     *         or <tt>-1</tt> if an implementation-dependent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     *         default capacity is to be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * @return  A new reader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    public static Reader newReader(ReadableByteChannel ch,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                                   CharsetDecoder dec,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                                   int minBufferCap)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    {
1637
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 715
diff changeset
   507
        checkNotNull(ch, "ch");
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 715
diff changeset
   508
        return StreamDecoder.forDecoder(ch, dec.reset(), minBufferCap);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * Constructs a reader that decodes bytes from the given channel according
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * to the named charset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * <p> An invocation of this method of the form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * Channels.newReader(ch, csname)</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * behaves in exactly the same way as the expression
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * Channels.newReader(ch,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     *                    Charset.forName(csName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     *                        .newDecoder(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     *                    -1);</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * @param  ch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     *         The channel from which bytes will be read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * @param  csName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     *         The name of the charset to be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * @return  A new reader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * @throws  UnsupportedCharsetException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     *          If no support for the named charset is available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     *          in this instance of the Java virtual machine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    public static Reader newReader(ReadableByteChannel ch,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                                   String csName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    {
1637
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 715
diff changeset
   543
        checkNotNull(csName, "csName");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        return newReader(ch, Charset.forName(csName).newDecoder(), -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * Constructs a writer that encodes characters using the given encoder and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * writes the resulting bytes to the given channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * <p> The resulting stream will contain an internal output buffer of at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * least <tt>minBufferCap</tt> bytes.  The stream's <tt>write</tt> methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * will, as needed, flush the buffer by writing bytes to the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * channel; if the channel is in non-blocking mode when bytes are to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * written then an {@link IllegalBlockingModeException} will be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * The resulting stream will not otherwise be buffered.  Closing the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * will in turn cause the channel to be closed.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * @param  ch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     *         The channel to which bytes will be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * @param  enc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     *         The charset encoder to be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * @param  minBufferCap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     *         The minimum capacity of the internal byte buffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     *         or <tt>-1</tt> if an implementation-dependent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     *         default capacity is to be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * @return  A new writer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    public static Writer newWriter(final WritableByteChannel ch,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                                   final CharsetEncoder enc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                                   final int minBufferCap)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    {
1637
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 715
diff changeset
   576
        checkNotNull(ch, "ch");
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 715
diff changeset
   577
        return StreamEncoder.forEncoder(ch, enc.reset(), minBufferCap);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * Constructs a writer that encodes characters according to the named
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * charset and writes the resulting bytes to the given channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * <p> An invocation of this method of the form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * Channels.newWriter(ch, csname)</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * behaves in exactly the same way as the expression
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * Channels.newWriter(ch,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     *                    Charset.forName(csName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     *                        .newEncoder(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     *                    -1);</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * @param  ch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     *         The channel to which bytes will be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * @param  csName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     *         The name of the charset to be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * @return  A new writer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * @throws  UnsupportedCharsetException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     *          If no support for the named charset is available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     *          in this instance of the Java virtual machine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    public static Writer newWriter(WritableByteChannel ch,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                                   String csName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    {
1637
bfa39b25f0fc 6725399: (ch) Channels.newInputStream should check for null
sherman
parents: 715
diff changeset
   612
        checkNotNull(csName, "csName");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        return newWriter(ch, Charset.forName(csName).newEncoder(), -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
}