mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-26 22:34:27 +02:00
8191706: Add Reader::transferTo(Writer)
Reviewed-by: alanb, bpb, briangoetz
This commit is contained in:
parent
a53a1b7844
commit
5eafe731b0
2 changed files with 395 additions and 1 deletions
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 1996, 2012, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 1996, 2017, 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
|
||||
|
@ -26,6 +26,8 @@
|
|||
package java.io;
|
||||
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* Abstract class for reading character streams. The only methods that a
|
||||
* subclass must implement are read(char[], int, int) and close(). Most
|
||||
|
@ -50,6 +52,8 @@ package java.io;
|
|||
|
||||
public abstract class Reader implements Readable, Closeable {
|
||||
|
||||
private static final int TRANSFER_BUFFER_SIZE = 8192;
|
||||
|
||||
/**
|
||||
* The object used to synchronize operations on this stream. For
|
||||
* efficiency, a character-stream object may use an object other than
|
||||
|
@ -262,4 +266,41 @@ public abstract class Reader implements Readable, Closeable {
|
|||
*/
|
||||
public abstract void close() throws IOException;
|
||||
|
||||
/**
|
||||
* Reads all characters from this reader and writes the characters to the
|
||||
* given writer in the order that they are read. On return, this reader
|
||||
* will be at end of the stream. This method does not close either reader
|
||||
* or writer.
|
||||
* <p>
|
||||
* This method may block indefinitely reading from the reader, or
|
||||
* writing to the writer. The behavior for the case where the reader
|
||||
* and/or writer is <i>asynchronously closed</i>, or the thread
|
||||
* interrupted during the transfer, is highly reader and writer
|
||||
* specific, and therefore not specified.
|
||||
* <p>
|
||||
* If an I/O error occurs reading from the reader or writing to the
|
||||
* writer, then it may do so after some characters have been read or
|
||||
* written. Consequently the reader may not be at end of the stream and
|
||||
* one, or both, streams may be in an inconsistent state. It is strongly
|
||||
* recommended that both streams be promptly closed if an I/O error occurs.
|
||||
*
|
||||
* @param out the writer, non-null
|
||||
* @return the number of characters transferred
|
||||
* @throws IOException if an I/O error occurs when reading or writing
|
||||
* @throws NullPointerException if {@code out} is {@code null}
|
||||
*
|
||||
* @since 10
|
||||
*/
|
||||
public long transferTo(Writer out) throws IOException {
|
||||
Objects.requireNonNull(out, "out");
|
||||
long transferred = 0;
|
||||
char[] buffer = new char[TRANSFER_BUFFER_SIZE];
|
||||
int nRead;
|
||||
while ((nRead = read(buffer, 0, TRANSFER_BUFFER_SIZE)) >= 0) {
|
||||
out.write(buffer, 0, nRead);
|
||||
transferred += nRead;
|
||||
}
|
||||
return transferred;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue