use of java.util.zip.GZIPInputStream in project storm by apache.
the class GzipSerializationDelegate method deserialize.
@Override
public <T> T deserialize(byte[] bytes, Class<T> clazz) {
try {
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
GZIPInputStream gis = new GZIPInputStream(bis);
ObjectInputStream ois = new ObjectInputStream(gis);
Object ret = ois.readObject();
ois.close();
return (T) ret;
} catch (IOException ioe) {
throw new RuntimeException(ioe);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
use of java.util.zip.GZIPInputStream in project storm by apache.
the class Utils method fromCompressedJsonConf.
public static Map<String, Object> fromCompressedJsonConf(byte[] serialized) {
try {
ByteArrayInputStream bis = new ByteArrayInputStream(serialized);
InputStreamReader in = new InputStreamReader(new GZIPInputStream(bis));
Object ret = JSONValue.parseWithException(in);
in.close();
return (Map<String, Object>) ret;
} catch (IOException | ParseException e) {
throw new RuntimeException(e);
}
}
use of java.util.zip.GZIPInputStream in project storm by apache.
the class Utils method gunzip.
public static byte[] gunzip(byte[] data) {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ByteArrayInputStream bis = new ByteArrayInputStream(data);
GZIPInputStream in = new GZIPInputStream(bis);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = in.read(buffer)) >= 0) {
bos.write(buffer, 0, len);
}
in.close();
bos.close();
return bos.toByteArray();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of java.util.zip.GZIPInputStream in project storm by apache.
the class WritableUtils method readCompressedByteArray.
public static byte[] readCompressedByteArray(DataInput in) throws IOException {
int length = in.readInt();
if (length == -1)
return null;
byte[] buffer = new byte[length];
// could/should use readFully(buffer,0,length)?
in.readFully(buffer);
GZIPInputStream gzi = new GZIPInputStream(new ByteArrayInputStream(buffer, 0, buffer.length));
byte[] outbuf = new byte[length];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int len;
while ((len = gzi.read(outbuf, 0, outbuf.length)) != -1) {
bos.write(outbuf, 0, len);
}
byte[] decompressed = bos.toByteArray();
bos.close();
gzi.close();
return decompressed;
}
use of java.util.zip.GZIPInputStream in project GCViewer by chewiebug.
the class DataReaderFactory method getDataReader.
/**
* Returns the {@link DataReader} determined by content analysis. If no datareader can
* be determined, an Exception is thrown.
*
* @param gcResource resource information for inputStream
* @param inStream input stream to be read
* @return DataReader appropriate datareader if it could be determined
* @throws IOException if no appropriate datareader could be determined
*/
public DataReader getDataReader(GCResource gcResource, InputStream inStream) throws IOException {
this.gcResource = gcResource;
InputStream in = new BufferedInputStream(inStream, FOUR_KB);
// isGZipped relies on streams to support "mark" -> BufferdInputStream does
if (isGZipped(in)) {
getLogger().info("GZip stream detected");
in = new BufferedInputStream(new GZIPInputStream(in, FOUR_KB), FOUR_KB);
}
DataReader dataReader = null;
long nextPos = 0;
String chunkOfLastLine = null;
int attemptCount = 0;
String s = "";
while (attemptCount < MAX_ATTEMPT_COUNT) {
in.mark(FOUR_KB + (int) nextPos);
if (nextPos > 0) {
long skipped = in.skip(nextPos);
if (skipped != nextPos) {
break;
}
}
byte[] buf = new byte[ONE_KB * 3];
int length = in.read(buf);
in.reset();
if (length <= 0) {
break;
}
nextPos += length;
s = new String(buf, 0, length, "ASCII");
// prepend chunk of last block
if (chunkOfLastLine != null && chunkOfLastLine.length() > 0) {
s = chunkOfLastLine + s;
}
// deal with chunk of last line in the new block; cut it from end of block
chunkOfLastLine = getChunkOfLastLine(s);
if (chunkOfLastLine.length() > 0) {
s = s.substring(0, s.lastIndexOf(chunkOfLastLine));
}
dataReader = getDataReaderBySample(s, gcResource, in);
if (dataReader != null) {
break;
}
attemptCount++;
}
if (dataReader == null) {
if (getLogger().isLoggable(Level.SEVERE))
getLogger().severe(LocalisationHelper.getString("datareaderfactory_instantiation_failed") + "\ncontent:" + "\n" + s);
throw new IOException(LocalisationHelper.getString("datareaderfactory_instantiation_failed"));
}
return dataReader;
}
Aggregations