Subversion Repositories bacoAlunos

Rev

Rev 297 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package jomm.utils;

import java.io.*;

/**
 * @author Jorge Machado
 * @date 27/Mai/2008
 * @see jomm.utils
 */

public class StreamsUtils
{
    public static byte[] readBytes(5+0%2Fdocs%2Fapi+InputStream">InputStream stream) throws 1.5.0/docs/api/java/io/IOException.html">IOException
    {
        1.5.0/docs/api/java/io/ByteArrayOutputStream.html">ByteArrayOutputStream b = new 1.5.0/docs/api/java/io/ByteArrayOutputStream.html">ByteArrayOutputStream();
        int readedBytes;
        byte[] buf = new byte[1024];
        while ((readedBytes = stream.read(buf)) > 0)
        {
            b.write(buf, 0, readedBytes);
        }
        b.close();
        return b.toByteArray();
    }

    public static 1.5.0/docs/api/java/lang/String.html">String readString(5+0%2Fdocs%2Fapi+InputStream">InputStream stream) throws 1.5.0/docs/api/java/io/IOException.html">IOException
    {
        1.5.0/docs/api/java/io/ByteArrayOutputStream.html">ByteArrayOutputStream b = new 1.5.0/docs/api/java/io/ByteArrayOutputStream.html">ByteArrayOutputStream();
        int readedBytes;
        byte[] buf = new byte[1024];
        while ((readedBytes = stream.read(buf)) > 0)
        {
            b.write(buf, 0, readedBytes);
        }
        b.close();
        return b.toString();
    }

    public static void inputStream2File(5+0%2Fdocs%2Fapi+InputStream">InputStream stream, 1.5.0/docs/api/java/io/File.html">File f) throws 1.5.0/docs/api/java/io/IOException.html">IOException
    {
        f.getParentFile().mkdirs();
        1.5.0/docs/api/java/io/FileOutputStream.html">FileOutputStream out = new 1.5.0/docs/api/java/io/FileOutputStream.html">FileOutputStream(f);
        inputStream2OutputStream(stream,out);
    }

    public static void inputStream2OutputStream(5+0%2Fdocs%2Fapi+InputStream">InputStream stream, 5+0%2Fdocs%2Fapi+OutputStream">OutputStream out) throws 1.5.0/docs/api/java/io/IOException.html">IOException
    {
        int readedBytes;
        byte[] buf = new byte[1024];
        while ((readedBytes = stream.read(buf)) > 0)
        {
            out.write(buf, 0, readedBytes);
        }
        stream.close();
        out.close();
    }
}