mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-28 07:14:30 +02:00
8273038: ChannelInputStream.transferTo() uses FileChannel.transferTo(FileChannel)
Reviewed-by: alanb
This commit is contained in:
parent
6750c34c92
commit
185557423d
4 changed files with 388 additions and 80 deletions
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2001, 2021, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
|
@ -142,4 +142,29 @@ public class ChannelInputStream
|
|||
ch.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long transferTo(OutputStream out) throws IOException {
|
||||
Objects.requireNonNull(out, "out");
|
||||
|
||||
if (out instanceof ChannelOutputStream cos
|
||||
&& ch instanceof FileChannel fc
|
||||
&& cos.channel() instanceof FileChannel dst) {
|
||||
return transfer(fc, dst);
|
||||
}
|
||||
|
||||
return super.transferTo(out);
|
||||
}
|
||||
|
||||
private static long transfer(FileChannel src, FileChannel dst) throws IOException {
|
||||
long initialPos = src.position();
|
||||
long pos = initialPos;
|
||||
try {
|
||||
while (pos < src.size()) {
|
||||
pos += src.transferTo(pos, Long.MAX_VALUE, dst);
|
||||
}
|
||||
} finally {
|
||||
src.position(pos);
|
||||
}
|
||||
return pos - initialPos;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue