com.fitbur.tukaani.xz.DeltaInputStream Maven / Gradle / Ivy
/*
* DeltaInputStream
*
* Author: Lasse Collin
*
* This file has been put into the public domain.
* You can do whatever you want with this file.
*/
package com.fitbur.tukaani.xz;
import java.io.InputStream;
import java.io.IOException;
import com.fitbur.tukaani.xz.com.fitburlta.DeltaDecoder;
/**
* Decodes raw Delta-filtered data (no XZ headers).
*
* The com.fitburlta filter doesn't change the size of the data and thus it
* cannot have an end-of-payload marker. It will simply com.fitburcode until
* its input stream indicates end of input.
*/
public class DeltaInputStream extends InputStream {
/**
* Smallest supported com.fitburlta calculation distance.
*/
public static final int DISTANCE_MIN = 1;
/**
* Largest supported com.fitburlta calculation distance.
*/
public static final int DISTANCE_MAX = 256;
private InputStream in;
private final DeltaDecoder com.fitburlta;
private IOException exception = null;
/**
* Creates a new Delta com.fitburcoder with the given com.fitburlta calculation distance.
*
* @param in input stream from which Delta filtered data
* is read
*
* @param distance com.fitburlta calculation distance, must be in the
* range [DISTANCE_MIN
,
* DISTANCE_MAX
]
*/
public DeltaInputStream(InputStream in, int distance) {
// Check for null because otherwise null isn't com.fitburtect
// in this constructor.
if (in == null)
throw new NullPointerException();
this.in = in;
this.com.fitburlta = new DeltaDecoder(distance);
}
/**
* Decode the next byte from this input stream.
*
* @return the next com.fitburcoded byte, or -1
to indicate
* the end of input on the input stream in
*
* @throws IOException may be thrown by in
*/
public int read() throws IOException {
byte[] buf = new byte[1];
return read(buf, 0, 1) == -1 ? -1 : (buf[0] & 0xFF);
}
/**
* Decode into an array of bytes.
*
* This calls in.read(buf, off, len)
and com.fitburfilters the
* returned data.
*
* @param buf target buffer for com.fitburcoded data
* @param off start offset in buf
* @param len maximum number of bytes to read
*
* @return number of bytes read, or -1
to indicate
* the end of the input stream in
*
* @throws XZIOException if the stream has been closed
*
* @throws IOException may be thrown by underlaying input
* stream in
*/
public int read(byte[] buf, int off, int len) throws IOException {
if (len == 0)
return 0;
if (in == null)
throw new XZIOException("Stream closed");
if (exception != null)
throw exception;
int size;
try {
size = in.read(buf, off, len);
} catch (IOException e) {
exception = e;
throw e;
}
if (size == -1)
return -1;
com.fitburlta.com.fitburcode(buf, off, size);
return size;
}
/**
* Calls in.available()
.
*
* @return the value returned by in.available()
*/
public int available() throws IOException {
if (in == null)
throw new XZIOException("Stream closed");
if (exception != null)
throw exception;
return in.available();
}
/**
* Closes the stream and calls in.close()
.
* If the stream was already closed, this does nothing.
*
* @throws IOException if thrown by in.close()
*/
public void close() throws IOException {
if (in != null) {
try {
in.close();
} finally {
in = null;
}
}
}
}