Skip to content

Commit 1855574

Browse files
mkargAlan Bateman
authored andcommitted
8273038: ChannelInputStream.transferTo() uses FileChannel.transferTo(FileChannel)
Reviewed-by: alanb
1 parent 6750c34 commit 1855574

File tree

4 files changed

+388
-80
lines changed

4 files changed

+388
-80
lines changed

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

Lines changed: 4 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import java.util.Objects;
4242
import java.util.concurrent.ExecutionException;
4343
import sun.nio.ch.ChannelInputStream;
44+
import sun.nio.ch.ChannelOutputStream;
4445
import sun.nio.cs.StreamDecoder;
4546
import sun.nio.cs.StreamEncoder;
4647

@@ -63,40 +64,6 @@ public final class Channels {
6364

6465
private Channels() { throw new Error("no instances"); }
6566

66-
/**
67-
* Write all remaining bytes in buffer to the given channel.
68-
* If the channel is selectable then it must be configured blocking.
69-
*/
70-
private static void writeFullyImpl(WritableByteChannel ch, ByteBuffer bb)
71-
throws IOException
72-
{
73-
while (bb.remaining() > 0) {
74-
int n = ch.write(bb);
75-
if (n <= 0)
76-
throw new RuntimeException("no bytes written");
77-
}
78-
}
79-
80-
/**
81-
* Write all remaining bytes in buffer to the given channel.
82-
*
83-
* @throws IllegalBlockingModeException
84-
* If the channel is selectable and configured non-blocking.
85-
*/
86-
private static void writeFully(WritableByteChannel ch, ByteBuffer bb)
87-
throws IOException
88-
{
89-
if (ch instanceof SelectableChannel sc) {
90-
synchronized (sc.blockingLock()) {
91-
if (!sc.isBlocking())
92-
throw new IllegalBlockingModeException();
93-
writeFullyImpl(ch, bb);
94-
}
95-
} else {
96-
writeFullyImpl(ch, bb);
97-
}
98-
}
99-
10067
// -- Byte streams from channels --
10168

10269
/**
@@ -136,47 +103,7 @@ public static InputStream newInputStream(ReadableByteChannel ch) {
136103
*/
137104
public static OutputStream newOutputStream(WritableByteChannel ch) {
138105
Objects.requireNonNull(ch, "ch");
139-
140-
return new OutputStream() {
141-
142-
private ByteBuffer bb;
143-
private byte[] bs; // Invoker's previous array
144-
private byte[] b1;
145-
146-
@Override
147-
public synchronized void write(int b) throws IOException {
148-
if (b1 == null)
149-
b1 = new byte[1];
150-
b1[0] = (byte) b;
151-
this.write(b1);
152-
}
153-
154-
@Override
155-
public synchronized void write(byte[] bs, int off, int len)
156-
throws IOException
157-
{
158-
if ((off < 0) || (off > bs.length) || (len < 0) ||
159-
((off + len) > bs.length) || ((off + len) < 0)) {
160-
throw new IndexOutOfBoundsException();
161-
} else if (len == 0) {
162-
return;
163-
}
164-
ByteBuffer bb = ((this.bs == bs)
165-
? this.bb
166-
: ByteBuffer.wrap(bs));
167-
bb.limit(Math.min(off + len, bb.capacity()));
168-
bb.position(off);
169-
this.bb = bb;
170-
this.bs = bs;
171-
Channels.writeFully(ch, bb);
172-
}
173-
174-
@Override
175-
public void close() throws IOException {
176-
ch.close();
177-
}
178-
179-
};
106+
return new ChannelOutputStream(ch);
180107
}
181108

182109
/**
@@ -216,10 +143,8 @@ public synchronized int read() throws IOException {
216143
public synchronized int read(byte[] bs, int off, int len)
217144
throws IOException
218145
{
219-
if ((off < 0) || (off > bs.length) || (len < 0) ||
220-
((off + len) > bs.length) || ((off + len) < 0)) {
221-
throw new IndexOutOfBoundsException();
222-
} else if (len == 0) {
146+
Objects.checkFromIndexSize(off, len, bs.length);
147+
if (len == 0) {
223148
return 0;
224149
}
225150

src/java.base/share/classes/sun/nio/ch/ChannelInputStream.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2001, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -142,4 +142,29 @@ public void close() throws IOException {
142142
ch.close();
143143
}
144144

145+
@Override
146+
public long transferTo(OutputStream out) throws IOException {
147+
Objects.requireNonNull(out, "out");
148+
149+
if (out instanceof ChannelOutputStream cos
150+
&& ch instanceof FileChannel fc
151+
&& cos.channel() instanceof FileChannel dst) {
152+
return transfer(fc, dst);
153+
}
154+
155+
return super.transferTo(out);
156+
}
157+
158+
private static long transfer(FileChannel src, FileChannel dst) throws IOException {
159+
long initialPos = src.position();
160+
long pos = initialPos;
161+
try {
162+
while (pos < src.size()) {
163+
pos += src.transferTo(pos, Long.MAX_VALUE, dst);
164+
}
165+
} finally {
166+
src.position(pos);
167+
}
168+
return pos - initialPos;
169+
}
145170
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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

Comments
 (0)