Example usage for java.util.zip GZIPInputStream read

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

Introduction

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

Prototype

public int read(byte[] buf, int off, int len) throws IOException 

Source Link

Document

Reads uncompressed data into an array of bytes.

Usage

From source file:com.firegnom.valkyrie.map.tiled.TiledZoneLoader.java

/**
 * Gets the layer.// w  ww .j  av  a2 s . com
 *
 * @param map the map
 * @param element the element
 * @return the layer
 * @throws TiledLoaderException the tiled loader exception
 */
public Layer getLayer(Zone map, Element element) throws TiledLoaderException {
    // this.map = map;
    String name = element.getAttribute("name");
    int width = Integer.parseInt(element.getAttribute("width"));
    int height = Integer.parseInt(element.getAttribute("height"));
    Layer l = new Layer(name, width, height);
    // data = new int[width][height][3];
    // now read the layer properties

    Element propsElement = (Element) element.getElementsByTagName("properties").item(0);
    if (propsElement != null) {
        NodeList properties = propsElement.getElementsByTagName("property");
        if (properties != null) {
            l.props = new Properties();
            for (int p = 0; p < properties.getLength(); p++) {
                Element propElement = (Element) properties.item(p);

                String k = propElement.getAttribute("name");
                String v = propElement.getAttribute("value");
                l.props.setProperty(k, v);
            }
        }
    }

    Element dataNode = (Element) element.getElementsByTagName("data").item(0);
    String encoding = dataNode.getAttribute("encoding");
    String compression = dataNode.getAttribute("compression");
    long decodetime = 0, readTime = 0;
    if (encoding.equals("base64") && compression.equals("gzip")) {
        try {
            Node cdata = dataNode.getFirstChild();
            char[] enc = cdata.getNodeValue().trim().toCharArray();
            byte[] dec = Base64.decode(enc);

            GZIPInputStream is = new GZIPInputStream(new ByteArrayInputStream(dec));

            byte[] r = new byte[height * width * 4];
            int read = 0;
            while (read < height * width * 4) {
                read += is.read((byte[]) (r), read, height * width * 4 - read);
            }
            // Log.d("aadsdasdsa","data read :"+read+"more :"+is.available());
            // //5072
            // if (l.name.equals("Layer 2")){
            // read = is.read((byte[])(r), read, height*width*4-read);
            // Log.d("aadsdasdsa","data read :"+read+"more :"+is.available());
            // }

            int tileId, y, x;
            int pos = 0, tmp;
            StringTileSet set;

            for (y = 0; y < height; y++) {
                for (x = 0; x < width; x++) {
                    tmp = 0;
                    tmp |= ((int) (r[pos] & 0xFF));
                    tmp |= ((int) (r[pos + 1] & 0xFF)) << 8;
                    tmp |= ((int) (r[pos + 2] & 0xFF)) << 16;
                    tmp |= ((int) (r[pos + 3] & 0xFF)) << 24;

                    tileId = tmp;
                    l.data[x][y] = tileId;
                    if (tileId != 0 && map.activeTiles.get(tileId) == null) {
                        set = map.findTileSet(tileId);
                        // rl.downloadService(set.getTileNamePng(tileId));
                        map.activeTiles.put(tileId, set.getTileNamePng(tileId));
                    }
                    pos += 4;
                }

            }
            if (l.name.equals(Zone.MOVE_MATRIX)) {
                map.buildMoveMatrix(l);
            }
            return l;
        } catch (IOException e) {
            Log.e("error", e.toString());
            throw new TiledLoaderException("Unable to decode base 64 block");
        }
    } else {
        throw new TiledLoaderException(
                "Unsupport tiled map type: " + encoding + "," + compression + " (only gzip base64 supported)");
    }
}

From source file:InstallJars.java

protected int loadBytes(byte[] buf, GZIPInputStream zis) throws IOException {
    int loaded = -1;
    for (int size = zis.read(buf, 0, buf.length), count = 0; size > 0; size = zis.read(buf, loaded,
            buf.length - loaded), count++) {
        // if (count % 4 == 0) print(".");
        // System.out.println("loadBytes: loaded=" + loaded);
        if (loaded < 0) {
            loaded = 0;/*from  w w  w.  j  a  v  a2s . c  o  m*/
        }
        loaded += size;
    }
    return loaded;
}

From source file:grafix.basedados.Download.java

public String baixaArquivoGZIP() {
    String retorno = null;//www. j  a v  a  2  s.c o m
    URL url = null;
    URLConnection con = null;
    try {
        url = new URL(this.url);
        con = url.openConnection();
    } catch (Exception ex) {
        ex.printStackTrace();
        return retorno;
    }
    if (this.usaProxy) {
        System.setProperty("http.proxySet", "true");
        System.setProperty("http.proxyHost", this.servidorProxy);
        System.setProperty("http.proxyPort", String.format("%d", this.portaProxy));
        System.setProperty("http.proxyType", "4");
        if (this.usuarioProxy != null) {
            if (this.usuarioProxy.length() > 0) {
                String proxyUser = this.usuarioProxy, proxyPassword = this.senhaProxy;
                con.setRequestProperty("Proxy-Authorization",
                        "Basic " + Base64.encodeToString((proxyUser + ":" + proxyPassword).getBytes(), false));
            }
        }
    }
    GZIPInputStream gzip;
    try {
        gzip = new GZIPInputStream(con.getInputStream());
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        int readLen = 0;
        byte[] buffer = new byte[4096];
        int bytes = 0;
        //  int sfs =   gzip.      //con.getInputStream().available();
        int total = con.getContentLength();
        if (this.mostraProgresso)
            this.formAtualizacao.definirPercentualProgresso(0);
        while ((readLen = gzip.read(buffer, 0, buffer.length)) > 0) {
            os.write(buffer, 0, readLen);
            //  int sfs =  con.getInputStream().available();
            bytes += readLen;
            if (this.mostraProgresso)
                this.formAtualizacao.informaBytesLidos(bytes);
            // float sd = (float)(total-sfs)/(float)total*100.0f;
            // this.formAtualizacao.informarLog("Baixados" + (total - sfs) + " bytes de " + total + "progresso " + (int) sd);
            // this.formAtualizacao.definirPercentualProgresso( (int) sd);
        }
        retorno = os.toString();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

    return retorno;
}

From source file:com.qlkh.client.server.proxy.ProxyServlet.java

/**
 * A highly performant ungzip implementation. Do not refactor this without taking new timings.
 * See ElementTest in ehcache for timings
 *
 * @param gzipped the gzipped content//from ww w  . ja v  a  2s .c  om
 * @return an ungzipped byte[]
 * @throws java.io.IOException when something bad happens
 */
private byte[] ungzip(final byte[] gzipped) throws IOException {
    final GZIPInputStream inputStream = new GZIPInputStream(new ByteArrayInputStream(gzipped));
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(gzipped.length);
    final byte[] buffer = new byte[FOUR_KB];
    int bytesRead = 0;
    while (bytesRead != -1) {
        bytesRead = inputStream.read(buffer, 0, FOUR_KB);
        if (bytesRead != -1) {
            byteArrayOutputStream.write(buffer, 0, bytesRead);
        }
    }
    byte[] ungzipped = byteArrayOutputStream.toByteArray();
    inputStream.close();
    byteArrayOutputStream.close();
    return ungzipped;
}

From source file:org.warlock.itk.distributionenvelope.Payload.java

/**
 * Return decompressed content.//from   w w w. jav  a  2s .  com
 * @param t Base64 encoded string containing the compressed content.
 * @return
 * @throws Exception 
 */
private byte[] decompressBody(String t) throws Exception {
    byte decoded[] = null;
    Base64 b64 = new Base64();
    decoded = b64.decode(t.getBytes("UTF-8"));
    ByteArrayOutputStream uncomp = new ByteArrayOutputStream();
    GZIPInputStream gzIn = new GZIPInputStream(new ByteArrayInputStream(decoded), UNCOMPRESSBUFFERSIZE);
    byte[] buffer = new byte[UNCOMPRESSBUFFERSIZE];
    int l = -1;
    while ((l = gzIn.read(buffer, 0, UNCOMPRESSBUFFERSIZE)) != -1) {
        uncomp.write(buffer, 0, l);
    }
    gzIn.close();
    return uncomp.toByteArray();
}

From source file:net.cbtltd.rest.nextpax.A_Handler.java

/**
 * Get unzipped input stream for file name.
 * //from w  ww .j  av  a  2 s.c om
 * @param fn the file name.
 * @return the input stream.
 * @throws Throwable the exception that can be thrown.
 */
private final synchronized InputStream ftp(String fn) throws Throwable {
    String urlname = "ftp://bookingnet:FEJvvn$LYGCUd-2_Vq4zI@secure.nextpax.com/" + fn + ".gz;type=i";
    URL url = new URL(urlname);
    URLConnection urlc = url.openConnection();

    byte[] buf = new byte[1024];
    GZIPInputStream zinstream = new GZIPInputStream(urlc.getInputStream());
    FileOutputStream outstream = new FileOutputStream(fn);
    int n;
    while ((n = zinstream.read(buf, 0, 1024)) > -1) {
        outstream.write(buf, 0, n);
    }
    outstream.close();
    zinstream.close();
    return new BufferedInputStream(new FileInputStream(fn));
}