Top Description Fields Constructors Methods
org.eclipse.jetty.util

public Class IO

extends Object
Class Inheritance
Imports
java.io.ByteArrayOutputStream, .Closeable, .File, .FileInputStream, .FileOutputStream, .IOException, .InputStream, .InputStreamReader, .InterruptedIOException, .OutputStream, .PrintWriter, .Reader, .StringWriter, .Writer, java.net.URI, java.nio.ByteBuffer, java.nio.channels.GatheringByteChannel, java.nio.charset.Charset, java.nio.file.CopyOption, .FileVisitResult, .Files, .Path, .SimpleFileVisitor, java.nio.file.attribute.BasicFileAttributes, java.util.Iterator, .Objects, java.util.concurrent.CompletionException, .ExecutionException, java.util.stream.Stream, org.slf4j.Logger, .LoggerFactory

IO Utilities. Provides stream handling utilities in singleton Threadpool implementation accessed by static members.

Field Summary

Modifier and TypeField and Description
public static final int
public static final String
public static final byte[]
private static final Logger

Constructor Summary

AccessConstructor and Description
private
IO()

Method Summary

Modifier and TypeMethod and Description
public static File

Returns:

A File representation of the passed argument or null.
asFile
(Object
A File, String, Path or null to be converted into a File
fileObject
)

Convert an object to a File if possible.

public static void
close(AutoCloseable
the closeable to close
closeable
)

Closes an arbitrary closable, and logs exceptions at ignore level

public static void
close(Closeable
the closeable to close
closeable
)

Closes an arbitrary closable, and logs exceptions at ignore level

public static void
close(InputStream
the input stream to close
is
)

closes an input stream, and logs exceptions

public static void
close(OutputStream
the output stream to close
os
)

closes an output stream, and logs exceptions

public static void
close(Reader
the reader to close
reader
)

closes a reader, and logs exceptions

public static void
close(Writer
the writer to close
writer
)

closes a writer, and logs exceptions

public static void
copy(InputStream
the input stream to read from (until EOF)
in
,
OutputStream
the output stream to write to
out
)

Copy Stream in to Stream out until EOF or exception.

public static void
copy(Reader
the read to read from (until EOF)
in
,
Writer
the writer to write to
out
)

Copy Reader to Writer out until EOF or exception.

public static void
copy(InputStream
the stream to read from
in
,
OutputStream
the stream to write to
out
,
long
the number of bytes to copy
byteCount
)

Copy Stream in to Stream for byteCount bytes or until EOF or exception.

public static void
copy(Reader
the Reader to read from
in
,
Writer
the Writer to write to
out
,
long
the number of bytes to copy
byteCount
)

Copy Reader to Writer for byteCount bytes or until EOF or exception.

public static void
copy(File
the file to copy
from
,
File
the destination to copy to
to
)

Copy files or directories

public static void
copyDir(File from, File to)

public static void
copyDir(Path
the source directory
srcDir
,
Path
the destination directory
destDir
,
CopyOption... copyOptions)

Copy the contents of a directory from one directory to another.

public static void
copyFile(File from, File to)

public static boolean

Returns:

true if file was deleted, or directory referenced was deleted. false if file doesn't exist, or was null.
delete
(File
The file (or directory) to be deleted.
file
)

Delete File.

public static boolean
delete(Path path)

public static boolean

Returns:

true if directory is null, doesn't exist, or has no content. false if not a directory, or has contents
isEmptyDir
(File
the directory
dir
)

Test if directory is empty.

public static byte[]
public static IOException
public static String

Returns:

the String parsed from path (default Charset)
toString
(Path
the path to read from (until EOF)
path
,
Charset
the charset to read with
charset
)

Read Path to string.

public static String

Returns:

the String parsed from stream (default Charset)
toString
(InputStream
the stream to read from (until EOF)
in
)

Read input stream to string.

public static String

Returns:

the String parsed from the stream
toString
(InputStream
the stream to read from (until EOF)
in
,
String
the encoding to use (can be null to use default Charset)
encoding
)

Read input stream to string.

public static String

Returns:

the String parsed from the stream
toString
(InputStream
the stream to read from (until EOF)
in
,
Charset
the Charset to use (can be null to use default Charset)
encoding
)

Read input stream to string.

public static String

Returns:

the String parsed from the reader
toString
(Reader
the reader to read from (until EOF)
in
)

Read input stream to string.

public static long

Returns:

The total bytes written
write
(GatheringByteChannel
The GatheringByteChannel to write to
out
,
ByteBuffer[]
The buffers to write
buffers
,
int
The offset into the buffers array
offset
,
int
The length in buffers to write
length
)

A gathering write utility wrapper.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

bufferSizeback to summary
public static final int bufferSize
CRLFback to summary
public static final String CRLF
CRLF_BYTESback to summary
public static final byte[] CRLF_BYTES
LOGback to summary
private static final Logger LOG

Constructor Detail

IOback to summary
private IO()

Method Detail

asFileback to summary
public static File asFile(Object fileObject)

Convert an object to a File if possible.

Parameters
fileObject:Object

A File, String, Path or null to be converted into a File

Returns:File

A File representation of the passed argument or null.

closeback to summary
public static void close(AutoCloseable closeable)

Closes an arbitrary closable, and logs exceptions at ignore level

Parameters
closeable:AutoCloseable

the closeable to close

closeback to summary
public static void close(Closeable closeable)

Closes an arbitrary closable, and logs exceptions at ignore level

Parameters
closeable:Closeable

the closeable to close

closeback to summary
public static void close(InputStream is)

closes an input stream, and logs exceptions

Parameters
is:InputStream

the input stream to close

closeback to summary
public static void close(OutputStream os)

closes an output stream, and logs exceptions

Parameters
os:OutputStream

the output stream to close

closeback to summary
public static void close(Reader reader)

closes a reader, and logs exceptions

Parameters
reader:Reader

the reader to close

closeback to summary
public static void close(Writer writer)

closes a writer, and logs exceptions

Parameters
writer:Writer

the writer to close

copyback to summary
public static void copy(InputStream in, OutputStream out) throws IOException

Copy Stream in to Stream out until EOF or exception.

Parameters
in:InputStream

the input stream to read from (until EOF)

out:OutputStream

the output stream to write to

Exceptions
IOException:
if unable to copy streams
copyback to summary
public static void copy(Reader in, Writer out) throws IOException

Copy Reader to Writer out until EOF or exception.

Parameters
in:Reader

the read to read from (until EOF)

out:Writer

the writer to write to

Exceptions
IOException:
if unable to copy the streams
copyback to summary
public static void copy(InputStream in, OutputStream out, long byteCount) throws IOException

Copy Stream in to Stream for byteCount bytes or until EOF or exception.

Parameters
in:InputStream

the stream to read from

out:OutputStream

the stream to write to

byteCount:long

the number of bytes to copy

Exceptions
IOException:
if unable to copy the streams
copyback to summary
public static void copy(Reader in, Writer out, long byteCount) throws IOException

Copy Reader to Writer for byteCount bytes or until EOF or exception.

Parameters
in:Reader

the Reader to read from

out:Writer

the Writer to write to

byteCount:long

the number of bytes to copy

Exceptions
IOException:
if unable to copy streams
copyback to summary
public static void copy(File from, File to) throws IOException

Copy files or directories

Parameters
from:File

the file to copy

to:File

the destination to copy to

Exceptions
IOException:
if unable to copy
copyDirback to summary
public static void copyDir(File from, File to) throws IOException
copyDirback to summary
public static void copyDir(Path srcDir, Path destDir, CopyOption... copyOptions) throws IOException

Copy the contents of a directory from one directory to another.

Parameters
srcDir:Path

the source directory

destDir:Path

the destination directory

Exceptions
IOException:
if unable to copy the file
copyFileback to summary
public static void copyFile(File from, File to) throws IOException
deleteback to summary
public static boolean delete(File file)

Delete File. This delete will recursively delete directories - BE CAREFUL

Parameters
file:File

The file (or directory) to be deleted.

Returns:boolean

true if file was deleted, or directory referenced was deleted. false if file doesn't exist, or was null.

deleteback to summary
public static boolean delete(Path path)
isEmptyDirback to summary
public static boolean isEmptyDir(File dir)

Test if directory is empty.

Parameters
dir:File

the directory

Returns:boolean

true if directory is null, doesn't exist, or has no content. false if not a directory, or has contents

readBytesback to summary
public static byte[] readBytes(InputStream in) throws IOException
rethrowback to summary
public static IOException rethrow(Throwable cause)
toStringback to summary
public static String toString(Path path, Charset charset) throws IOException

Read Path to string.

Parameters
path:Path

the path to read from (until EOF)

charset:Charset

the charset to read with

Returns:String

the String parsed from path (default Charset)

Exceptions
IOException:
if unable to read the path (or handle the charset)
toStringback to summary
public static String toString(InputStream in) throws IOException

Read input stream to string.

Parameters
in:InputStream

the stream to read from (until EOF)

Returns:String

the String parsed from stream (default Charset)

Exceptions
IOException:
if unable to read the stream (or handle the charset)
toStringback to summary
public static String toString(InputStream in, String encoding) throws IOException

Read input stream to string.

Parameters
in:InputStream

the stream to read from (until EOF)

encoding:String

the encoding to use (can be null to use default Charset)

Returns:String

the String parsed from the stream

Exceptions
IOException:
if unable to read the stream (or handle the charset)
toStringback to summary
public static String toString(InputStream in, Charset encoding) throws IOException

Read input stream to string.

Parameters
in:InputStream

the stream to read from (until EOF)

encoding:Charset

the Charset to use (can be null to use default Charset)

Returns:String

the String parsed from the stream

Exceptions
IOException:
if unable to read the stream (or handle the charset)
toStringback to summary
public static String toString(Reader in) throws IOException

Read input stream to string.

Parameters
in:Reader

the reader to read from (until EOF)

Returns:String

the String parsed from the reader

Exceptions
IOException:
if unable to read the stream (or handle the charset)
writeback to summary
public static long write(GatheringByteChannel out, ByteBuffer[] buffers, int offset, int length) throws IOException

A gathering write utility wrapper.

This method wraps a gather write with a loop that handles the limitations of some operating systems that have a limit on the number of buffers written. The method loops on the write until either all the content is written or no progress is made.

Parameters
out:GatheringByteChannel

The GatheringByteChannel to write to

buffers:ByteBuffer[]

The buffers to write

offset:int

The offset into the buffers array

length:int

The length in buffers to write

Returns:long

The total bytes written

Exceptions
IOException:
if unable write to the GatheringByteChannel