1
1
/*
2
- * Copyright (c) 2001, 2021 , Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2001, 2022 , Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
40
40
import jdk .internal .util .ArraysSupport ;
41
41
42
42
/**
43
- * This class is defined here rather than in java.nio.channels.Channels
44
- * so that code can be shared with SocketAdaptor.
43
+ * An InputStream that reads bytes from a channel.
45
44
*
46
45
* @author Mike McCloskey
47
46
* @author Mark Reinhold
48
- * @since 1.4
49
47
*/
50
-
51
- public class ChannelInputStream
52
- extends InputStream
53
- {
48
+ class ChannelInputStream extends InputStream {
54
49
private static final int DEFAULT_BUFFER_SIZE = 8192 ;
55
50
56
- public static int read (ReadableByteChannel ch , ByteBuffer bb ,
57
- boolean block )
58
- throws IOException
59
- {
51
+ private final ReadableByteChannel ch ;
52
+ private ByteBuffer bb ;
53
+ private byte [] bs ; // Invoker's previous array
54
+ private byte [] b1 ;
55
+
56
+ /**
57
+ * Initialize a ChannelInputStream that reads from the given channel.
58
+ */
59
+ ChannelInputStream (ReadableByteChannel ch ) {
60
+ this .ch = ch ;
61
+ }
62
+
63
+ /**
64
+ * Reads a sequence of bytes from the channel into the given buffer.
65
+ */
66
+ private int read (ByteBuffer bb ) throws IOException {
60
67
if (ch instanceof SelectableChannel sc ) {
61
68
synchronized (sc .blockingLock ()) {
62
- boolean bm = sc .isBlocking ();
63
- if (!bm )
69
+ if (!sc .isBlocking ())
64
70
throw new IllegalBlockingModeException ();
65
- if (bm != block )
66
- sc .configureBlocking (block );
67
- int n = ch .read (bb );
68
- if (bm != block )
69
- sc .configureBlocking (bm );
70
- return n ;
71
+ return ch .read (bb );
71
72
}
72
73
} else {
73
74
return ch .read (bb );
74
75
}
75
76
}
76
77
77
- protected final ReadableByteChannel ch ;
78
- private ByteBuffer bb = null ;
79
- private byte [] bs = null ; // Invoker's previous array
80
- private byte [] b1 = null ;
81
-
82
- public ChannelInputStream (ReadableByteChannel ch ) {
83
- this .ch = ch ;
84
- }
85
-
78
+ @ Override
86
79
public synchronized int read () throws IOException {
87
80
if (b1 == null )
88
81
b1 = new byte [1 ];
89
- int n = this . read (b1 );
82
+ int n = read (b1 );
90
83
if (n == 1 )
91
84
return b1 [0 ] & 0xff ;
92
85
return -1 ;
93
86
}
94
87
88
+ @ Override
95
89
public synchronized int read (byte [] bs , int off , int len )
96
90
throws IOException
97
91
{
@@ -109,12 +103,6 @@ public synchronized int read(byte[] bs, int off, int len)
109
103
return read (bb );
110
104
}
111
105
112
- protected int read (ByteBuffer bb )
113
- throws IOException
114
- {
115
- return ChannelInputStream .read (ch , bb , true );
116
- }
117
-
118
106
@ Override
119
107
public byte [] readAllBytes () throws IOException {
120
108
if (!(ch instanceof SeekableByteChannel sbc ))
@@ -201,6 +189,7 @@ public byte[] readNBytes(int len) throws IOException {
201
189
return (capacity == nread ) ? buf : Arrays .copyOf (buf , nread );
202
190
}
203
191
192
+ @ Override
204
193
public int available () throws IOException {
205
194
// special case where the channel is to a file
206
195
if (ch instanceof SeekableByteChannel sbc ) {
@@ -210,6 +199,7 @@ public int available() throws IOException {
210
199
return 0 ;
211
200
}
212
201
202
+ @ Override
213
203
public synchronized long skip (long n ) throws IOException {
214
204
// special case where the channel is to a file
215
205
if (ch instanceof SeekableByteChannel sbc ) {
@@ -230,46 +220,62 @@ public synchronized long skip(long n) throws IOException {
230
220
return super .skip (n );
231
221
}
232
222
233
- public void close () throws IOException {
234
- ch .close ();
235
- }
236
-
237
223
@ Override
238
224
public long transferTo (OutputStream out ) throws IOException {
239
225
Objects .requireNonNull (out , "out" );
240
226
241
- if (out instanceof ChannelOutputStream cos
242
- && ch instanceof FileChannel fc ) {
243
- WritableByteChannel wbc = cos .channel ();
244
-
245
- if (wbc instanceof FileChannel dst ) {
246
- return transfer (fc , dst );
247
- }
248
-
249
- if (wbc instanceof SelectableChannel sc ) {
227
+ if (ch instanceof FileChannel fc ) {
228
+ // FileChannel -> SocketChannel
229
+ if (out instanceof SocketOutputStream sos ) {
230
+ SocketChannelImpl sc = sos .channel ();
250
231
synchronized (sc .blockingLock ()) {
251
232
if (!sc .isBlocking ())
252
233
throw new IllegalBlockingModeException ();
253
- return transfer (fc , wbc );
234
+ return transfer (fc , sc );
254
235
}
255
236
}
256
237
257
- return transfer (fc , wbc );
238
+ // FileChannel -> WritableByteChannel
239
+ if (out instanceof ChannelOutputStream cos ) {
240
+ WritableByteChannel wbc = cos .channel ();
241
+
242
+ if (wbc instanceof SelectableChannel sc ) {
243
+ synchronized (sc .blockingLock ()) {
244
+ if (!sc .isBlocking ())
245
+ throw new IllegalBlockingModeException ();
246
+ return transfer (fc , wbc );
247
+ }
248
+ }
249
+
250
+ return transfer (fc , wbc );
251
+ }
258
252
}
259
253
260
254
return super .transferTo (out );
261
255
}
262
256
263
- private static long transfer (FileChannel src , WritableByteChannel dst ) throws IOException {
264
- long initialPos = src .position ();
257
+ /**
258
+ * Transfers all bytes from a channel's file to a target writeable byte channel.
259
+ * If the writeable byte channel is a selectable channel then it must be in
260
+ * blocking mode.
261
+ */
262
+ private static long transfer (FileChannel fc , WritableByteChannel target )
263
+ throws IOException
264
+ {
265
+ long initialPos = fc .position ();
265
266
long pos = initialPos ;
266
267
try {
267
- while (pos < src .size ()) {
268
- pos += src .transferTo (pos , Long .MAX_VALUE , dst );
268
+ while (pos < fc .size ()) {
269
+ pos += fc .transferTo (pos , Long .MAX_VALUE , target );
269
270
}
270
271
} finally {
271
- src .position (pos );
272
+ fc .position (pos );
272
273
}
273
274
return pos - initialPos ;
274
275
}
276
+
277
+ @ Override
278
+ public void close () throws IOException {
279
+ ch .close ();
280
+ }
275
281
}
1 commit comments
openjdk-notifier[bot] commentedon Jan 6, 2022
Review
Issues