Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

To prevent some issues? #92

Merged
merged 1 commit into from
Dec 10, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 31 additions & 19 deletions src/main/java/org/itxtech/nemisys/utils/Zlib.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package org.itxtech.nemisys.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.DataFormatException;
import java.io.InputStream;
import java.util.zip.Deflater;
import java.util.zip.Inflater;
import java.util.zip.InflaterInputStream;


public abstract class Zlib {
Expand All @@ -20,30 +21,41 @@ public static byte[] deflate(byte[] data, int level) throws Exception {
deflater.finish();
ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length);
byte[] buf = new byte[1024];
while (!deflater.finished()) {
int i = deflater.deflate(buf);
bos.write(buf, 0, i);
try {
while (!deflater.finished()) {
int i = deflater.deflate(buf);
bos.write(buf, 0, i);
}
} finally {
deflater.end();
}
deflater.end();
return bos.toByteArray();
}

public static byte[] inflate(byte[] data) throws DataFormatException, IOException {
Inflater inflater = new Inflater();
inflater.setInput(data);
ByteArrayOutputStream o = new ByteArrayOutputStream(data.length);
byte[] buf = new byte[1024];
while (!inflater.finished()) {
int i = inflater.inflate(buf);
o.write(buf, 0, i);
public static byte[] inflate(InputStream stream) throws IOException {
InflaterInputStream inputStream = new InflaterInputStream(stream);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;

while ((length = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, length);
}
inflater.end();
return o.toByteArray();

buffer = outputStream.toByteArray();
outputStream.flush();
outputStream.close();
inputStream.close();

return buffer;
}

public static byte[] inflate(byte[] data, int maxSize) throws DataFormatException, IOException {
return Binary.subBytes(inflate(data), 0, maxSize);
public static byte[] inflate(byte[] data) throws IOException {
return inflate(new ByteArrayInputStream(data));
}

}
public static byte[] inflate(byte[] data, int maxSize) throws IOException {
return inflate(new ByteArrayInputStream(data, 0, maxSize));
}

}