Description
Copy some or all chars from a large (over 2GB)
InputStream
to an
OutputStream
, optionally skipping input chars.
License
Apache License
Parameter
Parameter | Description |
---|
input | the <code>Reader</code> to read from |
output | the <code>Writer</code> to write to |
inputOffset | : number of chars to skip from input before copying -ve values are ignored |
length | : number of chars to copy. -ve means all |
buffer | the buffer to be used for the copy |
Exception
Parameter | Description |
---|
NullPointerException | if the input or output is null |
IOException | if an I/O error occurs |
Return
the number of chars copied
Declaration
public static long copyLarge(Reader input, Writer output, final long inputOffset, final long length,
char[] buffer) throws IOException
Method Source Code
//package com.java2s;
/*/* w w w. ja v a 2 s . c om*/
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
public class Main {
private static final int EOF = -1;
/**
* The default buffer size ({@value}) to use for
* {@link #copyLarge(InputStream, OutputStream)}
* and
* {@link #copyLarge(Reader, Writer)}
*/
private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
/**
* The default buffer size to use for the skip() methods.
*/
private static final int SKIP_BUFFER_SIZE = 2048;
private static char[] SKIP_CHAR_BUFFER;
private static byte[] SKIP_BYTE_BUFFER;
/**
* Copy bytes from a large (over 2GB) <code>InputStream</code> to an
* <code>OutputStream</code>.
* <p>
* This method buffers the input internally, so there is no need to use a
* <code>BufferedInputStream</code>.
* <p>
* The buffer size is given by {@link #DEFAULT_BUFFER_SIZE}.
*
* @param input the <code>InputStream</code> to read from
* @param output the <code>OutputStream</code> to write to
* @return the number of bytes copied
* @throws NullPointerException if the input or output is null
* @throws IOException if an I/O error occurs
* @since 1.3
*/
public static long copyLarge(InputStream input, OutputStream output) throws IOException {
return copyLarge(input, output, new byte[DEFAULT_BUFFER_SIZE]);
}
/**
* Copy bytes from a large (over 2GB) <code>InputStream</code> to an
* <code>OutputStream</code>.
* <p>
* This method uses the provided buffer, so there is no need to use a
* <code>BufferedInputStream</code>.
* <p>
*
* @param input the <code>InputStream</code> to read from
* @param output the <code>OutputStream</code> to write to
* @param buffer the buffer to use for the copy
* @return the number of bytes copied
* @throws NullPointerException if the input or output is null
* @throws IOException if an I/O error occurs
* @since 2.2
*/
public static long copyLarge(InputStream input, OutputStream output, byte[] buffer) throws IOException {
long count = 0;
int n = 0;
while (EOF != (n = input.read(buffer))) {
output.write(buffer, 0, n);
count += n;
}
return count;
}
/**
* Copy some or all bytes from a large (over 2GB) <code>InputStream</code> to an
* <code>OutputStream</code>, optionally skipping input bytes.
* <p>
* This method buffers the input internally, so there is no need to use a
* <code>BufferedInputStream</code>.
* <p>
* The buffer size is given by {@link #DEFAULT_BUFFER_SIZE}.
*
* @param input the <code>InputStream</code> to read from
* @param output the <code>OutputStream</code> to write to
* @param inputOffset : number of bytes to skip from input before copying
* -ve values are ignored
* @param length : number of bytes to copy. -ve means all
* @return the number of bytes copied
* @throws NullPointerException if the input or output is null
* @throws IOException if an I/O error occurs
* @since 2.2
*/
public static long copyLarge(InputStream input, OutputStream output, long inputOffset, long length)
throws IOException {
return copyLarge(input, output, inputOffset, length, new byte[DEFAULT_BUFFER_SIZE]);
}
/**
* Copy some or all bytes from a large (over 2GB) <code>InputStream</code> to an
* <code>OutputStream</code>, optionally skipping input bytes.
* <p>
* This method uses the provided buffer, so there is no need to use a
* <code>BufferedInputStream</code>.
* <p>
*
* @param input the <code>InputStream</code> to read from
* @param output the <code>OutputStream</code> to write to
* @param inputOffset : number of bytes to skip from input before copying
* -ve values are ignored
* @param length : number of bytes to copy. -ve means all
* @param buffer the buffer to use for the copy
*
* @return the number of bytes copied
* @throws NullPointerException if the input or output is null
* @throws IOException if an I/O error occurs
* @since 2.2
*/
public static long copyLarge(InputStream input, OutputStream output, final long inputOffset, final long length,
byte[] buffer) throws IOException {
if (inputOffset > 0) {
skipFully(input, inputOffset);
}
if (length == 0) {
return 0;
}
final int bufferLength = buffer.length;
int bytesToRead = bufferLength;
if (length > 0 && length < bufferLength) {
bytesToRead = (int) length;
}
int read;
long totalRead = 0;
while (bytesToRead > 0 && EOF != (read = input.read(buffer, 0, bytesToRead))) {
output.write(buffer, 0, read);
totalRead += read;
if (length > 0) { // only adjust length if not reading to the end
// Note the cast must work because buffer.length is an integer
bytesToRead = (int) Math.min(length - totalRead, bufferLength);
}
}
return totalRead;
}
/**
* Copy chars from a large (over 2GB) <code>Reader</code> to a <code>Writer</code>.
* <p>
* This method buffers the input internally, so there is no need to use a
* <code>BufferedReader</code>.
* <p>
* The buffer size is given by {@link #DEFAULT_BUFFER_SIZE}.
*
* @param input the <code>Reader</code> to read from
* @param output the <code>Writer</code> to write to
* @return the number of characters copied
* @throws NullPointerException if the input or output is null
* @throws IOException if an I/O error occurs
* @since 1.3
*/
public static long copyLarge(Reader input, Writer output) throws IOException {
return copyLarge(input, output, new char[DEFAULT_BUFFER_SIZE]);
}
/**
* Copy chars from a large (over 2GB) <code>Reader</code> to a <code>Writer</code>.
* <p>
* This method uses the provided buffer, so there is no need to use a
* <code>BufferedReader</code>.
* <p>
*
* @param input the <code>Reader</code> to read from
* @param output the <code>Writer</code> to write to
* @param buffer the buffer to be used for the copy
* @return the number of characters copied
* @throws NullPointerException if the input or output is null
* @throws IOException if an I/O error occurs
* @since 2.2
*/
public static long copyLarge(Reader input, Writer output, char[] buffer) throws IOException {
long count = 0;
int n = 0;
while (EOF != (n = input.read(buffer))) {
output.write(buffer, 0, n);
count += n;
}
return count;
}
/**
* Copy some or all chars from a large (over 2GB) <code>InputStream</code> to an
* <code>OutputStream</code>, optionally skipping input chars.
* <p>
* This method buffers the input internally, so there is no need to use a
* <code>BufferedReader</code>.
* <p>
* The buffer size is given by {@link #DEFAULT_BUFFER_SIZE}.
*
* @param input the <code>Reader</code> to read from
* @param output the <code>Writer</code> to write to
* @param inputOffset : number of chars to skip from input before copying
* -ve values are ignored
* @param length : number of chars to copy. -ve means all
* @return the number of chars copied
* @throws NullPointerException if the input or output is null
* @throws IOException if an I/O error occurs
* @since 2.2
*/
public static long copyLarge(Reader input, Writer output, final long inputOffset, final long length)
throws IOException {
return copyLarge(input, output, inputOffset, length, new char[DEFAULT_BUFFER_SIZE]);
}
/**
* Copy some or all chars from a large (over 2GB) <code>InputStream</code> to an
* <code>OutputStream</code>, optionally skipping input chars.
* <p>
* This method uses the provided buffer, so there is no need to use a
* <code>BufferedReader</code>.
* <p>
*
* @param input the <code>Reader</code> to read from
* @param output the <code>Writer</code> to write to
* @param inputOffset : number of chars to skip from input before copying
* -ve values are ignored
* @param length : number of chars to copy. -ve means all
* @param buffer the buffer to be used for the copy
* @return the number of chars copied
* @throws NullPointerException if the input or output is null
* @throws IOException if an I/O error occurs
* @since 2.2
*/
public static long copyLarge(Reader input, Writer output, final long inputOffset, final long length,
char[] buffer) throws IOException {
if (inputOffset > 0) {
skipFully(input, inputOffset);
}
if (length == 0) {
return 0;
}
int bytesToRead = buffer.length;
if (length > 0 && length < buffer.length) {
bytesToRead = (int) length;
}
int read;
long totalRead = 0;
while (bytesToRead > 0 && EOF != (read = input.read(buffer, 0, bytesToRead))) {
output.write(buffer, 0, read);
totalRead += read;
if (length > 0) { // only adjust length if not reading to the end
// Note the cast must work because buffer.length is an integer
bytesToRead = (int) Math.min(length - totalRead, buffer.length);
}
}
return totalRead;
}
/**
* Read characters from an input character stream.
* This implementation guarantees that it will read as many characters
* as possible before giving up; this may not always be the case for
* subclasses of {@link Reader}.
*
* @param input where to read input from
* @param buffer destination
* @param offset inital offset into buffer
* @param length length to read, must be >= 0
* @return actual length read; may be less than requested if EOF was reached
* @throws IOException if a read error occurs
* @since 2.2
*/
public static int read(Reader input, char[] buffer, int offset, int length) throws IOException {
if (length < 0) {
throw new IllegalArgumentException("Length must not be negative: " + length);
}
int remaining = length;
while (remaining > 0) {
int location = length - remaining;
int count = input.read(buffer, offset + location, remaining);
if (EOF == count) { // EOF
break;
}
remaining -= count;
}
return length - remaining;
}
/**
* Read characters from an input character stream.
* This implementation guarantees that it will read as many characters
* as possible before giving up; this may not always be the case for
* subclasses of {@link Reader}.
*
* @param input where to read input from
* @param buffer destination
* @return actual length read; may be less than requested if EOF was reached
* @throws IOException if a read error occurs
* @since 2.2
*/
public static int read(Reader input, char[] buffer) throws IOException {
return read(input, buffer, 0, buffer.length);
}
/**
* Read bytes from an input stream.
* This implementation guarantees that it will read as many bytes
* as possible before giving up; this may not always be the case for
* subclasses of {@link InputStream}.
*
* @param input where to read input from
* @param buffer destination
* @param offset inital offset into buffer
* @param length length to read, must be >= 0
* @return actual length read; may be less than requested if EOF was reached
* @throws IOException if a read error occurs
* @since 2.2
*/
public static int read(InputStream input, byte[] buffer, int offset, int length) throws IOException {
if (length < 0) {
throw new IllegalArgumentException("Length must not be negative: " + length);
}
int remaining = length;
while (remaining > 0) {
int location = length - remaining;
int count = input.read(buffer, offset + location, remaining);
if (EOF == count) { // EOF
break;
}
remaining -= count;
}
return length - remaining;
}
/**
* Read bytes from an input stream.
* This implementation guarantees that it will read as many bytes
* as possible before giving up; this may not always be the case for
* subclasses of {@link InputStream}.
*
* @param input where to read input from
* @param buffer destination
* @return actual length read; may be less than requested if EOF was reached
* @throws IOException if a read error occurs
* @since 2.2
*/
public static int read(InputStream input, byte[] buffer) throws IOException {
return read(input, buffer, 0, buffer.length);
}
/**
* Writes bytes from a <code>byte[]</code> to an <code>OutputStream</code>.
*
* @param data the byte array to write, do not modify during output,
* null ignored
* @param output the <code>OutputStream</code> to write to
* @throws NullPointerException if output is null
* @throws IOException if an I/O error occurs
* @since 1.1
*/
public static void write(byte[] data, OutputStream output) throws IOException {
if (data != null) {
output.write(data);
}
}
/**
* Writes bytes from a <code>byte[]</code> to chars on a <code>Writer</code>
* using the default character encoding of the platform.
* <p>
* This method uses {@link String#String(byte[])}.
*
* @param data the byte array to write, do not modify during output,
* null ignored
* @param output the <code>Writer</code> to write to
* @throws NullPointerException if output is null
* @throws IOException if an I/O error occurs
* @since 1.1
*/
public static void write(byte[] data, Writer output) throws IOException {
if (data != null) {
output.write(new String(data));
}
}
/**
* Writes bytes from a <code>byte[]</code> to chars on a <code>Writer</code>
* using the specified character encoding.
* <p>
* Character encoding names can be found at
* <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
* <p>
* This method uses {@link String#String(byte[], String)}.
*
* @param data the byte array to write, do not modify during output,
* null ignored
* @param output the <code>Writer</code> to write to
* @param encoding the encoding to use, null means platform default
* @throws NullPointerException if output is null
* @throws IOException if an I/O error occurs
* @since 1.1
*/
public static void write(byte[] data, Writer output, String encoding) throws IOException {
if (data != null) {
if (encoding == null) {
write(data, output);
} else {
output.write(new String(data, encoding));
}
}
}
/**
* Writes chars from a <code>char[]</code> to a <code>Writer</code>
* using the default character encoding of the platform.
*
* @param data the char array to write, do not modify during output,
* null ignored
* @param output the <code>Writer</code> to write to
* @throws NullPointerException if output is null
* @throws IOException if an I/O error occurs
* @since 1.1
*/
public static void write(char[] data, Writer output) throws IOException {
if (data != null) {
output.write(data);
}
}
/**
* Writes chars from a <code>char[]</code> to bytes on an
* <code>OutputStream</code>.
* <p>
* This method uses {@link String#String(char[])} and
* {@link String#getBytes()}.
*
* @param data the char array to write, do not modify during output,
* null ignored
* @param output the <code>OutputStream</code> to write to
* @throws NullPointerException if output is null
* @throws IOException if an I/O error occurs
* @since 1.1
*/
public static void write(char[] data, OutputStream output) throws IOException {
if (data != null) {
output.write(new String(data).getBytes());
}
}
/**
* Writes chars from a <code>char[]</code> to bytes on an
* <code>OutputStream</code> using the specified character encoding.
* <p>
* Character encoding names can be found at
* <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
* <p>
* This method uses {@link String#String(char[])} and
* {@link String#getBytes(String)}.
*
* @param data the char array to write, do not modify during output,
* null ignored
* @param output the <code>OutputStream</code> to write to
* @param encoding the encoding to use, null means platform default
* @throws NullPointerException if output is null
* @throws IOException if an I/O error occurs
* @since 1.1
*/
public static void write(char[] data, OutputStream output, String encoding) throws IOException {
if (data != null) {
if (encoding == null) {
write(data, output);
} else {
output.write(new String(data).getBytes(encoding));
}
}
}
/**
* Writes chars from a <code>CharSequence</code> to a <code>Writer</code>.
*
* @param data the <code>CharSequence</code> to write, null ignored
* @param output the <code>Writer</code> to write to
* @throws NullPointerException if output is null
* @throws IOException if an I/O error occurs
* @since 2.0
*/
public static void write(CharSequence data, Writer output) throws IOException {
if (data != null) {
write(data.toString(), output);
}
}
/**
* Writes chars from a <code>CharSequence</code> to bytes on an
* <code>OutputStream</code> using the default character encoding of the
* platform.
* <p>
* This method uses {@link String#getBytes()}.
*
* @param data the <code>CharSequence</code> to write, null ignored
* @param output the <code>OutputStream</code> to write to
* @throws NullPointerException if output is null
* @throws IOException if an I/O error occurs
* @since 2.0
*/
public static void write(CharSequence data, OutputStream output) throws IOException {
if (data != null) {
write(data.toString(), output);
}
}
/**
* Writes chars from a <code>CharSequence</code> to bytes on an
* <code>OutputStream</code> using the specified character encoding.
* <p>
* Character encoding names can be found at
* <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
* <p>
* This method uses {@link String#getBytes(String)}.
*
* @param data the <code>CharSequence</code> to write, null ignored
* @param output the <code>OutputStream</code> to write to
* @param encoding the encoding to use, null means platform default
* @throws NullPointerException if output is null
* @throws IOException if an I/O error occurs
* @since 2.0
*/
public static void write(CharSequence data, OutputStream output, String encoding) throws IOException {
if (data != null) {
write(data.toString(), output, encoding);
}
}
/**
* Writes chars from a <code>String</code> to a <code>Writer</code>.
*
* @param data the <code>String</code> to write, null ignored
* @param output the <code>Writer</code> to write to
* @throws NullPointerException if output is null
* @throws IOException if an I/O error occurs
* @since 1.1
*/
public static void write(String data, Writer output) throws IOException {
if (data != null) {
output.write(data);
}
}
/**
* Writes chars from a <code>String</code> to bytes on an
* <code>OutputStream</code> using the default character encoding of the
* platform.
* <p>
* This method uses {@link String#getBytes()}.
*
* @param data the <code>String</code> to write, null ignored
* @param output the <code>OutputStream</code> to write to
* @throws NullPointerException if output is null
* @throws IOException if an I/O error occurs
* @since 1.1
*/
public static void write(String data, OutputStream output) throws IOException {
if (data != null) {
output.write(data.getBytes());
}
}
/**
* Writes chars from a <code>String</code> to bytes on an
* <code>OutputStream</code> using the specified character encoding.
* <p>
* Character encoding names can be found at
* <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
* <p>
* This method uses {@link String#getBytes(String)}.
*
* @param data the <code>String</code> to write, null ignored
* @param output the <code>OutputStream</code> to write to
* @param encoding the encoding to use, null means platform default
* @throws NullPointerException if output is null
* @throws IOException if an I/O error occurs
* @since 1.1
*/
public static void write(String data, OutputStream output, String encoding) throws IOException {
if (data != null) {
if (encoding == null) {
write(data, output);
} else {
output.write(data.getBytes(encoding));
}
}
}
/**
* Writes chars from a <code>StringBuffer</code> to a <code>Writer</code>.
*
* @param data the <code>StringBuffer</code> to write, null ignored
* @param output the <code>Writer</code> to write to
* @throws NullPointerException if output is null
* @throws IOException if an I/O error occurs
* @since 1.1
* @deprecated replaced by write(CharSequence, Writer)
*/
@Deprecated
public static void write(StringBuffer data, Writer output) throws IOException {
if (data != null) {
output.write(data.toString());
}
}
/**
* Writes chars from a <code>StringBuffer</code> to bytes on an
* <code>OutputStream</code> using the default character encoding of the
* platform.
* <p>
* This method uses {@link String#getBytes()}.
*
* @param data the <code>StringBuffer</code> to write, null ignored
* @param output the <code>OutputStream</code> to write to
* @throws NullPointerException if output is null
* @throws IOException if an I/O error occurs
* @since 1.1
* @deprecated replaced by write(CharSequence, OutputStream)
*/
@Deprecated
public static void write(StringBuffer data, OutputStream output) throws IOException {
if (data != null) {
output.write(data.toString().getBytes());
}
}
/**
* Writes chars from a <code>StringBuffer</code> to bytes on an
* <code>OutputStream</code> using the specified character encoding.
* <p>
* Character encoding names can be found at
* <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
* <p>
* This method uses {@link String#getBytes(String)}.
*
* @param data the <code>StringBuffer</code> to write, null ignored
* @param output the <code>OutputStream</code> to write to
* @param encoding the encoding to use, null means platform default
* @throws NullPointerException if output is null
* @throws IOException if an I/O error occurs
* @since 1.1
* @deprecated replaced by write(CharSequence, OutputStream, String)
*/
@Deprecated
public static void write(StringBuffer data, OutputStream output, String encoding) throws IOException {
if (data != null) {
if (encoding == null) {
write(data, output);
} else {
output.write(data.toString().getBytes(encoding));
}
}
}
/**
* Skip the requested number of bytes or fail if there are not enough left.
* <p>
* This allows for the possibility that {@link InputStream#skip(long)} may
* not skip as many bytes as requested (most likely because of reaching EOF).
*
* @param input stream to skip
* @param toSkip the number of bytes to skip
* @see InputStream#skip(long)
*
* @throws IOException if there is a problem reading the file
* @throws IllegalArgumentException if toSkip is negative
* @throws EOFException if the number of bytes skipped was incorrect
* @since 2.0
*/
public static void skipFully(InputStream input, long toSkip) throws IOException {
if (toSkip < 0) {
throw new IllegalArgumentException("Bytes to skip must not be negative: " + toSkip);
}
long skipped = skip(input, toSkip);
if (skipped != toSkip) {
throw new EOFException("Bytes to skip: " + toSkip + " actual: " + skipped);
}
}
/**
* Skip the requested number of characters or fail if there are not enough left.
* <p>
* This allows for the possibility that {@link Reader#skip(long)} may
* not skip as many characters as requested (most likely because of reaching EOF).
*
* @param input stream to skip
* @param toSkip the number of characters to skip
* @see Reader#skip(long)
*
* @throws IOException if there is a problem reading the file
* @throws IllegalArgumentException if toSkip is negative
* @throws EOFException if the number of characters skipped was incorrect
* @since 2.0
*/
public static void skipFully(Reader input, long toSkip) throws IOException {
long skipped = skip(input, toSkip);
if (skipped != toSkip) {
throw new EOFException("Chars to skip: " + toSkip + " actual: " + skipped);
}
}
/**
* Get the contents of a <code>byte[]</code> as a String
* using the default character encoding of the platform.
*
* @param input the byte array to read from
* @return the requested String
* @throws NullPointerException if the input is null
* @throws IOException if an I/O error occurs (never occurs)
* @deprecated Use {@link String#String(byte[])}
*/
@Deprecated
public static String toString(byte[] input) throws IOException {
return new String(input);
}
/**
* Get the contents of a <code>byte[]</code> as a String
* using the specified character encoding.
* <p>
* Character encoding names can be found at
* <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
*
* @param input the byte array to read from
* @param encoding the encoding to use, null means platform default
* @return the requested String
* @throws NullPointerException if the input is null
* @throws IOException if an I/O error occurs (never occurs)
* @deprecated Use {@link String#String(byte[],String)}
*/
@Deprecated
public static String toString(byte[] input, String encoding) throws IOException {
if (encoding == null) {
return new String(input);
} else {
return new String(input, encoding);
}
}
/**
* Skip bytes from an input byte stream.
* This implementation guarantees that it will read as many bytes
* as possible before giving up; this may not always be the case for
* subclasses of {@link Reader}.
*
* @param input byte stream to skip
* @param toSkip number of bytes to skip.
* @return number of bytes actually skipped.
*
* @see InputStream#skip(long)
*
* @throws IOException if there is a problem reading the file
* @throws IllegalArgumentException if toSkip is negative
* @since 2.0
*/
public static long skip(InputStream input, long toSkip) throws IOException {
if (toSkip < 0) {
throw new IllegalArgumentException("Skip count must be non-negative, actual: " + toSkip);
}
/*
* N.B. no need to synchronize this because: - we don't care if the buffer is created multiple times (the data
* is ignored) - we always use the same size buffer, so if it it is recreated it will still be OK (if the buffer
* size were variable, we would need to synch. to ensure some other thread did not create a smaller one)
*/
if (SKIP_BYTE_BUFFER == null) {
SKIP_BYTE_BUFFER = new byte[SKIP_BUFFER_SIZE];
}
long remain = toSkip;
while (remain > 0) {
long n = input.read(SKIP_BYTE_BUFFER, 0, (int) Math.min(remain, SKIP_BUFFER_SIZE));
if (n < 0) { // EOF
break;
}
remain -= n;
}
return toSkip - remain;
}
/**
* Skip characters from an input character stream.
* This implementation guarantees that it will read as many characters
* as possible before giving up; this may not always be the case for
* subclasses of {@link Reader}.
*
* @param input character stream to skip
* @param toSkip number of characters to skip.
* @return number of characters actually skipped.
*
* @see Reader#skip(long)
*
* @throws IOException if there is a problem reading the file
* @throws IllegalArgumentException if toSkip is negative
* @since 2.0
*/
public static long skip(Reader input, long toSkip) throws IOException {
if (toSkip < 0) {
throw new IllegalArgumentException("Skip count must be non-negative, actual: " + toSkip);
}
/*
* N.B. no need to synchronize this because: - we don't care if the buffer is created multiple times (the data
* is ignored) - we always use the same size buffer, so if it it is recreated it will still be OK (if the buffer
* size were variable, we would need to synch. to ensure some other thread did not create a smaller one)
*/
if (SKIP_CHAR_BUFFER == null) {
SKIP_CHAR_BUFFER = new char[SKIP_BUFFER_SIZE];
}
long remain = toSkip;
while (remain > 0) {
long n = input.read(SKIP_CHAR_BUFFER, 0, (int) Math.min(remain, SKIP_BUFFER_SIZE));
if (n < 0) { // EOF
break;
}
remain -= n;
}
return toSkip - remain;
}
}
Related
- copyLarge(Reader input, Writer output)
- copyLarge(Reader input, Writer output)
- copyLarge(Reader input, Writer output, char[] buffer)
- copyLarge(Reader input, Writer output, char[] buffer)
- copyLarge(Reader input, Writer output, char[] buffer)
- copyLarge(Reader input, Writer output, long limit)