|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjlibs.core.io.IOUtil
public class IOUtil
This class contains various Input/Output related utility methods.
byte buff[] = ...
String str = new String(buff, IOUtil.UTF_8);
Pumping:
CharArrayWriter cout = new CharArrayWriter();
IOUtil.pump(new FileReader(file), cout, true, true);
String content = cout.toString();
To simplify code, pump(...) method returns output; So the above code could be written in single line as follows:
String content = IOUtil.pump(new FileReader(file), new CharArrayWriter(), true, true).toString();if output is not specified, it defaults to
CharArrayWriter2. so the same code can be written as:
String content = IOUtil.pump(new FileReader(file), true).toString();
Similar versions of pump(...) methods are available for byte-streams also;
IOUtil.pump(new FileInputStream(fromFile), new FileOutputStream(toFile), true, true);
To create zip file:
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
for(File file: files){
zipOut.putNextEntry(new ZipEntry(file.getName());
IOUtil.pump(new FileInputStream(file), zipOut, true, false); // note that last arg is false
zipOut.closeEntry();
}
zipOut.close();
To create file with given string:
String content = ...
IOUtil.pump(new StringReader(content), new FileWriter(file), true, true);
to read a file content into byte array:
byte bytes[] = IOUtil.pump(new FileInputStream(file), true).toByteArray(); // output defaults toByteArrayOutputStream2
| Field Summary | |
|---|---|
static Charset |
ISO_8859_1
|
static Charset |
US_ASCII
|
static Charset |
UTF_16
|
static Charset |
UTF_16BE
|
static Charset |
UTF_16LE
|
static Charset |
UTF_32
|
static Charset |
UTF_32BE
|
static Charset |
UTF_32LE
|
static Charset |
UTF_8
|
| Constructor Summary | |
|---|---|
IOUtil()
|
|
| Method Summary | ||
|---|---|---|
static ByteArrayOutputStream2 |
pump(InputStream is,
boolean closeIn)
Reads data from is and writes it into an instanceof ByteArrayOutputStream2. |
|
static
|
pump(InputStream is,
T os,
boolean closeIn,
boolean closeOut)
Reads data from is and writes it into os.is and os are closed if closeIn and closeOut
are true respectively. |
|
static CharArrayWriter2 |
pump(Reader reader,
boolean closeReader)
Reads data from reader and writes it into an instanceof CharArrayWriter2. |
|
static
|
pump(Reader reader,
T writer,
boolean closeReader,
boolean closeWriter)
Reads data from reader and writes it into writer.reader and writer are closed if closeReader and closeWriter
are true respectively. |
|
static int |
readFully(InputStream in,
byte[] b)
Reads data from given inputstream into specified buffer. If the given inputstream doesn't have number of bytes equal to the length of the buffer available, it simply reads only the available number of bytes. |
|
static int |
readFully(InputStream in,
byte[] b,
int off,
int len)
Reads len bytes from given input stream into specified buffer.If the given inputstream doesn't have len bytes available,
it simply reads only the availabel number of bytes. |
|
static int |
readFully(Reader reader,
char[] ch)
Reads data from given reader into specified buffer. If the given reader doesn't have number of chars equal to the length of the buffer available, it simply reads only the available number of chars. |
|
static int |
readFully(Reader reader,
char[] ch,
int off,
int len)
Reads len chars from given reader into specified buffer.If the given reader doesn't have len chars available,
it simply reads only the availabel number of chars. |
|
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static final Charset US_ASCII
public static final Charset ISO_8859_1
public static final Charset UTF_8
public static final Charset UTF_16BE
public static final Charset UTF_16LE
public static final Charset UTF_16
public static final Charset UTF_32BE
public static final Charset UTF_32LE
public static final Charset UTF_32
| Constructor Detail |
|---|
public IOUtil()
| Method Detail |
|---|
public static ByteArrayOutputStream2 pump(InputStream is,
boolean closeIn)
throws IOException
is and writes it into an instanceof ByteArrayOutputStream2.
is - inputstream from which data is readcloseIn - close inputstream or not
ByteArrayOutputStream2 into which data is written
IOException - if an I/O error occurs.
public static <T extends OutputStream> T pump(InputStream is,
T os,
boolean closeIn,
boolean closeOut)
throws IOException
is and writes it into os.is and os are closed if closeIn and closeOut
are true respectively.
is - inputstream from which data is reados - outputstream into which data is writtencloseIn - close inputstream or notcloseOut - close outputstream or not
os
- Throws:
IOException - if an I/O error occurs.
public static CharArrayWriter2 pump(Reader reader,
boolean closeReader)
throws IOException
reader and writes it into an instanceof CharArrayWriter2.
reader - reader from which data is readcloseReader - close reader or not
CharArrayWriter2 into which data is written
IOException - if an I/O error occurs.
public static <T extends Writer> T pump(Reader reader,
T writer,
boolean closeReader,
boolean closeWriter)
throws IOException
reader and writes it into writer.reader and writer are closed if closeReader and closeWriter
are true respectively.
reader - reader from which data is readwriter - writer into which data is writtencloseReader - close reader or notcloseWriter - close writer or not
writer
- Throws:
IOException - if an I/O error occurs.
public static int readFully(InputStream in,
byte[] b)
throws IOException
in - input stream from which data is readb - the buffer into which the data is read.
IOException - if an I/O error occurs.
public static int readFully(InputStream in,
byte[] b,
int off,
int len)
throws IOException
len bytes from given input stream into specified buffer.len bytes available,
it simply reads only the availabel number of bytes.
in - input stream from which data is readb - the buffer into which the data is read.off - an int specifying the offset into the data.len - an int specifying the number of bytes to read.
len bytes available
it returns the the number of bytes read
IOException - if an I/O error occurs.
public static int readFully(Reader reader,
char[] ch)
throws IOException
reader - reader from which data is readch - the buffer into which the data is read.
IOException - if an I/O error occurs.
public static int readFully(Reader reader,
char[] ch,
int off,
int len)
throws IOException
len chars from given reader into specified buffer.len chars available,
it simply reads only the availabel number of chars.
reader - input stream from which data is readch - the buffer into which the data is read.off - an int specifying the offset into the data.len - an int specifying the number of chars to read.
len chars available
it returns the the number of chars read
IOException - if an I/O error occurs.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||