java.util.zip.InflaterInputStream#read ( )源码实例Demo

下面列出了java.util.zip.InflaterInputStream#read ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: QQRobot   文件: ZLibUtils.java
/** 
 * 解压缩 
 *  
 * @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();  
}
 
源代码2 项目: Nukkit   文件: ZlibOriginal.java
@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;
}
 
源代码3 项目: anthelion   文件: DeflateUtils.java
/**
 * 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();
}
 
源代码4 项目: Nukkit   文件: ZlibSingleThreadLowMem.java
@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;
}
 
源代码5 项目: Jupiter   文件: Zlib.java
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;
}
 
源代码6 项目: Nukkit   文件: ZlibThreadLocal.java
@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;
}
 
源代码7 项目: gcs   文件: PdfReader.java
/**
 * 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();
	}
}
 
源代码8 项目: Slide   文件: Usernotes.java
/**
 * 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;
    }
}
 
源代码9 项目: Nemisys   文件: Zlib.java
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;
}
 
源代码10 项目: android-project-wo2b   文件: ZLibUtils.java
/**
 * 解压缩
 * 
 * @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();
}
 
源代码11 项目: android_9.0.0_r45   文件: BlobBackupHelper.java
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;
}
 
源代码12 项目: rocketmq-4.3.0   文件: RegisterBrokerBody.java
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;
}
 
源代码13 项目: ache   文件: EncodingUtils.java
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();
}
 
源代码14 项目: letv   文件: j.java
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;
        }
    }
}
 
源代码15 项目: warp10-platform   文件: INFLATE.java
@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;
}
 
源代码16 项目: zstack   文件: Compresser.java
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;
}
 
源代码17 项目: rocketmq   文件: RegisterBrokerBody.java
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;
}
 
源代码18 项目: archive-patcher   文件: DeflateUncompressor.java
@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();
  }
}
 
源代码19 项目: archive-patcher   文件: DeflateCompressorTest.java
/**
 * 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();
}
 
源代码20 项目: database   文件: RecordCompressor.java
/**
     * 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();

    }
 
 同类方法