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 b[]) throws IOException 

Source Link

Document

Reads up to b.length bytes of data from this input stream into an array of bytes.

Usage

From source file:com.meetup.memcached.MemcachedClient.java

/** 
 * This method loads the data from cache into a Map.
 *
 * Pass a SockIO object which is ready to receive data and a HashMap<br/>
 * to store the results./*from   ww  w . ja  v a2s. c o  m*/
 * 
 * @param sock socket waiting to pass back data
 * @param hm hashmap to store data into
 * @param asString if true, and if we are using NativehHandler, return string val
 * @throws IOException if io exception happens while reading from socket
 */
private void loadMulti(LineInputStream input, Map<String, Object> hm, boolean asString) throws IOException {

    while (true) {
        String line = input.readLine();
        if (log.isDebugEnabled())
            log.debug("++++ line: " + line);

        if (line.startsWith(VALUE)) {
            String[] info = line.split(" ");
            String key = info[1];
            int flag = Integer.parseInt(info[2]);
            int length = Integer.parseInt(info[3]);

            if (log.isDebugEnabled()) {
                log.debug("++++ key: " + key);
                log.debug("++++ flags: " + flag);
                log.debug("++++ length: " + length);
            }

            // read obj into buffer
            byte[] buf = new byte[length];
            input.read(buf);
            input.clearEOL();

            // ready object
            Object o;

            // check for compression
            if ((flag & F_COMPRESSED) == F_COMPRESSED) {
                try {
                    // read the input stream, and write to a byte array output stream since
                    // we have to read into a byte array, but we don't know how large it
                    // will need to be, and we don't want to resize it a bunch
                    GZIPInputStream gzi = new GZIPInputStream(new ByteArrayInputStream(buf));
                    ByteArrayOutputStream bos = new ByteArrayOutputStream(buf.length);

                    int count;
                    byte[] tmp = new byte[2048];
                    while ((count = gzi.read(tmp)) != -1) {
                        bos.write(tmp, 0, count);
                    }

                    // store uncompressed back to buffer
                    buf = bos.toByteArray();
                    gzi.close();
                } catch (IOException e) {

                    // if we have an errorHandler, use its hook
                    if (errorHandler != null)
                        errorHandler.handleErrorOnGet(this, e, key);

                    log.error(
                            "++++ IOException thrown while trying to uncompress input stream for key: " + key);
                    log.error(e.getMessage(), e);
                    throw new NestedIOException(
                            "++++ IOException thrown while trying to uncompress input stream for key: " + key,
                            e);
                }
            }

            // we can only take out serialized objects
            if ((flag & F_SERIALIZED) != F_SERIALIZED) {
                if (primitiveAsString || asString) {
                    // pulling out string value
                    if (log.isInfoEnabled())
                        log.info("++++ retrieving object and stuffing into a string.");
                    o = new String(buf, defaultEncoding);
                } else {
                    // decoding object
                    try {
                        o = NativeHandler.decode(buf, flag);
                    } catch (Exception e) {

                        // if we have an errorHandler, use its hook
                        if (errorHandler != null)
                            errorHandler.handleErrorOnGet(this, e, key);

                        log.error("++++ Exception thrown while trying to deserialize for key: " + key, e);
                        throw new NestedIOException(e);
                    }
                }
            } else {
                // deserialize if the data is serialized
                ContextObjectInputStream ois = new ContextObjectInputStream(new ByteArrayInputStream(buf),
                        classLoader);
                try {
                    o = ois.readObject();
                    if (log.isInfoEnabled())
                        log.info("++++ deserializing " + o.getClass());
                } catch (ClassNotFoundException e) {

                    // if we have an errorHandler, use its hook
                    if (errorHandler != null)
                        errorHandler.handleErrorOnGet(this, e, key);

                    log.error("++++ ClassNotFoundException thrown while trying to deserialize for key: " + key,
                            e);
                    throw new NestedIOException("+++ failed while trying to deserialize for key: " + key, e);
                } finally {
                    ois.close();
                }
            }

            // store the object into the cache
            hm.put(key, o);
        } else if (END.equals(line)) {
            if (log.isDebugEnabled())
                log.debug("++++ finished reading from cache server");
            break;
        }
    }
}

From source file:com.meetup.memcached.MemcachedClient.java

/**
 * Retrieve a key from the server, using a specific hash.
 *
 *  If the data was compressed or serialized when compressed, it will automatically<br/>
 *  be decompressed or serialized, as appropriate. (Inclusive or)<br/>
 *<br/>//from   w ww  . ja  va  2 s . co  m
 *  Non-serialized data will be returned as a string, so explicit conversion to<br/>
 *  numeric types will be necessary, if desired<br/>
 *
 * @param key key where data is stored
 * @param hashCode if not null, then the int hashcode to use
 * @param asString if true, then return string val
 * @return the object that was previously stored, or null if it was not previously stored
 */
public Object get(String key, Integer hashCode, boolean asString) {

    if (key == null) {
        log.error("key is null for get()");
        return null;
    }

    try {
        key = sanitizeKey(key);
    } catch (UnsupportedEncodingException e) {

        // if we have an errorHandler, use its hook
        if (errorHandler != null)
            errorHandler.handleErrorOnGet(this, e, key);

        log.error("failed to sanitize your key!", e);
        return null;
    }

    // get SockIO obj using cache key
    SockIOPool.SockIO sock = pool.getSock(key, hashCode);

    if (sock == null) {
        if (errorHandler != null)
            errorHandler.handleErrorOnGet(this, new IOException("no socket to server available"), key);
        return null;
    }

    try {
        String cmd = "get " + key + "\r\n";

        if (log.isDebugEnabled())
            log.debug("++++ memcache get command: " + cmd);

        sock.write(cmd.getBytes());
        sock.flush();

        // ready object
        Object o = null;

        while (true) {
            String line = sock.readLine();

            if (log.isDebugEnabled())
                log.debug("++++ line: " + line);

            if (line.startsWith(VALUE)) {
                String[] info = line.split(" ");
                int flag = Integer.parseInt(info[2]);
                int length = Integer.parseInt(info[3]);

                if (log.isDebugEnabled()) {
                    log.debug("++++ key: " + key);
                    log.debug("++++ flags: " + flag);
                    log.debug("++++ length: " + length);
                }

                // read obj into buffer
                byte[] buf = new byte[length];
                sock.read(buf);
                sock.clearEOL();

                if ((flag & F_COMPRESSED) == F_COMPRESSED) {
                    try {
                        // read the input stream, and write to a byte array output stream since
                        // we have to read into a byte array, but we don't know how large it
                        // will need to be, and we don't want to resize it a bunch
                        GZIPInputStream gzi = new GZIPInputStream(new ByteArrayInputStream(buf));
                        ByteArrayOutputStream bos = new ByteArrayOutputStream(buf.length);

                        int count;
                        byte[] tmp = new byte[2048];
                        while ((count = gzi.read(tmp)) != -1) {
                            bos.write(tmp, 0, count);
                        }

                        // store uncompressed back to buffer
                        buf = bos.toByteArray();
                        gzi.close();
                    } catch (IOException e) {

                        // if we have an errorHandler, use its hook
                        if (errorHandler != null)
                            errorHandler.handleErrorOnGet(this, e, key);

                        log.error("++++ IOException thrown while trying to uncompress input stream for key: "
                                + key);
                        log.error(e.getMessage(), e);
                        throw new NestedIOException(
                                "++++ IOException thrown while trying to uncompress input stream for key: "
                                        + key,
                                e);
                    }
                }

                // we can only take out serialized objects
                if ((flag & F_SERIALIZED) != F_SERIALIZED) {
                    if (primitiveAsString || asString) {
                        // pulling out string value
                        if (log.isInfoEnabled())
                            log.info("++++ retrieving object and stuffing into a string.");
                        o = new String(buf, defaultEncoding);
                    } else {
                        // decoding object
                        try {
                            o = NativeHandler.decode(buf, flag);
                        } catch (Exception e) {

                            // if we have an errorHandler, use its hook
                            if (errorHandler != null)
                                errorHandler.handleErrorOnGet(this, e, key);

                            log.error("++++ Exception thrown while trying to deserialize for key: " + key, e);
                            throw new NestedIOException(e);
                        }
                    }
                } else {
                    // deserialize if the data is serialized
                    ContextObjectInputStream ois = new ContextObjectInputStream(new ByteArrayInputStream(buf),
                            classLoader);
                    try {
                        o = ois.readObject();
                        if (log.isInfoEnabled())
                            log.info("++++ deserializing " + o.getClass());
                    } catch (ClassNotFoundException e) {

                        // if we have an errorHandler, use its hook
                        if (errorHandler != null)
                            errorHandler.handleErrorOnGet(this, e, key);

                        log.error("++++ ClassNotFoundException thrown while trying to deserialize for key: "
                                + key, e);
                        throw new NestedIOException("+++ failed while trying to deserialize for key: " + key,
                                e);
                    } finally {
                        ois.close();
                    }
                }
            } else if (END.equals(line)) {
                if (log.isDebugEnabled())
                    log.debug("++++ finished reading from cache server");
                break;
            }
        }

        sock.close();
        sock = null;
        return o;
    } catch (IOException e) {

        // if we have an errorHandler, use its hook
        if (errorHandler != null)
            errorHandler.handleErrorOnGet(this, e, key);

        // exception thrown
        log.error("++++ exception thrown while trying to get object from cache for key: " + key);
        log.error(e.getMessage(), e);

        try {
            sock.trueClose();
        } catch (IOException ioe) {
            log.error("++++ failed to close socket : " + sock.toString());
        }
        sock = null;
    }

    if (sock != null)
        sock.close();

    return null;
}