jlibs.core.io
Class IOUtil

java.lang.Object
  extended by jlibs.core.io.IOUtil

public class IOUtil
extends Object

This class contains various Input/Output related utility methods.


Stardard Charsets:
This class contains constants to stardard charsets that are supported by JVM Spec.
 byte buff[] = ...
 String str = new String(buff, IOUtil.UTF_8);
 
Pumping:
To read a file content as String:
 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;
Let us see how these methods simplify some code;

To copy file:
 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 to ByteArrayOutputStream2
 

Author:
Santhosh Kumar T

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
<T extends OutputStream>
T
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
<T extends Writer>
T
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

US_ASCII

public static final Charset US_ASCII

ISO_8859_1

public static final Charset ISO_8859_1

UTF_8

public static final Charset UTF_8

UTF_16BE

public static final Charset UTF_16BE

UTF_16LE

public static final Charset UTF_16LE

UTF_16

public static final Charset UTF_16

UTF_32BE

public static final Charset UTF_32BE

UTF_32LE

public static final Charset UTF_32LE

UTF_32

public static final Charset UTF_32
Constructor Detail

IOUtil

public IOUtil()
Method Detail

pump

public static ByteArrayOutputStream2 pump(InputStream is,
                                          boolean closeIn)
                                   throws IOException
Reads data from is and writes it into an instanceof ByteArrayOutputStream2.

Parameters:
is - inputstream from which data is read
closeIn - close inputstream or not
Returns:
the instance of ByteArrayOutputStream2 into which data is written
Throws:
IOException - if an I/O error occurs.

pump

public static <T extends OutputStream> T pump(InputStream is,
                                              T os,
                                              boolean closeIn,
                                              boolean closeOut)
                                   throws IOException
Reads data from is and writes it into os.
is and os are closed if closeIn and closeOut are true respectively.

Parameters:
is - inputstream from which data is read
os - outputstream into which data is written
closeIn - close inputstream or not
closeOut - close outputstream or not
Returns:
the argument os
Throws:
IOException - if an I/O error occurs.

pump

public static CharArrayWriter2 pump(Reader reader,
                                    boolean closeReader)
                             throws IOException
Reads data from reader and writes it into an instanceof CharArrayWriter2.

Parameters:
reader - reader from which data is read
closeReader - close reader or not
Returns:
the instance of CharArrayWriter2 into which data is written
Throws:
IOException - if an I/O error occurs.

pump

public static <T extends Writer> T pump(Reader reader,
                                        T writer,
                                        boolean closeReader,
                                        boolean closeWriter)
                             throws IOException
Reads data from reader and writes it into writer.
reader and writer are closed if closeReader and closeWriter are true respectively.

Parameters:
reader - reader from which data is read
writer - writer into which data is written
closeReader - close reader or not
closeWriter - close writer or not
Returns:
the argument writer
Throws:
IOException - if an I/O error occurs.

readFully

public static int readFully(InputStream in,
                            byte[] b)
                     throws IOException
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.

Parameters:
in - input stream from which data is read
b - the buffer into which the data is read.
Returns:
the number of bytes read. if the inputstream doen't have enough bytes available to fill the buffer, it returns the the number of bytes read
Throws:
IOException - if an I/O error occurs.

readFully

public static int readFully(InputStream in,
                            byte[] b,
                            int off,
                            int len)
                     throws IOException
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.

Parameters:
in - input stream from which data is read
b - 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.
Returns:
the number of bytes read. if the inputstream doen't have len bytes available it returns the the number of bytes read
Throws:
IOException - if an I/O error occurs.

readFully

public static int readFully(Reader reader,
                            char[] ch)
                     throws IOException
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.

Parameters:
reader - reader from which data is read
ch - the buffer into which the data is read.
Returns:
the number of chars read. if the reader doen't have enough chars available to fill the buffer, it returns the the number of chars read
Throws:
IOException - if an I/O error occurs.

readFully

public static int readFully(Reader reader,
                            char[] ch,
                            int off,
                            int len)
                     throws IOException
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.

Parameters:
reader - input stream from which data is read
ch - 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.
Returns:
the number of chars read. if the reader doen't have len chars available it returns the the number of chars read
Throws:
IOException - if an I/O error occurs.


Copyright © 2018. All rights reserved.