下面列出了java.util.zip.DataFormatException#getMessage ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public byte[] unwrap(byte[] bytes) {
inflater.setInput(bytes);
try {
int len;
byte[] buffer = new byte[bytes.length];
while (!inflater.finished()) {
len = inflater.inflate(buffer, 0, buffer.length);
if (len > 0)
unwrapBuffer.write(buffer, 0, len);
}
return unwrapBuffer.toByteArray();
} catch (DataFormatException e) {
throw new RuntimeException("unknown format: " + e.getMessage());
} finally {
inflater.reset();
unwrapBuffer.reset();
}
}
@Override
public byte[] unwrap(byte[] bytes) {
inflater.setInput(bytes);
try {
int len;
byte[] buffer = new byte[bytes.length];
while (!inflater.finished()) {
len = inflater.inflate(buffer, 0, buffer.length);
if (len > 0)
unwrapBuffer.write(buffer, 0, len);
}
return unwrapBuffer.toByteArray();
} catch (DataFormatException e) {
throw new RuntimeException("unknown format: " + e.getMessage());
} finally {
inflater.reset();
unwrapBuffer.reset();
}
}
public List<String> readNameValueBlock(int length) throws IOException {
this.compressedLimit += length;
try {
int numberOfPairs = nameValueBlockIn.readInt();
if (numberOfPairs < 0) {
throw new IOException("numberOfPairs < 0: " + numberOfPairs);
}
if (numberOfPairs > 1024) {
throw new IOException("numberOfPairs > 1024: " + numberOfPairs);
}
List<String> entries = new ArrayList<String>(numberOfPairs * 2);
for (int i = 0; i < numberOfPairs; i++) {
String name = readString();
String values = readString();
if (name.length() == 0) throw new IOException("name.length == 0");
entries.add(name);
entries.add(values);
}
doneReading();
return entries;
} catch (DataFormatException e) {
throw new IOException(e.getMessage());
}
}
public List<String> readNameValueBlock(int length) throws IOException {
this.compressedLimit += length;
try {
int numberOfPairs = nameValueBlockIn.readInt();
if (numberOfPairs < 0) {
throw new IOException("numberOfPairs < 0: " + numberOfPairs);
}
if (numberOfPairs > 1024) {
throw new IOException("numberOfPairs > 1024: " + numberOfPairs);
}
List<String> entries = new ArrayList<String>(numberOfPairs * 2);
for (int i = 0; i < numberOfPairs; i++) {
String name = readString();
String values = readString();
if (name.length() == 0) throw new IOException("name.length == 0");
entries.add(name);
entries.add(values);
}
doneReading();
return entries;
} catch (DataFormatException e) {
throw new IOException(e.getMessage());
}
}
/**
* reads an archived string
*
* @return string
* @throws java.io.IOException
* @throws java.util.zip.DataFormatException
*/
public int readString(byte[] tmp, byte[] target) throws IOException {
int size = readInt();
if (size > 0) {
if (size > target.length)
throw new IOException("Unreasonable string length: " + size);
return in.read(target, 0, size);
} else {
size = -size;
if (size > tmp.length)
throw new IOException("Unreasonable string length: " + size);
int got = in.read(tmp, 0, size);
if (got != size)
throw new IOException("Bytes read: " + got + ", expected: " + size);
try {
return compressor.inflateByteArray(size, tmp, target);
} catch (DataFormatException e) {
throw new IOException(e.getMessage());
}
}
}
public List<String> readNameValueBlock(int length) throws IOException {
this.compressedLimit += length;
try {
int numberOfPairs = nameValueBlockIn.readInt();
if (numberOfPairs < 0) {
throw new IOException("numberOfPairs < 0: " + numberOfPairs);
}
if (numberOfPairs > 1024) {
throw new IOException("numberOfPairs > 1024: " + numberOfPairs);
}
List<String> entries = new ArrayList<String>(numberOfPairs * 2);
for (int i = 0; i < numberOfPairs; i++) {
String name = readString();
String values = readString();
if (name.length() == 0) throw new IOException("name.length == 0");
entries.add(name);
entries.add(values);
}
doneReading();
return entries;
} catch (DataFormatException e) {
throw new IOException(e.getMessage());
}
}
public List<String> readNameValueBlock(int length) throws IOException {
this.compressedLimit += length;
try {
int numberOfPairs = nameValueBlockIn.readInt();
if (numberOfPairs < 0) {
throw new IOException("numberOfPairs < 0: " + numberOfPairs);
}
if (numberOfPairs > 1024) {
throw new IOException("numberOfPairs > 1024: " + numberOfPairs);
}
List<String> entries = new ArrayList<String>(numberOfPairs * 2);
for (int i = 0; i < numberOfPairs; i++) {
String name = readString();
String values = readString();
if (name.length() == 0) throw new IOException("name.length == 0");
entries.add(name);
entries.add(values);
}
doneReading();
return entries;
} catch (DataFormatException e) {
throw new IOException(e.getMessage());
}
}
public List<String> readNameValueBlock(int length) throws IOException {
this.compressedLimit += length;
try {
int numberOfPairs = nameValueBlockIn.readInt();
if (numberOfPairs < 0) {
throw new IOException("numberOfPairs < 0: " + numberOfPairs);
}
if (numberOfPairs > 1024) {
throw new IOException("numberOfPairs > 1024: " + numberOfPairs);
}
List<String> entries = new ArrayList<String>(numberOfPairs * 2);
for (int i = 0; i < numberOfPairs; i++) {
String name = readString();
String values = readString();
if (name.length() == 0) throw new IOException("name.length == 0");
entries.add(name);
entries.add(values);
}
doneReading();
return entries;
} catch (DataFormatException e) {
throw new IOException(e.getMessage());
}
}
public List<String> readNameValueBlock(int length) throws IOException {
this.compressedLimit += length;
try {
int numberOfPairs = nameValueBlockIn.readInt();
if (numberOfPairs < 0) {
throw new IOException("numberOfPairs < 0: " + numberOfPairs);
}
if (numberOfPairs > 1024) {
throw new IOException("numberOfPairs > 1024: " + numberOfPairs);
}
List<String> entries = new ArrayList<String>(numberOfPairs * 2);
for (int i = 0; i < numberOfPairs; i++) {
String name = readString();
String values = readString();
if (name.length() == 0) throw new IOException("name.length == 0");
entries.add(name);
entries.add(values);
}
doneReading();
return entries;
} catch (DataFormatException e) {
throw new IOException(e.getMessage());
}
}
public List<String> readNameValueBlock(int length) throws IOException {
this.compressedLimit += length;
try {
int numberOfPairs = nameValueBlockIn.readInt();
if (numberOfPairs < 0) {
throw new IOException("numberOfPairs < 0: " + numberOfPairs);
}
if (numberOfPairs > 1024) {
throw new IOException("numberOfPairs > 1024: " + numberOfPairs);
}
List<String> entries = new ArrayList<String>(numberOfPairs * 2);
for (int i = 0; i < numberOfPairs; i++) {
String name = readString();
String values = readString();
if (name.length() == 0) throw new IOException("name.length == 0");
entries.add(name);
entries.add(values);
}
doneReading();
return entries;
} catch (DataFormatException e) {
throw new IOException(e.getMessage());
}
}
/**
* Flushes this output stream, forcing any pending buffered output bytes to be
* written.
*
* @throws IOException if an I/O error occurs or this stream is already
* closed
*/
public void flush() throws IOException {
ensureOpen();
// Finish decompressing and writing pending output data
if (!inf.finished()) {
try {
while (!inf.finished() && !inf.needsInput()) {
int n;
// Decompress pending output data
n = inf.inflate(buf, 0, buf.length);
if (n < 1) {
break;
}
// Write the uncompressed output data block
out.write(buf, 0, n);
updateDigest(buf, 0, n);
}
super.flush();
} catch (DataFormatException ex) {
// Improperly formatted compressed (ZIP) data
String msg = ex.getMessage();
if (msg == null) {
msg = "Invalid ZLIB data format";
}
throw new ZipException(msg);
}
}
}
/**
* Flushes this output stream, forcing any pending buffered output bytes to be
* written.
*
* @throws IOException if an I/O error occurs or this stream is already
* closed
*/
public void flush() throws IOException {
ensureOpen();
// Finish decompressing and writing pending output data
if (!inf.finished()) {
try {
while (!inf.finished() && !inf.needsInput()) {
int n;
// Decompress pending output data
n = inf.inflate(buf, 0, buf.length);
if (n < 1) {
break;
}
// Write the uncompressed output data block
out.write(buf, 0, n);
updateDigest(buf, 0, n);
}
super.flush();
} catch (DataFormatException ex) {
// Improperly formatted compressed (ZIP) data
String msg = ex.getMessage();
if (msg == null) {
msg = "Invalid ZLIB data format";
}
throw new ZipException(msg);
}
}
}
/**
* Flushes this output stream, forcing any pending buffered output bytes to be
* written.
*
* @throws IOException if an I/O error occurs or this stream is already
* closed
*/
public void flush() throws IOException {
ensureOpen();
// Finish decompressing and writing pending output data
if (!inf.finished()) {
try {
while (!inf.finished() && !inf.needsInput()) {
int n;
// Decompress pending output data
n = inf.inflate(buf, 0, buf.length);
if (n < 1) {
break;
}
// Write the uncompressed output data block
out.write(buf, 0, n);
updateDigest(buf, 0, n);
}
super.flush();
} catch (DataFormatException ex) {
// Improperly formatted compressed (ZIP) data
String msg = ex.getMessage();
if (msg == null) {
msg = "Invalid ZLIB data format";
}
throw new ZipException(msg);
}
}
}
private int inflate(byte[] b, int off, int len) throws DataFormatException, ZipException {
checkState(inflater != null, "inflater is null");
try {
int inflaterTotalIn = inflater.getTotalIn();
int n = inflater.inflate(b, off, len);
int bytesConsumedDelta = inflater.getTotalIn() - inflaterTotalIn;
bytesConsumed += bytesConsumedDelta;
deflatedBytesConsumed += bytesConsumedDelta;
inflaterInputStart += bytesConsumedDelta;
crc.update(b, off, n);
if (inflater.finished()) {
// Save bytes written to check against the trailer ISIZE
expectedGzipTrailerIsize = (inflater.getBytesWritten() & 0xffffffffL);
state = State.TRAILER;
} else if (inflater.needsInput()) {
state = State.INFLATER_NEEDS_INPUT;
}
return n;
} catch (DataFormatException e) {
// Wrap the exception so tests can check for a specific prefix
throw new DataFormatException("Inflater data format exception: " + e.getMessage());
}
}
@Override
public synchronized int decompress(byte[] b, int off, int len)
throws IOException {
try {
return super.inflate(b, off, len);
} catch (DataFormatException dfe) {
throw new IOException(dfe.getMessage());
}
}
private List<String> readNameValueBlock(int length) throws IOException {
this.compressedLimit += length;
try {
int numberOfPairs = nameValueBlockIn.readInt();
if (numberOfPairs < 0) {
Logger.getLogger(getClass().getName()).warning("numberOfPairs < 0: " + numberOfPairs);
throw ioException("numberOfPairs < 0");
}
List<String> entries = new ArrayList<String>(numberOfPairs * 2);
for (int i = 0; i < numberOfPairs; i++) {
String name = readString();
String values = readString();
if (name.length() == 0) throw ioException("name.length == 0");
if (values.length() == 0) throw ioException("values.length == 0");
entries.add(name);
entries.add(values);
}
if (compressedLimit != 0) {
Logger.getLogger(getClass().getName()).warning("compressedLimit > 0: " + compressedLimit);
}
return entries;
} catch (DataFormatException e) {
throw new IOException(e.getMessage());
}
}
/**
* Reads uncompressed data into an array of bytes. If <code>len</code> is not
* zero, the method will block until some input can be decompressed; otherwise,
* no bytes are read and <code>0</code> is returned.
* @param b the buffer into which the data is read
* @param off the start offset in the destination array <code>b</code>
* @param len the maximum number of bytes read
* @return the actual number of bytes read, or -1 if the end of the
* compressed input is reached or a preset dictionary is needed
* @exception NullPointerException If <code>b</code> is <code>null</code>.
* @exception IndexOutOfBoundsException If <code>off</code> is negative,
* <code>len</code> is negative, or <code>len</code> is greater than
* <code>b.length - off</code>
* @exception ZipException if a ZIP format error has occurred
* @exception IOException if an I/O error has occurred
*/
public int read(byte[] b, int off, int len) throws IOException {
ensureOpen();
if (b == null) {
throw new NullPointerException();
} else if (off < 0 || len < 0 || len > b.length - off) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
return 0;
}
try {
int n;
while ((n = inf.inflate(b, off, len)) == 0) {
if (inf.finished() || inf.needsDictionary()) {
reachEOF = true;
return -1;
}
if (inf.needsInput()) {
fill();
}
}
return n;
} catch (DataFormatException e) {
String s = e.getMessage();
throw new ZipException(s != null ? s : "Invalid ZLIB data format");
}
}
/**
* Flushes this output stream, forcing any pending buffered output bytes to be
* written.
*
* @throws IOException if an I/O error occurs or this stream is already
* closed
*/
public void flush() throws IOException {
ensureOpen();
// Finish decompressing and writing pending output data
if (!inf.finished()) {
try {
while (!inf.finished() && !inf.needsInput()) {
int n;
// Decompress pending output data
n = inf.inflate(buf, 0, buf.length);
if (n < 1) {
break;
}
// Write the uncompressed output data block
out.write(buf, 0, n);
updateDigest(buf, 0, n);
}
super.flush();
} catch (DataFormatException ex) {
// Improperly formatted compressed (ZIP) data
String msg = ex.getMessage();
if (msg == null) {
msg = "Invalid ZLIB data format";
}
throw new ZipException(msg);
}
}
}
public int read(byte[] b, int off, int len) throws IOException {
if (b == null) {
throw new NullPointerException("input buffer is null");
} else if (off < 0 || len < 0 || len > b.length - off) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
return 0;
}
try {
int n;
if (bytesWritten >= uncompressedSize) {
finishInflating();
return -1;
}
while ((n = inflater.inflate(b, off, len)) == 0) {
if (inflater.finished() || inflater.needsDictionary()) {
finishInflating();
return -1;
}
if (inflater.needsInput()) {
fill();
}
}
bytesWritten += n;
return n;
} catch (DataFormatException e) {
String s = "Invalid ZLIB data format";
if (e.getMessage() != null) {
s = e.getMessage();
}
if (unzipEngine != null) {
if (unzipEngine.getLocalFileHeader().isEncrypted() &&
unzipEngine.getLocalFileHeader().getEncryptionMethod() == Zip4jConstants.ENC_METHOD_STANDARD) {
s += " - Wrong Password?";
}
}
throw new IOException(s);
}
}
private void readAndDecompress() throws IOException {
// Read the length of the compressed block
int ch1 = in.read();
int ch2 = in.read();
int ch3 = in.read();
int ch4 = in.read();
if ((ch1 | ch2 | ch3 | ch4) < 0)
throw new EOFException();
inLength = ((ch1 << 24) + (ch2 << 16) +
(ch3 << 8) + (ch4 << 0));
ch1 = in.read();
ch2 = in.read();
ch3 = in.read();
ch4 = in.read();
if ((ch1 | ch2 | ch3 | ch4) < 0)
throw new EOFException();
outLength = ((ch1 << 24) + (ch2 << 16) +
(ch3 << 8) + (ch4 << 0));
// Make sure we've got enough space to read the block
if ((inBuf == null) || (inLength > inBuf.length)) {
inBuf = new byte[inLength];
}
if ((outBuf == null) || (outLength > outBuf.length)) {
outBuf = new byte[outLength];
}
// Read until we're got the entire compressed buffer.
// read(...) will not necessarily block until all
// requested data has been read, so we loop until
// we're done.
int inOffs = 0;
while (inOffs < inLength) {
int inCount =
in.read(inBuf, inOffs, inLength - inOffs);
if (inCount == -1) {
throw new EOFException();
}
inOffs += inCount;
}
inflater.setInput(inBuf, 0, inLength);
try {
inflater.inflate(outBuf);
}
catch(DataFormatException dfe) {
throw new IOException(
"Data format exception - " +
dfe.getMessage());
}
// Reset the inflator so we can re-use it for the
// next block
inflater.reset();
outOffs = 0;
}