
net.jmatrix.utils.StreamUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jmutils Show documentation
Show all versions of jmutils Show documentation
PerfTrack and Async utilities.
package net.jmatrix.utils;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
/**
*
*/
public final class StreamUtil
{
static final int EOS=-1;
/**
* Pumps one stream to another, with a buffer - both streams are
* closed when the pump is complete.
*/
public static final int pump(InputStream is, OutputStream os)
throws IOException {
int totalBytes;
try
{
byte buffer[]=new byte[8192];
int bytes=is.read(buffer);
totalBytes = 0;
while (bytes > 0) {
totalBytes+=bytes;
os.write(buffer, 0, bytes);
// System.out.println ("last read: "+bytes+" reading...");
// SOLVED! -- See jet_net ChunkedInputStream, and
// Transfer-Encoding: chunked. !!
// pab, 24/7/2003
bytes=is.read(buffer);
}
}
finally
{
if (os != null)
{
os.flush();
os.close();
}
if (is != null)
{
is.close();
}
}
return totalBytes;
}
/**
* Pumps one stream to another, with a buffer - both streams are
* closed when the pump is complete.
*/
public static final int pump(Reader is, Writer os)
throws IOException {
char buffer[]=new char[8192];
int bytes=is.read(buffer);
int totalBytes=0;
try
{
while (bytes > 0) {
totalBytes+=bytes;
os.write(buffer, 0, bytes);
// System.out.println ("last read: "+bytes+" reading...");
// SOLVED! -- See jet_net ChunkedInputStream, and
// Transfer-Encoding: chunked. !!
// pab, 24/7/2003
bytes=is.read(buffer);
}
}
finally
{
if (os != null)
{
os.flush();
os.close();
}
if (is != null)
{
is.close();
}
}
return totalBytes;
}
public static final void unbufferedPump(InputStream is, OutputStream os)
throws IOException {
try
{
int b=is.read();
while (b != EOS) {
os.write(b);
b=is.read();
}
}
finally
{
if (os != null)
{
os.flush();
os.close();
}
if (is != null)
{
is.close();
}
}
}
/** */
public static final void pumpExactly(InputStream is, OutputStream os,
int bytes)
throws IOException {
try
{
for (int i=0; i
© 2015 - 2025 Weber Informatics LLC | Privacy Policy