Source Code : Read byte and string with ByteArrayOutputStream
Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com.
Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com.
Click Here or search from google with Libraries Name you get jar file related it
Read byte and string with ByteArrayOutputStream
import java.io.*;
/**
* @author Jorge Machado
* @date 27/Mai/2008
* @see jomm.utils
*/
public class StreamsUtils
{
public static byte[] readBytes(InputStream stream) throws IOException
{
ByteArrayOutputStream b = new 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 String readString(InputStream stream) throws IOException
{
ByteArrayOutputStream b = new 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(InputStream stream, File f) throws IOException
{
f.getParentFile().mkdirs();
FileOutputStream out = new FileOutputStream(f);
inputStream2OutputStream(stream,out);
}
public static void inputStream2OutputStream(InputStream stream, OutputStream out) throws IOException
{
int readedBytes;
byte[] buf = new byte[1024];
while ((readedBytes = stream.read(buf)) > 0)
{
out.write(buf, 0, readedBytes);
}
stream.close();
out.close();
}
}
Thank with us