Example usage for java.util.zip GZIPInputStream GZIPInputStream

List of usage examples for java.util.zip GZIPInputStream GZIPInputStream

Introduction

In this page you can find the example usage for java.util.zip GZIPInputStream GZIPInputStream.

Prototype

public GZIPInputStream(InputStream in) throws IOException 

Source Link

Document

Creates a new input stream with a default buffer size.

Usage

From source file:com.mbrlabs.mundus.utils.TerrainIO.java

public static Terrain importTerrain(ProjectContext projectContext, Terrain terrain) {
    FloatArray floatArray = new FloatArray();

    String terraPath = FilenameUtils.concat(projectContext.path, terrain.terraPath);
    try (DataInputStream is = new DataInputStream(
            new BufferedInputStream(new GZIPInputStream(new FileInputStream(terraPath))))) {
        while (is.available() > 0) {
            floatArray.add(is.readFloat());
        }//  w  w w. jav a2s  . c o m
    } catch (EOFException e) {
        //e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    //Log.debug("Terrain import. floats: " + floatArray.size);

    terrain.heightData = floatArray.toArray();
    terrain.init();
    terrain.update();

    // set default terrain base texture if none is present
    TerrainTexture terrainTexture = terrain.getTerrainTexture();
    if (terrainTexture.getTexture(SplatTexture.Channel.BASE) == null) {
        MTexture base = new MTexture();
        base.setId(-1);
        base.texture = TextureUtils.loadMipmapTexture(Gdx.files.internal("textures/terrain/chess.png"), true);
        terrainTexture.setSplatTexture(new SplatTexture(SplatTexture.Channel.BASE, base));
    }

    // load splat map if available
    SplatMap splatmap = terrainTexture.getSplatmap();
    if (splatmap != null) {
        String splatPath = FilenameUtils.concat(projectContext.path, splatmap.getPath());
        splatmap.loadPNG(Gdx.files.absolute(splatPath));
    }

    return terrain;
}

From source file:de.cellular.lib.lightlib.backend.LLHttpClientResponse.java

/**
 * Creates the instance of {@link LLHttpClientResponse}
 * //  w w  w .  j a  v  a 2s. c  o  m
 * @since 1.0
 * @param _urlStr
 *            the target url in {@link String}
 * @param _client
 *            the {@link DefaultHttpClient} with which we fired {@link LLRequest}.
 * @param _response
 *            the {@link HttpResponse} implementing object.
 * @return the created {@link LLHttpClientResponse}.
 * @throws IllegalStateException
 *             the illegal state exception
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static LLHttpClientResponse createInstance(String _urlStr, DefaultHttpClient _client,
        HttpResponse _response) throws IllegalStateException, IOException {
    if (_response != null) {
        LLHttpClientResponse r = new LLHttpClientResponse(_urlStr, _client, _response);
        int statusCode = r.mResponse.getStatusLine().getStatusCode();
        HttpEntity entity = r.mResponse.getEntity();
        if (statusCode == HttpStatus.SC_OK && entity != null) {
            InputStream instream = entity.getContent();
            Header contentType = r.mResponse.getFirstHeader("Content-Type");
            Header encoding = r.mResponse.getFirstHeader("Content-Encoding");

            if ((encoding != null && encoding.getValue().equalsIgnoreCase("gzip"))
                    || (contentType != null && contentType.getValue().equalsIgnoreCase("application/x-gzip"))) {
                r.mStream = new GZIPInputStream(instream);
            } else {
                r.mStream = instream;
            }
            if (r.mClient.getCookieStore() != null) {
                r.mCookies = r.mClient.getCookieStore().getCookies();
            }
            return r;
        }
        return null;
    } else {
        LL.e(":( Can't create an instance with NULL HttpResponse.");
        return null;
    }
}

From source file:gate.corpora.DocumentContentImpl.java

/** Contruction from URL and offsets. */
public DocumentContentImpl(URL u, String encoding, Long start, Long end) throws IOException {

    int readLength = 0;
    char[] readBuffer = new char[INTERNAL_BUFFER_SIZE];

    BufferedReader uReader = null;
    InputStream uStream = null;/*from   w ww  .ja v  a2s.c o m*/
    StringBuffer buf = new StringBuffer();

    long s = 0, e = Long.MAX_VALUE;
    if (start != null && end != null) {
        s = start.longValue();
        e = end.longValue();
    }

    try {
        URLConnection conn = u.openConnection();
        uStream = conn.getInputStream();

        if ("gzip".equals(conn.getContentEncoding())) {
            uStream = new GZIPInputStream(uStream);
        }

        if (encoding != null && !encoding.equalsIgnoreCase("")) {
            uReader = new BomStrippingInputStreamReader(uStream, encoding, INTERNAL_BUFFER_SIZE);
        } else {
            uReader = new BomStrippingInputStreamReader(uStream, INTERNAL_BUFFER_SIZE);
        }
        ;

        // 1. skip S characters
        uReader.skip(s);

        // 2. how many character shall I read?
        long toRead = e - s;

        // 3. read gtom source into buffer
        while (toRead > 0 && (readLength = uReader.read(readBuffer, 0, INTERNAL_BUFFER_SIZE)) != -1) {
            if (toRead < readLength) {
                //well, if toRead(long) is less than readLenght(int)
                //then there can be no overflow, so the cast is safe
                readLength = (int) toRead;
            }

            buf.append(readBuffer, 0, readLength);
            toRead -= readLength;
        }
    } finally {
        // 4.close reader
        IOUtils.closeQuietly(uReader);
        IOUtils.closeQuietly(uStream);
    }

    content = new String(buf);
    originalContent = content;
}

From source file:com.tedx.webservices.WebServices.java

public static JSONArray SendHttpPostArray(String URL, JSONObject jsonObjSend) {
    try {/*from   w  w  w .  j a va  2 s .  c  o m*/
        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpPost httpPostRequest = new HttpPost(URL);

        StringEntity se;
        se = new StringEntity(jsonObjSend.toString());

        // Set HTTP parameters
        httpPostRequest.setEntity(se);
        httpPostRequest.setHeader("Accept", "application/json");
        httpPostRequest.setHeader("Content-type", "application/json");
        //httpPostRequest.setHeader("Accept-Encoding", "gzip"); // only set this parameter if you would like to use gzip compression

        long t = System.currentTimeMillis();
        HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest);
        Log.i(TAG, "HTTPResponse received in [" + (System.currentTimeMillis() - t) + "ms]");

        // Get hold of the response entity (-> the data):
        HttpEntity entity = response.getEntity();

        if (entity != null) {
            // Read the content stream
            InputStream instream = entity.getContent();
            Header contentEncoding = response.getFirstHeader("Content-Encoding");
            if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
                instream = new GZIPInputStream(instream);
            }

            // convert content stream to a String
            String resultString = convertStreamToString(instream);
            instream.close();

            // Transform the String into a JSONObject
            JSONArray jsonObjRecv = new JSONArray(resultString);
            // Raw DEBUG output of our received JSON object:
            Log.i(TAG, "<jsonobject>\n" + jsonObjRecv.toString() + "\n</jsonobject>");

            return jsonObjRecv;
        }
    } catch (Exception e) {
        // More about HTTP exception handling in another tutorial.
        // For now we just print the stack trace.
        e.printStackTrace();
    }
    return null;
}

From source file:edu.umd.umiacs.clip.tools.io.GZIPFiles.java

protected static Stream<CSVRecord> records(CSVFormat format, Path path) throws IOException {
    return StreamSupport.stream(format.parse(new BufferedReader(new InputStreamReader(
            new GZIPInputStream(new BufferedInputStream(newInputStream(path), BUFFER_SIZE)),
            UTF_8.newDecoder().onMalformedInput(IGNORE)))).spliterator(), false);
}

From source file:ezbake.deployer.publishers.artifact.PythonRequirementsPublisher.java

@Override
public Collection<ArtifactDataEntry> generateEntries(DeploymentArtifact artifact) throws DeploymentException {

    // load the tarball
    TarArchiveInputStream tarIn = null;//from   w  ww  . j a va 2s .c  o m
    try {
        byte[] a = artifact.getArtifact();
        tarIn = new TarArchiveInputStream(new GZIPInputStream(new ByteArrayInputStream(a)));
    } catch (IOException e) {
        e.printStackTrace();
    }

    // collect the packages
    // (pip fails if there are duplicate package names, so we're using map
    // to avoid repeated packages)
    TarArchiveEntry entry = null;
    Map<String, String> requirements = new HashMap<String, String>();
    do {
        try {
            if (tarIn != null) {
                entry = tarIn.getNextTarEntry();
            }
        } catch (IOException e) {
            entry = null;
        }

        // get the name of the current entry
        String name = null;
        if (entry != null) {
            name = entry.getName();
        }

        if ((entry != null) && name.startsWith("./wheels/")) {
            String[] parts = name.split("/");
            String basename = parts[parts.length - 1];

            String[] p = extractPackageInfo(basename);

            if ((p != null) && (p.length == 2)) {
                requirements.put(p[0], p[1]);
            }
        }
    } while (entry != null);

    // create requirements.txt output
    StringBuffer out = new StringBuffer();
    out.append("# commands for pip\n");
    out.append("--no-index\n");
    out.append("--use-wheels\n");
    out.append("--find-links ./wheels/\n\n");

    out.append("# requirements\n");
    for (Map.Entry<String, String> p : requirements.entrySet()) {
        out.append(p.getKey()); // package name
        out.append("==");
        out.append(p.getValue()); // package version
        out.append("\n");
    }

    return Lists.newArrayList(
            new ArtifactDataEntry(new TarArchiveEntry("requirements.txt"), out.toString().getBytes()));
}

From source file:edu.cmu.cs.lti.ark.util.SerializedObjects.java

public static InputStream getInputStream(String inputFile) throws IOException {
    if (inputFile.endsWith(".gz")) {
        return new GZIPInputStream(new FileInputStream(inputFile));
    } else {/*ww w.  j  a v a  2  s . c o  m*/
        return new FileInputStream(inputFile);
    }
}

From source file:com.arturmkrtchyan.kafka.util.TarUnpacker.java

protected TarArchiveInputStream createTarArchiveInputStream(final InputStream inputStream,
        final boolean isGzipped) throws IOException {
    return (isGzipped) ? new TarArchiveInputStream(new GZIPInputStream(inputStream), BUFFER_SIZE)
            : new TarArchiveInputStream(new BufferedInputStream(inputStream), BUFFER_SIZE);
}

From source file:com.stratio.parsers.XMLDumpParser.java

/**
 *
 * Based on wikixmlj by Delip Rao and Jason Smith.
 * Licensed under the Apache License 2.0.
 * Available at http://code.google.com/p/wikixmlj/
 *
 * @return//from w w w.j  a va 2 s.  com
 * @throws FileNotFoundException
 * @throws IOException
 */
protected InputSource getInputSource() throws FileNotFoundException, IOException {
    if (inStream != null) {
        return new InputSource(inStream);
    }

    FileInputStream fis = new FileInputStream(dumpFile);
    InputStream is;
    InputStreamReader isr;

    if (dumpFile.endsWith(".gz")) {
        is = new GZIPInputStream(fis);
    } else if (dumpFile.endsWith(".bz2")) {
        byte[] ignoreBytes = new byte[2];
        fis.read(ignoreBytes); //"B", "Z" bytes from commandline tools
        is = new CBZip2InputStream(fis);
    } else {
        is = fis;
    }

    CharsetDecoder decoder = Charsets.UTF_8.newDecoder();
    decoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
    decoder.onMalformedInput(CodingErrorAction.IGNORE);
    decoder.replaceWith("?");
    isr = new InputStreamReader(is, decoder);

    return new InputSource(isr);
    //return new InputSource(new BufferedReader(isr));
}

From source file:com.kolich.havalo.io.stores.MetaObjectStore.java

/**
 * The caller is most definitely responsible for closing the
 * returned {@link InputStream} when finished with it.
 * @param index/*  w w w  .  j a v a2  s .c o m*/
 * @return
 */
@Override
public InputStream getInputStream(final String index) {
    InputStream is = null;
    try {
        is = new GZIPInputStream(new FileInputStream(getCanonicalFile(index, false)));
    } catch (Exception e) {
        throw new ObjectLoadException("Failed to read entity: " + index, e);
    }
    return is;
}