|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. Oracle designates this |
| 8 | + * particular file as subject to the "Classpath" exception as provided |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. |
| 10 | + * |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | + * accompanied this code). |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License version |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | + * |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 22 | + * or visit www.oracle.com if you need additional information or have any |
| 23 | + * questions. |
| 24 | + */ |
| 25 | + |
| 26 | +package sun.nio.ch; |
| 27 | + |
| 28 | +import java.io.*; |
| 29 | +import java.nio.*; |
| 30 | +import java.nio.channels.*; |
| 31 | +import java.nio.channels.spi.*; |
| 32 | +import java.util.Objects; |
| 33 | + |
| 34 | +/** |
| 35 | + * This class is defined here rather than in java.nio.channels.Channels |
| 36 | + * so that it will be accessible from java.nio.channels.Channels and |
| 37 | + * sun.nio.ch.ChannelInputStream. |
| 38 | + * |
| 39 | + * |
| 40 | + * @author Mark Reinhold |
| 41 | + * @author Mike McCloskey |
| 42 | + * @author JSR-51 Expert Group |
| 43 | + * @since 18 |
| 44 | + */ |
| 45 | +public class ChannelOutputStream extends OutputStream { |
| 46 | + |
| 47 | + private final WritableByteChannel ch; |
| 48 | + private ByteBuffer bb; |
| 49 | + private byte[] bs; // Invoker's previous array |
| 50 | + private byte[] b1; |
| 51 | + |
| 52 | + /** |
| 53 | + * Write all remaining bytes in buffer to the given channel. |
| 54 | + * If the channel is selectable then it must be configured blocking. |
| 55 | + */ |
| 56 | + private static void writeFullyImpl(WritableByteChannel ch, ByteBuffer bb) |
| 57 | + throws IOException |
| 58 | + { |
| 59 | + while (bb.remaining() > 0) { |
| 60 | + int n = ch.write(bb); |
| 61 | + if (n <= 0) |
| 62 | + throw new RuntimeException("no bytes written"); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Write all remaining bytes in buffer to the given channel. |
| 68 | + * |
| 69 | + * @throws IllegalBlockingModeException |
| 70 | + * If the channel is selectable and configured non-blocking. |
| 71 | + */ |
| 72 | + private static void writeFully(WritableByteChannel ch, ByteBuffer bb) |
| 73 | + throws IOException |
| 74 | + { |
| 75 | + if (ch instanceof SelectableChannel sc) { |
| 76 | + synchronized (sc.blockingLock()) { |
| 77 | + if (!sc.isBlocking()) |
| 78 | + throw new IllegalBlockingModeException(); |
| 79 | + writeFullyImpl(ch, bb); |
| 80 | + } |
| 81 | + } else { |
| 82 | + writeFullyImpl(ch, bb); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * @param ch The channel wrapped by this stream. |
| 88 | + */ |
| 89 | + public ChannelOutputStream(WritableByteChannel ch) { |
| 90 | + this.ch = ch; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @return The channel wrapped by this stream. |
| 95 | + */ |
| 96 | + WritableByteChannel channel() { |
| 97 | + return ch; |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public synchronized void write(int b) throws IOException { |
| 102 | + if (b1 == null) |
| 103 | + b1 = new byte[1]; |
| 104 | + b1[0] = (byte) b; |
| 105 | + this.write(b1); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public synchronized void write(byte[] bs, int off, int len) |
| 110 | + throws IOException { |
| 111 | + Objects.checkFromIndexSize(off, len, bs.length); |
| 112 | + if (len == 0) { |
| 113 | + return; |
| 114 | + } |
| 115 | + ByteBuffer bb = ((this.bs == bs) |
| 116 | + ? this.bb |
| 117 | + : ByteBuffer.wrap(bs)); |
| 118 | + bb.limit(Math.min(off + len, bb.capacity())); |
| 119 | + bb.position(off); |
| 120 | + this.bb = bb; |
| 121 | + this.bs = bs; |
| 122 | + writeFully(ch, bb); |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public void close() throws IOException { |
| 127 | + ch.close(); |
| 128 | + } |
| 129 | + |
| 130 | +} |
0 commit comments