fixed gzip and tests
This commit is contained in:
parent
f39cbf08a9
commit
a4d3a6607e
|
@ -1,37 +1,54 @@
|
|||
package eu.dnetlib.apps.oai.utils;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.zip.GZIPInputStream;
|
||||
import java.util.zip.GZIPOutputStream;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
public class GzipUtils {
|
||||
|
||||
public static byte[] compress(final String data) {
|
||||
if (StringUtils.isBlank(data)) { return null; }
|
||||
public static byte[] compress(final String str) {
|
||||
if (StringUtils.isBlank(str)) { return null; }
|
||||
|
||||
try (final ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length());
|
||||
final GZIPOutputStream gzip = new GZIPOutputStream(bos)) {
|
||||
gzip.write(data.getBytes());
|
||||
try {
|
||||
final ByteArrayOutputStream obj = new ByteArrayOutputStream();
|
||||
final GZIPOutputStream gzip = new GZIPOutputStream(obj);
|
||||
gzip.write(str.getBytes("UTF-8"));
|
||||
gzip.flush();
|
||||
return bos.toByteArray();
|
||||
gzip.close();
|
||||
return obj.toByteArray();
|
||||
} catch (final IOException e) {
|
||||
throw new RuntimeException("error in gzip", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static String decompress(final byte[] bytes) {
|
||||
if (bytes == null || bytes.length == 0) { return null; }
|
||||
try (final ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
|
||||
final GZIPInputStream gis = new GZIPInputStream(bis)) {
|
||||
return IOUtils.toString(gis, "UTF-8");
|
||||
public static String decompress(final byte[] compressed) {
|
||||
final StringBuilder outStr = new StringBuilder();
|
||||
if (compressed == null || compressed.length == 0) { return null; }
|
||||
try {
|
||||
if (isCompressed(compressed)) {
|
||||
final GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(compressed));
|
||||
final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(gis, "UTF-8"));
|
||||
|
||||
String line;
|
||||
while ((line = bufferedReader.readLine()) != null) {
|
||||
outStr.append(line);
|
||||
}
|
||||
} else {
|
||||
outStr.append(compressed);
|
||||
}
|
||||
return outStr.toString();
|
||||
} catch (final IOException e) {
|
||||
throw new RuntimeException("error in gunzip", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isCompressed(final byte[] compressed) {
|
||||
return compressed[0] == (byte) GZIPInputStream.GZIP_MAGIC && compressed[1] == (byte) (GZIPInputStream.GZIP_MAGIC >> 8);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
package eu.dnetlib.apps.oai.utils;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class GzipUtilsTest {
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws Exception {}
|
||||
|
||||
@Test
|
||||
final void testCompressAndDecompress() {
|
||||
final String message = "Hello world";
|
||||
|
||||
final byte[] bytes = GzipUtils.compress(message);
|
||||
assertNotNull(bytes);
|
||||
assertTrue(bytes.length > 0);
|
||||
|
||||
assertEquals(message, GzipUtils.decompress(bytes));
|
||||
}
|
||||
|
||||
@Test
|
||||
final void testCompressNull() {
|
||||
assertNull(GzipUtils.compress(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
final void testDecompressNull() {
|
||||
assertNull(GzipUtils.decompress(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
final void testCompressEmpty() {
|
||||
assertNull(GzipUtils.compress(""));
|
||||
}
|
||||
|
||||
@Test
|
||||
final void testDecompressEmpty() {
|
||||
assertNull(GzipUtils.decompress(new byte[] {}));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue