Java Code Examples for java.util.zip.InflaterInputStream#read()

The following examples show how to use java.util.zip.InflaterInputStream#read() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DeflateUtils.java    From anthelion with Apache License 2.0 7 votes vote down vote up
/**
 * Returns an inflated copy of the input array.  
 * @throws IOException if the input cannot be properly decompressed
 */
public static final byte[] inflate(byte[] in) throws IOException {
  // decompress using InflaterInputStream 
  ByteArrayOutputStream outStream = 
    new ByteArrayOutputStream(EXPECTED_COMPRESSION_RATIO * in.length);

  InflaterInputStream inStream = 
    new InflaterInputStream ( new ByteArrayInputStream(in) );

  byte[] buf = new byte[BUF_SIZE];
  while (true) {
    int size = inStream.read(buf);
    if (size <= 0) 
      break;
    outStream.write(buf, 0, size);
  }
  outStream.close();

  return outStream.toByteArray();
}
 
Example 2
Source File: ZLibUtils.java    From QQRobot with GNU General Public License v3.0 6 votes vote down vote up
/** 
 * 解压缩 
 *  
 * @param is 
 *            输入流 
 * @return byte[] 解压缩后的数据 
 */  
public static byte[] decompress(InputStream is) {  
    InflaterInputStream iis = new InflaterInputStream(is);  
    ByteArrayOutputStream o = new ByteArrayOutputStream(1024);  
    try {  
        int i = 1024;  
        byte[] buf = new byte[i];  

        while ((i = iis.read(buf, 0, i)) > 0) {  
            o.write(buf, 0, i);  
        }  

    } catch (IOException e) {  
        e.printStackTrace();  
    }  
    return o.toByteArray();  
}
 
Example 3
Source File: ZlibOriginal.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public byte[] inflate(InputStream stream) throws IOException {
    InflaterInputStream inputStream = new InflaterInputStream(stream);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int length;

    try {
        while ((length = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, length);
        }
    } finally {
        buffer = outputStream.toByteArray();
        outputStream.flush();
        outputStream.close();
        inputStream.close();
    }

    return buffer;
}
 
Example 4
Source File: ZLibUtils.java    From android-project-wo2b with Apache License 2.0 6 votes vote down vote up
/**
 * 解压缩
 * 
 * @param is 输入流
 * @return byte[] 解压缩后的数据
 */
public static byte[] decompress(InputStream is)
{
	InflaterInputStream iis = new InflaterInputStream(is);
	ByteArrayOutputStream o = new ByteArrayOutputStream(BUFFER_LENGTH);
	try
	{
		byte[] buf = new byte[BUFFER_LENGTH];
		int len = -1;
		while ((len = iis.read(buf)) != -1)
		{
			o.write(buf, 0, len);
		}
	}
	catch (IOException e)
	{
		e.printStackTrace();
	}

	return o.toByteArray();
}
 
Example 5
Source File: ZlibSingleThreadLowMem.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
synchronized public byte[] inflate(InputStream stream) throws IOException {
    InflaterInputStream inputStream = new InflaterInputStream(stream);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(BUFFER_SIZE);
    byte[] bufferOut;
    int length;

    try {
        while ((length = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, length);
        }
    } finally {
        outputStream.flush();
        bufferOut = outputStream.toByteArray();
        outputStream.close();
        inputStream.close();
    }

    return bufferOut;
}
 
Example 6
Source File: Zlib.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
public static byte[] inflate(InputStream stream) throws IOException {
    InflaterInputStream inputStream = new InflaterInputStream(stream);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int length;

    try {
        while ((length = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, length);
        }
    } finally {
        buffer = outputStream.toByteArray();
        outputStream.flush();
        outputStream.close();
        inputStream.close();
    }

    return buffer;
}
 
Example 7
Source File: ZlibThreadLocal.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public byte[] inflate(InputStream stream) throws IOException {
    InflaterInputStream inputStream = new InflaterInputStream(stream);
    FastByteArrayOutputStream outputStream = ThreadCache.fbaos.get();
    outputStream.reset();
    int length;

    byte[] result;
    byte[] buffer = buf.get();
    try {
        while ((length = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, length);
        }
    } finally {
        result = outputStream.toByteArray();
        outputStream.flush();
        outputStream.close();
        inputStream.close();
    }

    return result;
}
 
Example 8
Source File: PdfReader.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * A helper to FlateDecode.
 *
 * @param in the input data
 * @param strict <CODE>true</CODE> to read a correct stream. <CODE>false</CODE> to try to read a
 *            corrupted stream
 * @return the decoded data
 */
public static byte[] FlateDecode(byte in[], boolean strict) {
	ByteArrayInputStream stream = new ByteArrayInputStream(in);
	InflaterInputStream zip = new InflaterInputStream(stream);
	ByteArrayOutputStream out = new ByteArrayOutputStream();
	byte b[] = new byte[strict ? 4092 : 1];
	try {
		int n;
		while ((n = zip.read(b)) >= 0) {
			out.write(b, 0, n);
		}
		zip.close();
		out.close();
		return out.toByteArray();
	} catch (Exception e) {
		if (strict) {
			return null;
		}
		return out.toByteArray();
	}
}
 
Example 9
Source File: Usernotes.java    From Slide with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Converts a base64 encoded and zlib compressed blob into a String.
 * @param blob Blob to convert to string
 * @return Decoded blob
 */
public static String blobToJson(String blob) {
    final byte[] decoded = Base64.decode(blob, Base64.DEFAULT);

    // Adapted from https://stackoverflow.com/a/33022277
    try {
        ByteArrayInputStream input = new ByteArrayInputStream(decoded);
        InflaterInputStream inflater = new InflaterInputStream(input);

        StringBuilder result = new StringBuilder();
        byte[] buf = new byte[5];
        int rlen;
        while ((rlen = inflater.read(buf)) != -1) {
            result.append(new String(Arrays.copyOf(buf, rlen)));
        }
        return result.toString();
    } catch (IOException e) {
        return null;
    }
}
 
Example 10
Source File: Zlib.java    From Nemisys with GNU General Public License v3.0 6 votes vote down vote up
public static byte[] inflate(InputStream stream) throws IOException {
    InflaterInputStream inputStream = new InflaterInputStream(stream);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int length;

    while ((length = inputStream.read(buffer)) != -1) {
        outputStream.write(buffer, 0, length);
    }

    buffer = outputStream.toByteArray();
    outputStream.flush();
    outputStream.close();
    inputStream.close();

    return buffer;
}
 
Example 11
Source File: DeflateCompressorTest.java    From archive-patcher with Apache License 2.0 5 votes vote down vote up
/**
 * Uncompress some content with Java's built-in {@link Inflater} as a sanity check against our own
 * code.
 * @param nowrap value to set for the nowrap parameter for the {@link Inflater}
 * @param compressedData
 * @return the uncompressed data as a byte array
 * @throws IOException if anything goes wrong
 */
private byte[] uncompressWithJavaInflater(boolean nowrap, byte[] compressedData)
    throws IOException {
  Inflater inflater = new Inflater(nowrap);
  InflaterInputStream inflaterIn =
      new InflaterInputStream(new ByteArrayInputStream(compressedData), inflater);
  byte[] buffer = new byte[32768];
  ByteArrayOutputStream uncompressedOut = new ByteArrayOutputStream();
  int numRead = 0;
  while ((numRead = inflaterIn.read(buffer)) >= 0) {
    uncompressedOut.write(buffer, 0, numRead);
  }
  return uncompressedOut.toByteArray();
}
 
Example 12
Source File: BlobBackupHelper.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private byte[] inflate(byte[] compressedData) {
    byte[] result = null;
    if (compressedData != null) {
        try {
            ByteArrayInputStream source = new ByteArrayInputStream(compressedData);
            DataInputStream headerIn = new DataInputStream(source);
            int version = headerIn.readInt();
            if (version > mCurrentBlobVersion) {
                Log.w(TAG, "Saved payload from unrecognized version " + version);
                return null;
            }

            InflaterInputStream in = new InflaterInputStream(source);
            ByteArrayOutputStream inflated = new ByteArrayOutputStream();
            byte[] buffer = new byte[4096];
            int nRead;
            while ((nRead = in.read(buffer)) > 0) {
                inflated.write(buffer, 0, nRead);
            }
            in.close();
            inflated.flush();
            result = inflated.toByteArray();
            if (DEBUG) {
                Log.v(TAG, "Inflated " + compressedData.length + " bytes to " + result.length);
            }
        } catch (IOException e) {
            // result is still null here
            Log.w(TAG, "Unable to process restored payload: " + e.getMessage());
        }
    }
    return result;
}
 
Example 13
Source File: DeflateUncompressor.java    From archive-patcher with Apache License 2.0 5 votes vote down vote up
@Override
public void uncompress(InputStream compressedIn, OutputStream uncompressedOut)
    throws IOException {
  InflaterInputStream inflaterIn =
      new InflaterInputStream(compressedIn, createOrResetInflater(), inputBufferSize);
  byte[] buffer = new byte[outputBufferSize];
  int numRead = 0;
  while ((numRead = inflaterIn.read(buffer)) >= 0) {
    uncompressedOut.write(buffer, 0, numRead);
  }
  if (!isCaching()) {
    release();
  }
}
 
Example 14
Source File: RegisterBrokerBody.java    From rocketmq with Apache License 2.0 5 votes vote down vote up
private static byte[] readBytes(InflaterInputStream inflaterInputStream, int length) throws IOException {
    byte[] buffer = new byte[length];
    int bytesRead = 0;
    while (bytesRead < length) {
        int len = inflaterInputStream.read(buffer, bytesRead, length - bytesRead);
        if (len == -1) {
            throw new IOException("End of compressed data has reached");
        } else {
            bytesRead += len;
        }
    }
    return buffer;
}
 
Example 15
Source File: Compresser.java    From zstack with Apache License 2.0 5 votes vote down vote up
public static byte[] inflate(byte[] input, int bufferSize) throws IOException {
    ByteArrayInputStream in = new ByteArrayInputStream(input);
    Inflater inf = new Inflater();
    InflaterInputStream iis = new InflaterInputStream(in, inf, bufferSize);
    ByteArrayOutputStream out = new ByteArrayOutputStream(input.length * 5);
    for (int c = iis.read(); c != -1; c = iis.read()) {
        out.write(c);
    }
    in.close();
    iis.close();
    byte[] ret = out.toByteArray();
    out.close();
    return ret;
}
 
Example 16
Source File: INFLATE.java    From warp10-platform with Apache License 2.0 5 votes vote down vote up
@Override
public Object apply(WarpScriptStack stack) throws WarpScriptException {
  Object o = stack.pop();

  if (!(o instanceof byte[])) {
    throw new WarpScriptException(getName() + " operates on a byte array.");
  }
  
  byte[] data = (byte[]) o;
  
  ByteArrayInputStream bin = new ByteArrayInputStream(data);
  ByteArrayOutputStream decompressed = new ByteArrayOutputStream(data.length);
  
  byte[] buf = new byte[1024];
  
  try {
    InflaterInputStream in = new InflaterInputStream(bin);

    while(true) {
      int len = in.read(buf);
      
      if (len < 0) {
        break;
      }
      
      decompressed.write(buf, 0, len);
    }
    
    in.close();
  } catch (IOException ioe) {
    throw new WarpScriptException(getName() + " encountered an error while decompressing.", ioe);
  }
  
  stack.push(decompressed.toByteArray());
  
  return stack;
}
 
Example 17
Source File: j.java    From letv with Apache License 2.0 5 votes vote down vote up
public static byte[] b(byte[] bArr) {
    int i = 0;
    if (bArr == null) {
        return null;
    }
    InputStream byteArrayInputStream = new ByteArrayInputStream(bArr);
    InflaterInputStream inflaterInputStream = new InflaterInputStream(byteArrayInputStream);
    Object obj = new byte[0];
    Object obj2 = new byte[1024];
    while (true) {
        try {
            Object obj3;
            int read = inflaterInputStream.read(obj2);
            if (read > 0) {
                i += read;
                obj3 = new byte[i];
                System.arraycopy(obj, 0, obj3, 0, obj.length);
                System.arraycopy(obj2, 0, obj3, obj.length, read);
            } else {
                obj3 = obj;
            }
            if (read <= 0) {
                try {
                    byteArrayInputStream.close();
                    inflaterInputStream.close();
                    return obj3;
                } catch (IOException e) {
                    return null;
                }
            }
            obj = obj3;
        } catch (Exception e2) {
            return null;
        }
    }
}
 
Example 18
Source File: EncodingUtils.java    From ache with Apache License 2.0 5 votes vote down vote up
public static byte[] processDeflateEncoded(byte[] compressed, int sizeLimit) throws IOException {
    ByteArrayOutputStream outStream = new ByteArrayOutputStream(EXPECTED_DEFLATE_COMPRESSION_RATIO * compressed.length);

    // "true" because HTTP does not provide zlib headers
    Inflater inflater = new Inflater(true);
    InflaterInputStream inStream = new InflaterInputStream(new ByteArrayInputStream(compressed), inflater);

    byte[] buf = new byte[BUF_SIZE];
    int written = 0;
    while (true) {
        try {
            int size = inStream.read(buf);
            if (size <= 0) {
                break;
            }

            if ((written + size) > sizeLimit) {
                outStream.write(buf, 0, sizeLimit - written);
                break;
            }

            outStream.write(buf, 0, size);
            written += size;
        } catch (Exception e) {
            LOGGER.trace("Exception inflating content", e);
            break;
        }
    }

    safeClose(outStream);
    return outStream.toByteArray();
}
 
Example 19
Source File: RegisterBrokerBody.java    From rocketmq-4.3.0 with Apache License 2.0 5 votes vote down vote up
private static byte[] readBytes(InflaterInputStream inflaterInputStream, int length) throws IOException {
    byte[] buffer = new byte[length];
    int bytesRead = 0;
    while (bytesRead < length) {
        int len = inflaterInputStream.read(buffer, bytesRead, length - bytesRead);
        if (len == -1) {
            throw new IOException("End of compressed data has reached");
        } else {
            bytesRead += len;
        }
    }
    return buffer;
}
 
Example 20
Source File: RecordCompressor.java    From database with GNU General Public License v2.0 2 votes vote down vote up
/**
     * This decompresses data into a shared instance byte[]. If the byte[] runs
     * out of capacity then a new byte[] is allocated with twice the capacity,
     * the data is copied into new byte[], and decompression continues. The
     * shared instance byte[] is then returned to the caller. This approach is
     * suited to single-threaded processes that achieve a suitable buffer size
     * and then perform zero allocations thereafter.
     * 
     * @return A read-only view onto a shared buffer. The data between
     *         position() and limit() are the decompressed data. The contents of
     *         this buffer are valid only until the next compression or
     *         decompression request. The position will be zero. The limit will
     *         be the #of decompressed bytes.
     */
    protected ByteBuffer decompress(final InflaterInputStream iis) {

        int off = 0;
        
        try {

            while (true) { // use bulk I/O.

                int capacity = _buf.length - off;

                if (capacity == 0) {

                    final byte[] tmp = new byte[_buf.length * 2];

                    System.arraycopy(_buf, 0, tmp, 0, off);

                    _buf = tmp;

                    capacity = _buf.length - off;

                }

                final int nread = iis.read(_buf, off, capacity);

                if (nread == -1)
                    break; // EOF.

                off += nread;

            }

        }

        catch (IOException ex) {

            throw new RuntimeException(ex);

        }
//
//      /*
//      * make an exact fit copy of the uncompressed data and return it to
//      * the caller.
//      */
//
//     byte[] tmp = new byte[off];
//
//     System.arraycopy(_buf, 0, tmp, 0, off);
//
////     return tmp;
//        return ByteBuffer.wrap(tmp, 0, off);

        return ByteBuffer.wrap(_buf, 0, off).asReadOnlyBuffer();

    }