Class IOUtils
- java.lang.Object
-
- totalcross.util.IOUtils
-
public final class IOUtils extends java.lang.ObjectLargely based on Apache IOUtils Only needed methods copied, like copy InputStream -> OutoutStream and copy Reader -> Writer Javadoc maintained where possible, but the "since" tag was largely removed
-
-
Field Summary
Fields Modifier and Type Field Description static intDEFAULT_BUFFER_SIZEThe default buffer size (4096) to use forcopyLarge(InputStream, OutputStream)andcopyLarge(Reader, Writer)static intEOFRepresents the end-of-file (or stream).
-
Constructor Summary
Constructors Constructor Description IOUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static longcopy(java.io.InputStream input, java.io.OutputStream output, int bufferSize)Copies bytes from anInputStreamto anOutputStreamusing an internal buffer of the given size.static intcopy(java.io.Reader input, java.io.Writer output)Copies chars from aReaderto aWriter.static longcopyLarge(java.io.InputStream input, java.io.OutputStream output)Copies bytes from a large (over 2GB)InputStreamto anOutputStream.static longcopyLarge(java.io.InputStream input, java.io.OutputStream output, byte[] buffer)Copies bytes from a large (over 2GB)InputStreamto anOutputStream.static longcopyLarge(java.io.InputStream input, java.io.OutputStream output, long inputOffset, long length)Copies some or all bytes from a large (over 2GB)InputStreamto anOutputStream, optionally skipping input bytes.static longcopyLarge(java.io.InputStream input, java.io.OutputStream output, long inputOffset, long length, byte[] buffer)Copies some or all bytes from a large (over 2GB)InputStreamto anOutputStream, optionally skipping input bytes.static longcopyLarge(java.io.Reader input, java.io.Writer output)Copies chars from a large (over 2GB)Readerto aWriter.static longcopyLarge(java.io.Reader input, java.io.Writer output, char[] buffer)Copies chars from a large (over 2GB)Readerto aWriter.static longskip(java.io.InputStream input, long toSkip)Skips bytes from an input byte stream.static voidskipFully(java.io.InputStream input, long toSkip)Skips the requested number of bytes or fail if there are not enough left.
-
-
-
Field Detail
-
EOF
public static final int EOF
Represents the end-of-file (or stream).- See Also:
- Constant Field Values
-
DEFAULT_BUFFER_SIZE
public static final int DEFAULT_BUFFER_SIZE
The default buffer size (4096) to use forcopyLarge(InputStream, OutputStream)andcopyLarge(Reader, Writer)- See Also:
- Constant Field Values
-
-
Method Detail
-
copy
public static long copy(java.io.InputStream input, java.io.OutputStream output, int bufferSize) throws java.io.IOExceptionCopies bytes from anInputStreamto anOutputStreamusing an internal buffer of the given size.This method buffers the input internally, so there is no need to use a
BufferedInputStream.- Parameters:
input- theInputStreamto read fromoutput- theOutputStreamto write tobufferSize- the bufferSize used to copy from the input to the output- Returns:
- the number of bytes copied
- Throws:
java.lang.NullPointerException- if the input or output is nulljava.io.IOException- if an I/O error occurs
-
copyLarge
public static long copyLarge(java.io.InputStream input, java.io.OutputStream output, long inputOffset, long length) throws java.io.IOExceptionCopies some or all bytes from a large (over 2GB)InputStreamto anOutputStream, optionally skipping input bytes.This method buffers the input internally, so there is no need to use a
BufferedInputStream.Note that the implementation uses
The buffer size is given byskip(InputStream, long). This means that the method may be considerably less efficient than using the actual skip implementation, this is done to guarantee that the correct number of characters are skipped.DEFAULT_BUFFER_SIZE.- Parameters:
input- theInputStreamto read fromoutput- theOutputStreamto write toinputOffset- : number of bytes to skip from input before copying -ve values are ignoredlength- : number of bytes to copy. -ve means all- Returns:
- the number of bytes copied
- Throws:
java.lang.NullPointerException- if the input or output is nulljava.io.IOException- if an I/O error occurs
-
copyLarge
public static long copyLarge(java.io.InputStream input, java.io.OutputStream output) throws java.io.IOExceptionCopies bytes from a large (over 2GB)InputStreamto anOutputStream.This method buffers the input internally, so there is no need to use a
BufferedInputStream.The buffer size is given by
DEFAULT_BUFFER_SIZE.- Parameters:
input- theInputStreamto read fromoutput- theOutputStreamto write to- Returns:
- the number of bytes copied
- Throws:
java.lang.NullPointerException- if the input or output is nulljava.io.IOException- if an I/O error occurs
-
copyLarge
public static long copyLarge(java.io.InputStream input, java.io.OutputStream output, byte[] buffer) throws java.io.IOExceptionCopies bytes from a large (over 2GB)InputStreamto anOutputStream.This method uses the provided buffer, so there is no need to use a
BufferedInputStream.- Parameters:
input- theInputStreamto read fromoutput- theOutputStreamto write tobuffer- the buffer to use for the copy- Returns:
- the number of bytes copied
- Throws:
java.lang.NullPointerException- if the input or output is nulljava.io.IOException- if an I/O error occurs
-
copyLarge
public static long copyLarge(java.io.InputStream input, java.io.OutputStream output, long inputOffset, long length, byte[] buffer) throws java.io.IOExceptionCopies some or all bytes from a large (over 2GB)InputStreamto anOutputStream, optionally skipping input bytes.This method uses the provided buffer, so there is no need to use a
BufferedInputStream.Note that the implementation uses
skip(InputStream, long). This means that the method may be considerably less efficient than using the actual skip implementation, this is done to guarantee that the correct number of characters are skipped.- Parameters:
input- theInputStreamto read fromoutput- theOutputStreamto write toinputOffset- : number of bytes to skip from input before copying -ve values are ignoredlength- : number of bytes to copy. -ve means allbuffer- the buffer to use for the copy- Returns:
- the number of bytes copied
- Throws:
java.lang.NullPointerException- if the input or output is nulljava.io.IOException- if an I/O error occurs
-
skipFully
public static void skipFully(java.io.InputStream input, long toSkip) throws java.io.IOExceptionSkips the requested number of bytes or fail if there are not enough left.This allows for the possibility that
InputStream.skip(long)may not skip as many bytes as requested (most likely because of reaching EOF).Note that the implementation uses
skip(InputStream, long). This means that the method may be considerably less efficient than using the actual skip implementation, this is done to guarantee that the correct number of characters are skipped.- Parameters:
input- stream to skiptoSkip- the number of bytes to skip- Throws:
java.io.IOException- if there is a problem reading the filejava.lang.IllegalArgumentException- if toSkip is negativejava.io.EOFException- if the number of bytes skipped was incorrect- See Also:
InputStream.skip(long)
-
skip
public static long skip(java.io.InputStream input, long toSkip) throws java.io.IOExceptionSkips 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 skip() implementations in subclasses ofInputStream.Note that the implementation uses
InputStream.read(byte[], int, int)rather than delegating toInputStream.skip(long). This means that the method may be considerably less efficient than using the actual skip implementation, this is done to guarantee that the correct number of bytes are skipped.- Parameters:
input- byte stream to skiptoSkip- number of bytes to skip.- Returns:
- number of bytes actually skipped.
- Throws:
java.io.IOException- if there is a problem reading the filejava.lang.IllegalArgumentException- if toSkip is negative- See Also:
InputStream.skip(long), IO-203 - Add skipFully() method for InputStreams
-
copy
public static int copy(java.io.Reader input, java.io.Writer output) throws java.io.IOExceptionCopies chars from aReaderto aWriter.This method buffers the input internally, so there is no need to use a
BufferedReader.Large streams (over 2GB) will return a chars copied value of
-1after the copy has completed since the correct number of chars cannot be returned as an int. For large streams use thecopyLarge(Reader, Writer)method.- Parameters:
input- theReaderto read fromoutput- theWriterto write to- Returns:
- the number of characters copied, or -1 if > Integer.MAX_VALUE
- Throws:
java.lang.NullPointerException- if the input or output is nulljava.io.IOException- if an I/O error occurs- Since:
- 1.1
-
copyLarge
public static long copyLarge(java.io.Reader input, java.io.Writer output) throws java.io.IOExceptionCopies chars from a large (over 2GB)Readerto aWriter.This method buffers the input internally, so there is no need to use a
BufferedReader.The buffer size is given by
DEFAULT_BUFFER_SIZE.- Parameters:
input- theReaderto read fromoutput- theWriterto write to- Returns:
- the number of characters copied
- Throws:
java.lang.NullPointerException- if the input or output is nulljava.io.IOException- if an I/O error occurs- Since:
- 1.3
-
copyLarge
public static long copyLarge(java.io.Reader input, java.io.Writer output, char[] buffer) throws java.io.IOExceptionCopies chars from a large (over 2GB)Readerto aWriter.This method uses the provided buffer, so there is no need to use a
BufferedReader.- Parameters:
input- theReaderto read fromoutput- theWriterto write tobuffer- the buffer to be used for the copy- Returns:
- the number of characters copied
- Throws:
java.lang.NullPointerException- if the input or output is nulljava.io.IOException- if an I/O error occurs- Since:
- 2.2
-
-