|
|||||||||
| 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 BufferedInputStream |
bufferedInputStream(File file)
Opens a binary file on top of a buffered layer. |
static BufferedOutputStream |
bufferedOutputStream(File file)
Opens a binary file on top of a buffered layer. |
static BufferedOutputStream |
bufferedOutputStream(File file,
boolean append)
Opens a binary file on top of a buffered layer. |
static BufferedReader |
bufferedReader(File file,
String encoding)
Opens a text file on top of a buffered layer. |
static BufferedReader |
bufferedReader(InputStream i,
String encoding)
Opens a text stream over a binary one on top of a buffered layer. |
static BufferedWriter |
bufferedWriter(OutputStream o,
String encoding)
Open a writer with the specified encoding |
static byte[] |
bytesFromURL(URL url)
Read the contents of a URL as a byte array . |
static void |
close(InputStream in)
Helper method to close an input stream without exceptions in a null safe manner. |
static void |
close(OutputStream out)
Helper method to close an output stream without exceptions in a null safe manner. |
static void |
close(Reader in)
Helper method to close a Reader without exceptions in a null safe manner. |
static void |
close(Writer out)
Helper method to close a Writer without exceptions in a null safe manner. |
static boolean |
copyDirectory(File source,
File target)
Copy recursively a directory Use this with caution. |
static boolean |
copyDirectory(File source,
File target,
boolean recursive,
boolean createTarget)
Copy recursively a directory Use this with caution. |
static boolean |
copyFile(File sourceFile,
File targetFile)
Copy the contents of a file to another file. |
static boolean |
copyFile(File sourceFile,
File targetFile,
byte[] buffer)
Copy the contents of a file to another file. |
static boolean |
copyFile(File sourceFile,
File targetFile,
char[] buffer)
Deprecated. use copyFile(File, File, byte[]) |
static boolean |
copyFileIfNewer(File sourceFile,
File targetFile)
Copy a file if the source is newer than the target |
static void |
copyUrlToFile(String sourceURLString,
File targetFile)
Copy a URL (String format) to File. |
static boolean |
deleteDirectory(File dir)
Delete a directory and its subdirectories. |
static String |
getDefaultSystemEncoding()
Return the System Wide default encoding. |
static int |
getInternalBufferHolderRecursionDepth()
Returns the recursion depth of the internal per-thread BufferHolder. |
static boolean |
grep(File file,
String regex)
Returns true if the file contains
a string matching regex and false
otherwise. |
static boolean |
grep(File file,
String regex,
Charset charset)
Returns true if the file contains
a string matching regex and false
otherwise. |
static boolean |
grep(Reader reader,
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(InputStream input,
OutputStream output)
Copies all data from an InputStream to an OutputStream. |
static boolean |
inputStreamToOutputStream(InputStream input,
OutputStream output,
byte[] buffer,
boolean flush)
Copies all data from an InputStream to an OutputStream. |
static int |
inputStreamToOutputStream(InputStream input,
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 PrintWriter |
printWriter(File file,
String encoding)
Opens a text file with a reasonable stream interface on top of a buffered layer. |
static PrintWriter |
printWriter(OutputStream o,
String encoding)
Open a text stream over a binary one on top of a buffered layer. |
static PrintWriter |
printWriter(OutputStream o,
String encoding,
boolean autoFlush)
Open a text stream over a binary one on top of a buffered layer. |
static boolean |
readerToWriter(Reader reader,
Writer writer)
Copy Reader to Writer. |
static boolean |
readerToWriter(Reader reader,
Writer writer,
char[] buffer)
Copy Reader to Writer. |
static String |
readFirstLine(File file)
Reads the first line from file assuming UTF-8. |
static String |
readFirstLine(File file,
Charset charset)
Reads and returns the first line from file using
the specified charset to convert the bytes from
the file into characters. |
static String |
readLine(InputStream in)
Read a line from an InputStream. |
static String |
readLine(InputStream in,
char[] lineBuffer)
Read a line from an InputStream. |
static void |
searchReplace(File input,
File output,
String regexToFind,
String toReplace)
Searches and replaces input replacing regexToFind
with toReplace writing to output. |
static void |
searchReplace(File input,
File output,
String regexToFind,
String toReplace,
Charset charset)
Searches and replaces input replacing regexToFind
with toReplace writing to output. |
static void |
searchReplace(File input,
Writer writer,
String regexToFind,
String toReplace)
Searches and replaces input replacing regexToFind
with toReplace writing to writer. |
static void |
searchReplace(File input,
Writer writer,
String regexToFind,
String toReplace,
Charset charset)
Searches and replaces file replacing regexToFind
with toReplace writing to writer. |
static void |
searchReplace(Reader reader,
Writer writer,
String regexToFind,
String toReplace)
Searches the reader character stream and replaces every
occurrence of regexToFind with the replacement string
toReplace. |
static int |
spool(InputStream input,
byte[] buffer)
Copies up to buffer.length bytes from input to
buffer. |
static long |
spool(InputStream input,
OutputStream output)
Copies all the bytes from input to output. |
static long |
spool(InputStream input,
OutputStream output,
byte[] buffer,
boolean flushOutput,
long length)
Copies up to length bytes from input to
output using the supplied buffer. |
static long |
spool(InputStream input,
OutputStream output,
byte[] buffer,
long length)
Copies up to length bytes from input to
output using the supplied buffer. |
static long |
spool(InputStream input,
OutputStream output,
long length)
Copies up to length bytes from input to
output. |
static String |
stringFromURL(URL url,
String encoding)
Read the contents of a URL as a string. |
static void |
writeToFile(File file,
Object object,
boolean append)
Writes object to file assuming UTF-8. |
static void |
writeToFile(File file,
Object object,
boolean append,
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 PrintWriter printWriter(File file,
String encoding)
throws IOException
file - the file to openencoding - the encoding to use
IOException - if there was an error opening the file
public static PrintWriter printWriter(OutputStream o,
String encoding)
throws UnsupportedEncodingException
UnsupportedEncodingException
public static PrintWriter printWriter(OutputStream o,
String encoding,
boolean autoFlush)
throws UnsupportedEncodingException
UnsupportedEncodingException
public static BufferedWriter bufferedWriter(OutputStream o,
String encoding)
throws UnsupportedEncodingException
UnsupportedEncodingException
public static BufferedReader bufferedReader(File file,
String encoding)
throws IOException
file - the File to openedencoding - the encoding to use
IOException - if there was an error opening the file
public static BufferedReader bufferedReader(InputStream i,
String encoding)
throws UnsupportedEncodingException
UnsupportedEncodingException
public static BufferedInputStream bufferedInputStream(File file)
throws IOException
file - the file to open
IOException - if there was an error opening the file
public static BufferedOutputStream bufferedOutputStream(File file)
throws IOException
file - the file to open
IOException - if there was an error opening the file
public static BufferedOutputStream bufferedOutputStream(File file,
boolean append)
throws IOException
file - the file to openappend - true to append to the file, false otherwise.
IOException - if there was an error opening the file
public static String readLine(InputStream in)
throws IOException
in - the stream to read from
IOException - if there was an error in reading from
the stream
public static String readLine(InputStream in,
char[] lineBuffer)
throws 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.
IOException - if there was an error in reading from
the stream
public static boolean copyFileIfNewer(File sourceFile,
File targetFile)
sourceFile - the source of the copytargetFile - the target of the copy
public static boolean copyFile(File sourceFile,
File targetFile)
sourceFile - the sourcetargetFile - the target
public static boolean copyFile(File sourceFile,
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(File sourceFile,
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(Reader reader,
Writer writer)
reader - the source of the copywriter - the destination of the copy, null if none
public static boolean readerToWriter(Reader reader,
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(InputStream input,
OutputStream output,
byte[] buffer,
boolean flushOutput,
long length)
throws 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
IOException
public static long spool(InputStream input,
OutputStream output,
byte[] buffer,
long length)
throws 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
IOException
public static long spool(InputStream input,
OutputStream output,
long length)
throws 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
IOException
public static long spool(InputStream input,
OutputStream output)
throws 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
IOException
public static int spool(InputStream input,
byte[] buffer)
throws 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
IOException
public static boolean inputStreamToOutputStream(InputStream input,
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(InputStream input,
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(InputStream input,
OutputStream output,
byte[] buffer,
boolean flush,
int bytesToCopy)
throws 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
IOExceptionpublic static byte[] bytesFromURL(URL url)
url - the URL to read
public static String stringFromURL(URL url,
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(String sourceURLString,
File targetFile)
throws FileNotFoundException,
MalformedURLException,
IOException
sourceURLString - the URL string reference source contenttargetFile - the destination for the content
FileNotFoundException
MalformedURLException
IOException
public static boolean copyDirectory(File source,
File target)
source - The sourcetarget - The target
NullPointerException - if one of the arguments is null
public static boolean copyDirectory(File source,
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
NullPointerException - if one the source or the target is nullpublic static boolean deleteDirectory(File dir)
dir - Directory to delete. It has to be a directory that exists.
NullPointerException - if the argument is nullpublic static String getDefaultSystemEncoding()
public static final void close(InputStream in)
in - the InputStream to close.public static final void close(OutputStream out)
out - the OutputStream to close.public static final void close(Reader in)
in - the InputStream to close.public static final void close(Writer out)
out - the OutputStream to close.
public static boolean grep(Reader reader,
String regex)
throws 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
IOException
public static boolean grep(File file,
String regex,
Charset charset)
throws 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
IOException
public static boolean grep(File file,
String regex)
throws IOException
true if the file contains
a string matching regex and false
otherwise.
file - the file (UTF-8 encoding assumed)regex - the regular expression
IOException
public static void searchReplace(Reader reader,
Writer writer,
String regexToFind,
String toReplace)
throws 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.
IOException
public static void searchReplace(File input,
Writer writer,
String regexToFind,
String toReplace,
Charset charset)
throws IOException
file replacing regexToFind
with toReplace writing to writer.
IOException
public static void searchReplace(File input,
Writer writer,
String regexToFind,
String toReplace)
throws IOException
input replacing regexToFind
with toReplace writing to writer.
IOException
public static void searchReplace(File input,
File output,
String regexToFind,
String toReplace,
Charset charset)
throws IOException
input replacing regexToFind
with toReplace writing to output.
IOException
public static void searchReplace(File input,
File output,
String regexToFind,
String toReplace)
throws IOException
input replacing regexToFind
with toReplace writing to output.
IOException
public static String readFirstLine(File file,
Charset charset)
throws 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
IOException
public static String readFirstLine(File file)
throws IOException
file assuming UTF-8.
file - the file to read from
IOException
public static void writeToFile(File file,
Object object,
boolean append,
Charset charset)
throws 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
IOException
public static void writeToFile(File file,
Object object,
boolean append)
throws 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
IOException
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||