src/share/classes/java/nio/channels/Channels.java

Print this page




 165                         return;
 166                     }
 167                     ByteBuffer bb = ((this.bs == bs)
 168                                      ? this.bb
 169                                      : ByteBuffer.wrap(bs));
 170                     bb.limit(Math.min(off + len, bb.capacity()));
 171                     bb.position(off);
 172                     this.bb = bb;
 173                     this.bs = bs;
 174                     Channels.writeFully(ch, bb);
 175                 }
 176 
 177                 public void close() throws IOException {
 178                     ch.close();
 179                 }
 180 
 181             };
 182     }
 183 
 184     /**
 185      * {@note new}
 186      * Constructs a stream that reads bytes from the given channel.
 187      *
 188      * <p> The stream will not be buffered, and it will not support the {@link
 189      * InputStream#mark mark} or {@link InputStream#reset reset} methods.  The
 190      * stream will be safe for access by multiple concurrent threads.  Closing
 191      * the stream will in turn cause the channel to be closed.  </p>
 192      *
 193      * @param  ch
 194      *         The channel from which bytes will be read
 195      *
 196      * @return  A new input stream
 197      *
 198      * @since 1.7
 199      */
 200     public static InputStream newInputStream(final AsynchronousByteChannel ch) {
 201         checkNotNull(ch, "ch");
 202         return new InputStream() {
 203 
 204             private ByteBuffer bb = null;
 205             private byte[] bs = null;           // Invoker's previous array


 241                         } catch (ExecutionException ee) {
 242                             throw new IOException(ee.getCause());
 243                         } catch (InterruptedException ie) {
 244                             interrupted = true;
 245                         }
 246                     }
 247                 } finally {
 248                     if (interrupted)
 249                         Thread.currentThread().interrupt();
 250                 }
 251             }
 252 
 253             @Override
 254             public void close() throws IOException {
 255                 ch.close();
 256             }
 257         };
 258     }
 259 
 260     /**
 261      * {@note new}
 262      * Constructs a stream that writes bytes to the given channel.
 263      *
 264      * <p> The stream will not be buffered. The stream will be safe for access
 265      * by multiple concurrent threads.  Closing the stream will in turn cause
 266      * the channel to be closed.  </p>
 267      *
 268      * @param  ch
 269      *         The channel to which bytes will be written
 270      *
 271      * @return  A new output stream
 272      *
 273      * @since 1.7
 274      */
 275     public static OutputStream newOutputStream(final AsynchronousByteChannel ch) {
 276         checkNotNull(ch, "ch");
 277         return new OutputStream() {
 278 
 279             private ByteBuffer bb = null;
 280             private byte[] bs = null;   // Invoker's previous array
 281             private byte[] b1 = null;




 165                         return;
 166                     }
 167                     ByteBuffer bb = ((this.bs == bs)
 168                                      ? this.bb
 169                                      : ByteBuffer.wrap(bs));
 170                     bb.limit(Math.min(off + len, bb.capacity()));
 171                     bb.position(off);
 172                     this.bb = bb;
 173                     this.bs = bs;
 174                     Channels.writeFully(ch, bb);
 175                 }
 176 
 177                 public void close() throws IOException {
 178                     ch.close();
 179                 }
 180 
 181             };
 182     }
 183 
 184     /**

 185      * Constructs a stream that reads bytes from the given channel.
 186      *
 187      * <p> The stream will not be buffered, and it will not support the {@link
 188      * InputStream#mark mark} or {@link InputStream#reset reset} methods.  The
 189      * stream will be safe for access by multiple concurrent threads.  Closing
 190      * the stream will in turn cause the channel to be closed.  </p>
 191      *
 192      * @param  ch
 193      *         The channel from which bytes will be read
 194      *
 195      * @return  A new input stream
 196      *
 197      * @since 1.7
 198      */
 199     public static InputStream newInputStream(final AsynchronousByteChannel ch) {
 200         checkNotNull(ch, "ch");
 201         return new InputStream() {
 202 
 203             private ByteBuffer bb = null;
 204             private byte[] bs = null;           // Invoker's previous array


 240                         } catch (ExecutionException ee) {
 241                             throw new IOException(ee.getCause());
 242                         } catch (InterruptedException ie) {
 243                             interrupted = true;
 244                         }
 245                     }
 246                 } finally {
 247                     if (interrupted)
 248                         Thread.currentThread().interrupt();
 249                 }
 250             }
 251 
 252             @Override
 253             public void close() throws IOException {
 254                 ch.close();
 255             }
 256         };
 257     }
 258 
 259     /**

 260      * Constructs a stream that writes bytes to the given channel.
 261      *
 262      * <p> The stream will not be buffered. The stream will be safe for access
 263      * by multiple concurrent threads.  Closing the stream will in turn cause
 264      * the channel to be closed.  </p>
 265      *
 266      * @param  ch
 267      *         The channel to which bytes will be written
 268      *
 269      * @return  A new output stream
 270      *
 271      * @since 1.7
 272      */
 273     public static OutputStream newOutputStream(final AsynchronousByteChannel ch) {
 274         checkNotNull(ch, "ch");
 275         return new OutputStream() {
 276 
 277             private ByteBuffer bb = null;
 278             private byte[] bs = null;   // Invoker's previous array
 279             private byte[] b1 = null;