|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectde.fu_berlin.ties.io.IOUtils
public final class IOUtils
A static class that provides utility constants and methods for I/O. No instances of this class can be created, only the static members should be used.
Field Summary | |
---|---|
static String |
CONFIG_COMPRESS_GZIP
Configuration key: whether to compress your data in gzip
format. |
static char |
EXT_SEPARATOR
The character separating the base name from the extension of a file (a dot). |
static String |
KEY_LOCAL_CHARSET
Configuration and context key: the character set to use when reading and writing local files. |
static int |
STANDARD_BLOCK_SIZE
The standard block size recommended for I/O. |
static String |
STANDARD_HTTP_CHARSET
The standard character set defined by the HTTP standard. |
static String |
STANDARD_UNICODE_CHARSET
The standard Unicode-compatible character set. |
Method Summary | |
---|---|
static File |
createOutFile(File directory,
String localName)
Creates a file for writing output to. |
static File |
createOutFile(File directory,
String localName,
String outExtension)
Creates a file for writing output to. |
static File |
createOutFile(File directory,
String localName,
String outExtension,
MutableInt last)
Creates a file for writing output to. |
static Charset |
determineCharset(InputStreamReader reader)
Returns the charset used by an InputStreamReader. |
static Charset |
determineCharset(OutputStreamWriter writer)
Returns the charset used by an OutputStreamWriter. |
static String |
determineCharsetName(InputStreamReader reader)
Returns the canoncical name of the charset used by an InputStreamReader. |
static String |
determineCharsetName(OutputStreamWriter writer)
Returns the canoncical name of the charset used by an OutputStreamWriter. |
static File |
determineOutputDirectory(TiesConfiguration config)
Determines the output to directory to use, reading it from the TextProcessor.KEY_OUT_DIRECTORY configuration key in a given
configuration. |
static String |
getBaseName(File file)
Returns the base name of a file (the local name without getExtension(File) and preceding dot ). |
static String |
getBaseName(URL url)
Returns the base name of an URL (the local name without getExtension(File) and preceding dot ). |
static File |
getDefaultDirectory()
Returns an default directory that can be given as "parent" when constructing file objects. |
static String |
getExtension(File file)
Returns the extension of a file. |
static String |
getExtension(URL url)
Returns the file extension of an URL. |
static String |
getLocalName(URL url,
boolean acceptDir)
Returns the local name of an URL. |
static InputStream |
openCompressableInStream(InputStream in)
Opens an input stream that might have been compressed in gzip format. |
static OutputStream |
openCompressableOutStream(OutputStream out,
boolean doCompress)
Transparently opens an input stream that may use compression to store the data (in gzip format). |
static OutputStream |
openCompressableOutStream(OutputStream out,
TiesConfiguration config)
Transparently opens an input stream that may use compression to store the data (in gzip format). |
static InputStreamReader |
openReader(File file,
Configuration config)
Opens an reader on a local file. |
static InputStreamReader |
openReader(File file,
String charset)
Opens an reader on a local file, using a given charset. |
static InputStreamReader |
openUnicodeReader(InputStream in)
Opens a reader on an input stream that uses a Unicode character set (UTF-8, UTF-16, or UTF-32) and optionally a BOM (byte order mark) to identify the used charset. |
static OutputStreamWriter |
openUnicodeWriter(OutputStream out)
Opens a writer that uses the standard Unicode character set UTF-8. |
static Writer |
openWriter(File file,
Configuration config)
Opens an writer on a local file. |
static Writer |
openWriter(File file,
String charset)
Opens an writer on a local file, using a given charset. |
static String |
readToString(Reader reader)
Reads the contents of a reader into a string. |
static int |
readToWriter(Reader reader,
Writer writer)
Reads the contents of a reader into a writer. |
static String[] |
readURIList(CharSequence in)
Converts an URI list as defined in RFC 2483 (MIME type text/uri-list ) into an array of strings. |
static String[] |
readURIList(Reader in)
Converts an URI list as defined in RFC 2483 (MIME type text/uri-list ) into an array of strings. |
static void |
setDefaultDirectory(File directory)
Modifies the default directory that can be given as "parent" when constructing file objects. |
static boolean |
tryToClose(InputStream in)
Convenience method for closing an input stream. |
static boolean |
tryToClose(OutputStream out)
Convenience method for closing an output stream. |
static boolean |
tryToClose(Reader reader)
Convenience method for closing a reader. |
static boolean |
tryToClose(Writer writer)
Convenience method for closing a writer. |
static void |
writeToWriter(CharSequence input,
Writer writer)
Writes the contents of a character sequence to a writer. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final String KEY_LOCAL_CHARSET
public static final String CONFIG_COMPRESS_GZIP
gzip
format. Used by
openCompressableOutStream(OutputStream, TiesConfiguration)
.
public static final int STANDARD_BLOCK_SIZE
public static final String STANDARD_HTTP_CHARSET
public static final String STANDARD_UNICODE_CHARSET
public static final char EXT_SEPARATOR
Method Detail |
---|
public static File createOutFile(File directory, String localName)
createOutFile(File, String, String)
without specifying an
extension.
directory
- the directory in which the file should be created;
if null
or not writable, the file is created in the
current working directory insteadlocalName
- the local name of the input file --- the extension of
this file (if any) is replaced by the specified output extension
public static File createOutFile(File directory, String localName, String outExtension)
File.mkdir()
method).
The name of the file is derivated from the given local name by
replacing the extension of the input file (if any) by the specified
outExtension
. If a file with this name already exists,
"2" or the next available number is inserted before the extension unless
an unused file name is found.
E.g. for input file "test.txt" and output extension "out", the file "test.out" is created. If this file already exists, the file "test2.out" is created instead (or "test3.out" etc. unless an unsed name is found).
directory
- the directory in which the file should be created;
if null
or not writable, the file is created in the
current working directory insteadlocalName
- the local name of the input file --- the extension of
this file (if any) is replaced by the specified output extensionoutExtension
- the extension to append to the output file
(without a starting dot); ignored if empty or null
public static File createOutFile(File directory, String localName, String outExtension, MutableInt last)
File.mkdir()
method).
The name of the file is derivated from the given local name by
replacing the extension of the input file (if any) by the specified
outExtension
. If a file with this name already exists,
"2" or the next available number is inserted before the extension unless
an unused file name is found.
E.g. for input file "test.txt" and output extension "out", the file "test.out" is created. If this file already exists, the file "test2.out" is created instead (or "test3.out" etc. unless an unsed name is found).
directory
- the directory in which the file should be created;
if null
or not writable, the file is created in the
current working directory insteadlocalName
- the local name of the input file --- the extension of
this file (if any) is replaced by the specified output extensionoutExtension
- the extension to append to the output file
(without a starting dot); ignored if empty or null
last
- if not null
, assumped to wrap the last numeric
prefix already in use, i.e. the first tried prefix will the the value
of this + 1; after determining a file name, the value will be set to the
numeric prefix used this time
public static Charset determineCharset(InputStreamReader reader)
reader
- the reader to check
public static String determineCharsetName(InputStreamReader reader)
InputStreamReader.getEncoding()
directly, because that method
often returns a non-standard ("historical") name.
reader
- the reader to check
public static Charset determineCharset(OutputStreamWriter writer)
writer
- the writer to check
public static String determineCharsetName(OutputStreamWriter writer)
OutputStreamWriter.getEncoding()
directly, because that method
often returns a non-standard ("historical") name.
writer
- the writer to check
public static File determineOutputDirectory(TiesConfiguration config)
TextProcessor.KEY_OUT_DIRECTORY
configuration key in a given
configuration.
config
- configuration used to determine the output directory
(read from the TextProcessor.KEY_OUT_DIRECTORY
key)
null
if none
is configuredpublic static String getBaseName(File file)
getExtension(File)
and preceding dot
).
file
- the file to check
public static String getBaseName(URL url)
getExtension(File)
and preceding dot
).
url
- the URL to check
public static File getDefaultDirectory()
null
so the "system-dependent
default directory" (typically the working directory) will be used.
public static String getExtension(File file)
file
- the file to check
public static String getExtension(URL url)
url
- the URL to check
public static String getLocalName(URL url, boolean acceptDir)
For URLs that are recognizable as directories (i.e. end in a slash),
the name of the final directory is returned if acceptDir
is true
-- otherwise the empty string is returned.
url
- the URL to checkacceptDir
- whether to return a final directory name
(see description)
public static InputStream openCompressableInStream(InputStream in) throws IOException
gzip
format. This method autodetects whether the stream
has been compressed and returns a stream that allows accessing the
uncompressed data.
in
- the raw input stream (either uncompressed or in
gzip
format
IOException
- if an I/O error has occurredpublic static OutputStream openCompressableOutStream(OutputStream out, boolean doCompress) throws IOException
gzip
format).
out
- the original output streamdoCompress
- whether or not to use compression
GZIPOutputStream
wrapping out
if
doCompress
is true
; otherwise it will be the
raw out
stream
IOException
- if an I/O error has occurredpublic static OutputStream openCompressableOutStream(OutputStream out, TiesConfiguration config) throws IOException
gzip
format). Checks the
CONFIG_COMPRESS_GZIP
parameter to decide whether or not to
use compression.
out
- the original output streamconfig
- the configuration to use
IOException
- if an I/O error has occurredpublic static InputStreamReader openReader(File file, Configuration config) throws IOException
configured character set
-- if not specified, the default charset of
the current platform is used instead. Compressed files are automatically
decompressed (using the
openCompressableInStream(InputStream)
method). Don't forget to
finally close any reader you open!
file
- the file to readconfig
- the configuration to use
IOException
- if an I/O error has occurredpublic static InputStreamReader openReader(File file, String charset) throws IOException
openCompressableInStream(InputStream)
method). Don't forget to
finally close any reader you open!
file
- the file to readcharset
- the character set to use for reading the file;
if null
, the default charset of the current platform is used
IOException
- if an I/O error has occurredpublic static InputStreamReader openUnicodeReader(InputStream in) throws IOException
Adapted from the
UnicodeReader
class created by Thomas Weidenfeller and
Aki Nieminen.
in
- the input stream to wrap
IOException
- if an I/O error has occurredpublic static OutputStreamWriter openUnicodeWriter(OutputStream out)
out
- the output stream to wrap
public static Writer openWriter(File file, Configuration config) throws IOException, UnsupportedEncodingException
configured character set
-- if not specified, the default charset of
the current platform is used instead. Don't forget to finally
close any writer you open!
file
- the file to write toconfig
- the configuration to use
IOException
- if the file is a directory or for some
other reason cannot be opened for writing
UnsupportedEncodingException
- if the named charset is not
supportedpublic static Writer openWriter(File file, String charset) throws IOException, UnsupportedEncodingException
file
- the file to write tocharset
- the character set to use for writing the file;
if null
, the default charset of the current platform is used
IOException
- if the file is a directory or for some
other reason cannot be opened for writing
UnsupportedEncodingException
- if the named charset is not
supportedpublic static String readToString(Reader reader) throws IOException
reader
- the reader to use
IOException
- if an I/O error occurspublic static int readToWriter(Reader reader, Writer writer) throws IOException
reader
- the reader to read fromwriter
- the writer to write to write to
IOException
- if an I/O error occurspublic static String[] readURIList(CharSequence in)
text/uri-list
) into an array of strings. Comment lines
in the input are ignored.
in
- the URI list to convert
public static String[] readURIList(Reader in) throws IOException
text/uri-list
) into an array of strings. Comment lines
in the input are ignored.
in
- a reader containing the URI list to convert
IOException
- if an I/O error occurspublic static void setDefaultDirectory(File directory) throws IllegalArgumentException
null
so the "system-dependent
default directory" (typically the working directory) will be used.
directory
- the new directory to use
IllegalArgumentException
- if the given directory is neither
null
nor an existing directorypublic static boolean tryToClose(InputStream in)
null
, this method does nothing. Any IOException
thrown during closing is swallowed by this method.
in
- the input stream to close (might be null
)
true
if the stream was closed successfully,
false
otherwise (the stream was null
or an
exception was thrown during closing)public static boolean tryToClose(OutputStream out)
null
, this method does nothing. Any IOException
thrown during closing is swallowed by this method.
out
- the output stream to close (might be null
)
true
if the stream was closed successfully,
false
otherwise (the stream was null
or an
exception was thrown during closing)public static boolean tryToClose(Reader reader)
null
, this method does nothing. Any IOException
thrown during closing is swallowed by this method.
reader
- the reader to close (might be null
)
true
if the reader was closed successfully,
false
otherwise (reader was null
or an
exception was thrown during closing)public static boolean tryToClose(Writer writer)
null
, this method does nothing. Any IOException
thrown during closing is swallowed by this method.
writer
- the writer to close (might be null
)
true
if the writer was closed successfully,
false
otherwise (writer was null
or an
exception was thrown during closing)public static void writeToWriter(CharSequence input, Writer writer) throws IOException
input
- the text to send to the wrierwriter
- the writer to write to write to
IOException
- if an I/O error occurs
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |