Popular Posts
Read exif using metadata extraction metadata extraction version : 2.3.1 metadata extraction import java.io.File; import java.io.FileNotFoundException; import java.util.Iterato... Chrome Extension: Easy Cookie A simple cookie viewer/editor : Easy Cookie It's easy to view cookies at a web site Simply add a new cookie ... Capture response output stream using HttpModule using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Web; namespace TestWebA...
Stats
StreamReader
package bruce.lib.io;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author Bruce Tsai
 * 
 */
public class StreamReader {

    private InputStream stream;
    public String encoding;

    /**
     * @param stream
     */
    public StreamReader(InputStream stream) {
        this.stream = stream;
    }

    /**
     * @param stream
     * @param encoding
     */
    public StreamReader(InputStream stream, String encoding) {
        this.stream = stream;
        this.encoding = encoding;
    }

    /**
     * @return
     * @throws IOException
     */
    public String readAllText() throws IOException {
        return this.encoding == null || this.encoding.length() == 0 ? new String(readAllBytes()) : new String(readAllBytes(), this.encoding);
    }

    /**
     * @return
     * @throws IOException
     */
    public byte[] readAllBytes() throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[128];
        int readed = -1;
        while ((readed = this.stream.read(buffer)) > -1) {
            baos.write(buffer, 0, readed);
        }
        baos.close();
        this.stream.close();
        return baos.toByteArray();
    }
}