|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectariba.util.core.IOUtil
public final class IOUtil
Input/Output Utilities. These are helper functions for dealing with i/o.
Nested Class Summary | |
---|---|
static class |
IOUtil.BufferHolder
Holds a byte buffer for use in this class while copying bytes around. |
Field Summary | |
---|---|
static int |
DefaultInternalBufferSize
When internal buffers are used in buffered I/O, the size is specified by this value. |
Method Summary | |
---|---|
static java.io.BufferedInputStream |
bufferedInputStream(java.io.File file)
Opens a binary file on top of a buffered layer. |
static java.io.BufferedOutputStream |
bufferedOutputStream(java.io.File file)
Opens a binary file on top of a buffered layer. |
static java.io.BufferedOutputStream |
bufferedOutputStream(java.io.File file,
boolean append)
Opens a binary file on top of a buffered layer. |
static java.io.BufferedReader |
bufferedReader(java.io.File file,
java.lang.String encoding)
Opens a text file on top of a buffered layer. |
static java.io.BufferedReader |
bufferedReader(java.io.InputStream i,
java.lang.String encoding)
Opens a text stream over a binary one on top of a buffered layer. |
static java.io.BufferedWriter |
bufferedWriter(java.io.OutputStream o,
java.lang.String encoding)
Open a writer with the specified encoding |
static byte[] |
bytesFromURL(java.net.URL url)
Read the contents of a URL as a byte array . |
static void |
close(java.io.Closeable obj)
Helper method to close an Object which implements Closeable interface without exceptions in a null safe manner. |
static boolean |
copyDirectory(java.io.File source,
java.io.File target)
Copy recursively a directory Use this with caution. |
static boolean |
copyDirectory(java.io.File source,
java.io.File target,
boolean recursive,
boolean createTarget)
Copy recursively a directory Use this with caution. |
static boolean |
copyFile(java.io.File sourceFile,
java.io.File targetFile)
Copy the contents of a file to another file. |
static boolean |
copyFile(java.io.File sourceFile,
java.io.File targetFile,
byte[] buffer)
Copy the contents of a file to another file. |
static boolean |
copyFile(java.io.File sourceFile,
java.io.File targetFile,
char[] buffer)
Deprecated. use copyFile(File, File, byte[]) |
static boolean |
copyFileIfNewer(java.io.File sourceFile,
java.io.File targetFile)
Copy a file if the source is newer than the target |
static void |
copyUrlToFile(java.lang.String sourceURLString,
java.io.File targetFile)
Copy a URL (String format) to File. |
static boolean |
deleteDirectory(java.io.File dir)
Delete a directory and its subdirectories. |
static java.lang.String |
executeURL(java.lang.String url)
Returns the content of URL in String form using UTF8 encoding. |
static java.lang.String |
executeURL(java.lang.String url,
java.lang.String encoding)
Execute the Url String. |
static java.lang.String |
getDefaultSystemEncoding()
Return the System Wide default encoding. |
static int |
getInternalBufferHolderRecursionDepth()
Returns the recursion depth of the internal per-thread BufferHolder. |
static boolean |
grep(java.io.File file,
java.lang.String regex)
Returns true if the file contains
a string matching regex and false
otherwise. |
static boolean |
grep(java.io.File file,
java.lang.String regex,
java.nio.charset.Charset charset)
Returns true if the file contains
a string matching regex and false
otherwise. |
static boolean |
grep(java.io.Reader reader,
java.lang.String regex)
Greps the supplied reader character stream for regex
returning true the pattern is found in the character stream
and false otherwise. |
static boolean |
inputStreamToOutputStream(java.io.InputStream input,
java.io.OutputStream output)
Copies all data from an InputStream to an OutputStream. |
static boolean |
inputStreamToOutputStream(java.io.InputStream input,
java.io.OutputStream output,
byte[] buffer,
boolean flush)
Copies all data from an InputStream to an OutputStream. |
static int |
inputStreamToOutputStream(java.io.InputStream input,
java.io.OutputStream output,
byte[] buffer,
boolean flush,
int bytesToCopy)
Deprecated. Use spool(java.io.InputStream, java.io.OutputStream, byte[], boolean, long) instead. |
static byte[] |
makeByteBuffer()
Returns a new byte array buffer of size DefaultInternalBufferSize . |
static java.io.PrintWriter |
printWriter(java.io.File file,
java.lang.String encoding)
Opens a text file with a reasonable stream interface on top of a buffered layer. |
static java.io.PrintWriter |
printWriter(java.io.OutputStream o,
java.lang.String encoding)
Open a text stream over a binary one on top of a buffered layer. |
static java.io.PrintWriter |
printWriter(java.io.OutputStream o,
java.lang.String encoding,
boolean autoFlush)
Open a text stream over a binary one on top of a buffered layer. |
static boolean |
readerToWriter(java.io.Reader reader,
java.io.Writer writer)
Copy Reader to Writer. |
static boolean |
readerToWriter(java.io.Reader reader,
java.io.Writer writer,
char[] buffer)
Copy Reader to Writer. |
static java.lang.String |
readFileContent(java.io.File file)
Reads the first line from file assuming UTF-8. |
static java.lang.String |
readFileContent(java.io.File file,
java.nio.charset.Charset charset)
Reads the entire contents from file using the specified
charset to convert the bytes from the file into characters. |
static java.lang.String |
readFirstLine(java.io.File file)
Reads the first line from file assuming UTF-8. |
static java.lang.String |
readFirstLine(java.io.File file,
java.nio.charset.Charset charset)
Reads and returns the first line from file using
the specified charset to convert the bytes from
the file into characters. |
static java.lang.String |
readLine(java.io.InputStream in)
Read a line from an InputStream. |
static java.lang.String |
readLine(java.io.InputStream in,
char[] lineBuffer)
Read a line from an InputStream. |
static java.util.List<java.lang.String> |
readLines(java.io.File file,
java.nio.charset.Charset charset,
int n)
Reads and returns the first n lines from file using
the specified charset to convert the bytes from
the file into characters. |
static void |
searchReplace(java.io.File input,
java.io.File output,
java.lang.String regexToFind,
java.lang.String toReplace)
Searches and replaces input replacing regexToFind
with toReplace writing to output . |
static void |
searchReplace(java.io.File input,
java.io.File output,
java.lang.String regexToFind,
java.lang.String toReplace,
java.nio.charset.Charset charset)
Searches and replaces input replacing regexToFind
with toReplace writing to output . |
static void |
searchReplace(java.io.File input,
java.io.Writer writer,
java.lang.String regexToFind,
java.lang.String toReplace)
Searches and replaces input replacing regexToFind
with toReplace writing to writer . |
static void |
searchReplace(java.io.File input,
java.io.Writer writer,
java.lang.String regexToFind,
java.lang.String toReplace,
java.nio.charset.Charset charset)
Searches and replaces file replacing regexToFind
with toReplace writing to writer . |
static void |
searchReplace(java.io.Reader reader,
java.io.Writer writer,
java.lang.String regexToFind,
java.lang.String toReplace)
Searches the reader character stream and replaces every
occurrence of regexToFind with the replacement string
toReplace . |
static int |
spool(java.io.InputStream input,
byte[] buffer)
Copies up to buffer.length bytes from input to
buffer . |
static long |
spool(java.io.InputStream input,
java.io.OutputStream output)
Copies all the bytes from input to output . |
static long |
spool(java.io.InputStream input,
java.io.OutputStream output,
byte[] buffer,
boolean flushOutput,
long length)
Copies up to length bytes from input to
output using the supplied buffer . |
static long |
spool(java.io.InputStream input,
java.io.OutputStream output,
byte[] buffer,
long length)
Copies up to length bytes from input to
output using the supplied buffer . |
static long |
spool(java.io.InputStream input,
java.io.OutputStream output,
long length)
Copies up to length bytes from input to
output . |
static java.lang.String |
stringFromURL(java.net.URL url,
java.lang.String encoding)
Read the contents of a URL as a string. |
static void |
writeToFile(java.io.File file,
java.lang.Object object,
boolean append)
Writes object to file assuming UTF-8. |
static void |
writeToFile(java.io.File file,
java.lang.Object object,
boolean append,
java.nio.charset.Charset charset)
Writes object to file using the
specified charset . |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final int DefaultInternalBufferSize
Method Detail |
---|
public static int getInternalBufferHolderRecursionDepth()
public static byte[] makeByteBuffer()
DefaultInternalBufferSize
. This method is used by this
class for internal buffer allocation.
public static java.io.PrintWriter printWriter(java.io.File file, java.lang.String encoding) throws java.io.IOException
file
- the file to openencoding
- the encoding to use
java.io.IOException
- if there was an error opening the filepublic static java.io.PrintWriter printWriter(java.io.OutputStream o, java.lang.String encoding) throws java.io.UnsupportedEncodingException
java.io.UnsupportedEncodingException
public static java.io.PrintWriter printWriter(java.io.OutputStream o, java.lang.String encoding, boolean autoFlush) throws java.io.UnsupportedEncodingException
java.io.UnsupportedEncodingException
public static java.io.BufferedWriter bufferedWriter(java.io.OutputStream o, java.lang.String encoding) throws java.io.UnsupportedEncodingException
java.io.UnsupportedEncodingException
public static java.io.BufferedReader bufferedReader(java.io.File file, java.lang.String encoding) throws java.io.IOException
file
- the File to openedencoding
- the encoding to use
java.io.IOException
- if there was an error opening the filepublic static java.io.BufferedReader bufferedReader(java.io.InputStream i, java.lang.String encoding) throws java.io.UnsupportedEncodingException
java.io.UnsupportedEncodingException
public static java.io.BufferedInputStream bufferedInputStream(java.io.File file) throws java.io.IOException
file
- the file to open
java.io.IOException
- if there was an error opening the filepublic static java.io.BufferedOutputStream bufferedOutputStream(java.io.File file) throws java.io.IOException
file
- the file to open
java.io.IOException
- if there was an error opening the filepublic static java.io.BufferedOutputStream bufferedOutputStream(java.io.File file, boolean append) throws java.io.IOException
file
- the file to openappend
- true to append to the file, false otherwise.
java.io.IOException
- if there was an error opening the filepublic static java.lang.String readLine(java.io.InputStream in) throws java.io.IOException
in
- the stream to read from
java.io.IOException
- if there was an error in reading from
the streampublic static java.lang.String readLine(java.io.InputStream in, char[] lineBuffer) throws java.io.IOException
in
- the stream to read fromlineBuffer
- a buffer to use for reading the
lines. Performance is better if the buffer is big enough for
the longest line.
java.io.IOException
- if there was an error in reading from
the streampublic static boolean copyFileIfNewer(java.io.File sourceFile, java.io.File targetFile)
sourceFile
- the source of the copytargetFile
- the target of the copy
public static boolean copyFile(java.io.File sourceFile, java.io.File targetFile)
sourceFile
- the sourcetargetFile
- the target
public static boolean copyFile(java.io.File sourceFile, java.io.File targetFile, char[] buffer)
sourceFile
- the source of the copytargetFile
- the target of the copybuffer
- a buffer to use for the copy
public static boolean copyFile(java.io.File sourceFile, java.io.File targetFile, byte[] buffer)
sourceFile
- the source of the copytargetFile
- the target of the copybuffer
- a buffer to use for the copy
public static boolean readerToWriter(java.io.Reader reader, java.io.Writer writer)
reader
- the source of the copywriter
- the destination of the copy, null if none
public static boolean readerToWriter(java.io.Reader reader, java.io.Writer writer, char[] buffer)
reader
- the source of the copywriter
- the destination of the copy, null if nonebuffer
- a buffer to use for the copy
public static long spool(java.io.InputStream input, java.io.OutputStream output, byte[] buffer, boolean flushOutput, long length) throws java.io.IOException
length
bytes from input
to
output
using the supplied buffer
.
The copying stops if input
is exhausted before
length
bytes are copied. The actual number of bytes copied is
returned.
Notes:
input
or
output
.
input
or output
has to be
buffered--this method provides a buffer
input
- the stream to read fromoutput
- the stream to write tobuffer
- the byte buffer to useflushOutput
- if true
flush output
on each writelength
- the maximum number of bytes to copy
java.io.IOException
public static long spool(java.io.InputStream input, java.io.OutputStream output, byte[] buffer, long length) throws java.io.IOException
length
bytes from input
to
output
using the supplied buffer
.
The copying stops if input
is exhausted before
length
bytes are copied. The actual number of bytes copied is
returned.
Notes:
input
or
output
.
input
or output
has to be
buffered--this method provides a buffer
input
- the stream to read fromoutput
- the stream to write tobuffer
- the byte buffer to uselength
- the maximum number of bytes to copy
java.io.IOException
public static long spool(java.io.InputStream input, java.io.OutputStream output, long length) throws java.io.IOException
length
bytes from input
to
output
.
The copying stops if input
is exhausted before
length
bytes are copied. The actual number of bytes copied is
returned.
Notes:
input
or
output
.
input
or output
has to be
buffered--this method provides a buffer
input
- the stream to read fromoutput
- the stream to write tolength
- the maximum number of bytes to copy
java.io.IOException
public static long spool(java.io.InputStream input, java.io.OutputStream output) throws java.io.IOException
input
to output
.
The copying stops when input
is exhausted.
The actual number of bytes copied is returned.
input
- the stream to read fromoutput
- the stream to write to
java.io.IOException
public static int spool(java.io.InputStream input, byte[] buffer) throws java.io.IOException
buffer.length
bytes from input
to
buffer
.
The copying stops if input
is exhausted before buffer
is filled. The actual number of bytes copied is returned.
input
- the stream to read frombuffer
- the byte buffer to write to
java.io.IOException
public static boolean inputStreamToOutputStream(java.io.InputStream input, java.io.OutputStream output)
IOException
see spool(InputStream,OutputStream)
.
input
- the source of the copyoutput
- the destination of the copy, null if none
public static boolean inputStreamToOutputStream(java.io.InputStream input, java.io.OutputStream output, byte[] buffer, boolean flush)
IOException
see the spool
method taking the same arguments.
input
- the source of the copyoutput
- the destination of the copy, null if nonebuffer
- a buffer to use for copyingflush
- if true output is flushed after
every write call
public static int inputStreamToOutputStream(java.io.InputStream input, java.io.OutputStream output, byte[] buffer, boolean flush, int bytesToCopy) throws java.io.IOException
spool(java.io.InputStream, java.io.OutputStream, byte[], boolean, long)
instead.
input
- the source of the copyoutput
- the destination of the copy, null if nonebuffer
- a buffer to use for copyingflush
- if true output is flushed after
every write callbytesToCopy
- the number of bytes to copy
java.io.IOException
public static byte[] bytesFromURL(java.net.URL url)
url
- the URL to read
public static java.lang.String executeURL(java.lang.String url, java.lang.String encoding)
url
- the URL to readencoding
- encoding of the content present at given url
public static java.lang.String executeURL(java.lang.String url)
url
- the URL to read
public static java.lang.String stringFromURL(java.net.URL url, java.lang.String encoding)
url
- the URL to readencoding
- the encoding to use (as in a String constructor).
If encoding is null, we use the default encoding
public static void copyUrlToFile(java.lang.String sourceURLString, java.io.File targetFile) throws java.io.FileNotFoundException, java.net.MalformedURLException, java.io.IOException
sourceURLString
- the URL string reference source contenttargetFile
- the destination for the content
java.io.FileNotFoundException
java.net.MalformedURLException
java.io.IOException
public static boolean copyDirectory(java.io.File source, java.io.File target)
source
- The sourcetarget
- The target
java.lang.NullPointerException
- if one of the arguments is nullpublic static boolean copyDirectory(java.io.File source, java.io.File target, boolean recursive, boolean createTarget)
source
- The source. It has to be a directory that exists.target
- The targetrecursive
- if true, will also copy the subdirectoriescreateTarget
- if true, will create the target and its
subdirectories if needed
java.lang.NullPointerException
- if one the source or the target is nullpublic static boolean deleteDirectory(java.io.File dir)
dir
- Directory to delete. It has to be a directory that exists.
java.lang.NullPointerException
- if the argument is nullpublic static java.lang.String getDefaultSystemEncoding()
public static void close(java.io.Closeable obj)
obj
- Object needs to be closedpublic static boolean grep(java.io.Reader reader, java.lang.String regex) throws java.io.IOException
reader
character stream for regex
returning true
the pattern is found in the character stream
and false
otherwise.
Notes: reader
is read in a line-by-line manner (there is
no support currently for "across-line" matches)
reader
is advanced to the end of the first line that
contains regex
reader
- the character streamregex
- the regular expression
java.io.IOException
public static boolean grep(java.io.File file, java.lang.String regex, java.nio.charset.Charset charset) throws java.io.IOException
true
if the file
contains
a string matching regex
and false
otherwise.
file
- the fileregex
- the regular expressioncharset
- the character set used by file
java.io.IOException
public static boolean grep(java.io.File file, java.lang.String regex) throws java.io.IOException
true
if the file
contains
a string matching regex
and false
otherwise.
file
- the file (UTF-8 encoding assumed)regex
- the regular expression
java.io.IOException
public static void searchReplace(java.io.Reader reader, java.io.Writer writer, java.lang.String regexToFind, java.lang.String toReplace) throws java.io.IOException
reader
character stream and replaces every
occurrence of regexToFind
with the replacement string
toReplace
. Captured groups in regexToFind
can be referenced in toReplace
using the standard
mechanism (e.g. $1
references the first group etc.)
No support currently for across-line regexes.
This method does not close either reader
or
writer
.
java.io.IOException
public static void searchReplace(java.io.File input, java.io.Writer writer, java.lang.String regexToFind, java.lang.String toReplace, java.nio.charset.Charset charset) throws java.io.IOException
file
replacing regexToFind
with toReplace
writing to writer
.
java.io.IOException
public static void searchReplace(java.io.File input, java.io.Writer writer, java.lang.String regexToFind, java.lang.String toReplace) throws java.io.IOException
input
replacing regexToFind
with toReplace
writing to writer
.
java.io.IOException
public static void searchReplace(java.io.File input, java.io.File output, java.lang.String regexToFind, java.lang.String toReplace, java.nio.charset.Charset charset) throws java.io.IOException
input
replacing regexToFind
with toReplace
writing to output
.
java.io.IOException
public static void searchReplace(java.io.File input, java.io.File output, java.lang.String regexToFind, java.lang.String toReplace) throws java.io.IOException
input
replacing regexToFind
with toReplace
writing to output
.
java.io.IOException
public static java.lang.String readFirstLine(java.io.File file, java.nio.charset.Charset charset) throws java.io.IOException
file
using
the specified charset
to convert the bytes from
the file into characters.
file
- the file to read fromcharset
- the encoding to be used when reading the file
java.io.IOException
public static java.util.List<java.lang.String> readLines(java.io.File file, java.nio.charset.Charset charset, int n) throws java.io.IOException
n
lines from file
using
the specified charset
to convert the bytes from
the file into characters. Use the method with care. n
should typically be small as the lines are
in memory
file
- the file to read fromcharset
- the encoding to be used when reading the filen
- number of lines to read
java.io.IOException
public static java.lang.String readFirstLine(java.io.File file) throws java.io.IOException
file
assuming UTF-8.
file
- the file to read from
java.io.IOException
public static java.lang.String readFileContent(java.io.File file, java.nio.charset.Charset charset) throws java.io.IOException
file
using the specified
charset
to convert the bytes from the file into characters.
file
- the file to read fromcharset
- the encoding to be used when reading the file
java.io.IOException
public static java.lang.String readFileContent(java.io.File file) throws java.io.IOException
file
assuming UTF-8.
file
- the file to read from
java.io.IOException
public static void writeToFile(java.io.File file, java.lang.Object object, boolean append, java.nio.charset.Charset charset) throws java.io.IOException
object
to file
using the
specified charset
.
file
- the file to write toobject
- the object to write (toString()
is called)append
- whether to append to the file or to truncate and writecharset
- the encoding to use
java.io.IOException
public static void writeToFile(java.io.File file, java.lang.Object object, boolean append) throws java.io.IOException
object
to file
assuming UTF-8.
file
- the file to write toobject
- the object to write (toString()
is called)append
- whether to append to the file or to truncate and write
java.io.IOException
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |