Search in sources :

Example 6 with GZIPInputStream

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);
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream)

Example 7 with GZIPInputStream

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);
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) ComponentObject(org.apache.storm.generated.ComponentObject) IOException(java.io.IOException) ParseException(org.json.simple.parser.ParseException) Map(java.util.Map) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap)

Example 8 with GZIPInputStream

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);
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 9 with GZIPInputStream

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;
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream)

Example 10 with GZIPInputStream

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;
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException)

Aggregations

GZIPInputStream (java.util.zip.GZIPInputStream)376 InputStream (java.io.InputStream)144 IOException (java.io.IOException)125 ByteArrayInputStream (java.io.ByteArrayInputStream)120 FileInputStream (java.io.FileInputStream)98 ByteArrayOutputStream (java.io.ByteArrayOutputStream)77 InputStreamReader (java.io.InputStreamReader)57 File (java.io.File)56 BufferedReader (java.io.BufferedReader)45 BufferedInputStream (java.io.BufferedInputStream)41 Test (org.junit.Test)41 FileOutputStream (java.io.FileOutputStream)30 URL (java.net.URL)25 InflaterInputStream (java.util.zip.InflaterInputStream)25 OutputStream (java.io.OutputStream)24 GZIPOutputStream (java.util.zip.GZIPOutputStream)21 ObjectInputStream (java.io.ObjectInputStream)19 HttpURLConnection (java.net.HttpURLConnection)19 URLConnection (java.net.URLConnection)17 HashMap (java.util.HashMap)15