java.util.zip.DeflaterInputStream#java.util.zip.Deflater源码实例Demo

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

源代码1 项目: gcs   文件: FlateFilter.java
@Override
protected void encode(InputStream input, OutputStream encoded, COSDictionary parameters)
        throws IOException
{
    int compressionLevel = getCompressionLevel();
    Deflater deflater = new Deflater(compressionLevel);
    DeflaterOutputStream out = new DeflaterOutputStream(encoded, deflater);
    int amountRead;
    int mayRead = input.available();
    if (mayRead > 0)
    {
        byte[] buffer = new byte[Math.min(mayRead,BUFFER_SIZE)];
        while ((amountRead = input.read(buffer, 0, Math.min(mayRead,BUFFER_SIZE))) != -1)
        {
            out.write(buffer, 0, amountRead);
        }
    }
    out.close();
    encoded.flush();
    deflater.end();
}
 
源代码2 项目: pentaho-reporting   文件: ZipContentItem.java
public ZipContentItem( final ZipRepository repository, final ZipContentLocation parent, final String name ) {
  if ( repository == null ) {
    throw new NullPointerException();
  }
  if ( parent == null ) {
    throw new NullPointerException();
  }
  if ( name == null ) {
    throw new NullPointerException();
  }
  this.repository = repository;
  this.parent = parent;
  this.entryName = name;
  this.name = RepositoryUtilities.buildName( this, "/" );
  this.time = System.currentTimeMillis();
  this.comment = null;
  this.size = 0;
  this.rawData = EMPTY_BYTES;
  this.method = LibRepositoryBoot.ZIP_METHOD_DEFLATED;
  this.compression = Deflater.DEFAULT_COMPRESSION;
}
 
@Override
public byte[] compress(byte[] b, int offset, int length) {
    int len;
    int position = 0;

    if (_deflaterReset) {
        _deflater.reset();
    }
    _deflater.setInput(b, offset, length);
    while ((len = _deflater.deflate(_buffer, position, _capcacity - position, Deflater.SYNC_FLUSH)) > 0) {
        if ((position += len) == _capcacity) {
            _buffer = Arrays.copyOf(_buffer, _capcacity <<= 1);
        }
    }
    return Arrays.copyOf(_buffer, position - TAIL_LENGTH);
}
 
源代码4 项目: Smack   文件: Java7ZlibInputOutputStream.java
@Override
public OutputStream getOutputStream(OutputStream outputStream) {
    final int flushMethodInt;
    switch (flushMethod) {
    case SYNC_FLUSH:
        flushMethodInt = Deflater.SYNC_FLUSH;
        break;
    case FULL_FLUSH:
        flushMethodInt = Deflater.FULL_FLUSH;
        break;
    default:
        throw new AssertionError();
    }

    return new DeflaterOutputStream(outputStream, new Deflater(compressionLevel)) {
        @Override
        public void flush() throws IOException {
            int count;
            while ((count = def.deflate(buf, 0, buf.length, flushMethodInt)) > 0) {
                out.write(buf, 0, count);
            }
            super.flush();
        }
    };
}
 
源代码5 项目: learning-hadoop   文件: CassandraOutputData.java
/**
 * Compress a CQL query
 * 
 * @param queryStr
 *            the CQL query
 * @param compression
 *            compression option (GZIP is the only option - so far)
 * @return an array of bytes containing the compressed query
 */
public static byte[] compressQuery(String queryStr, Compression compression) {
	byte[] data = queryStr.getBytes(Charset
			.forName(CassandraColumnMetaData.UTF8));

	Deflater compressor = new Deflater();
	compressor.setInput(data);
	compressor.finish();

	ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
	byte[] buffer = new byte[1024];

	while (!compressor.finished()) {
		int size = compressor.deflate(buffer);
		byteArray.write(buffer, 0, size);
	}

	return byteArray.toByteArray();
}
 
源代码6 项目: xyz-hub   文件: Compression.java
/**
 *
 * @param bytearray
 * @return
 * @throws DataFormatException
 */
public static byte[] compressUsingInflate(byte[] bytearray) throws DataFormatException {
  if (bytearray == null) return new byte[0];

  try (final ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
    final Deflater deflater = new Deflater();
    final byte[] buff = new byte[1024];

    deflater.setInput(bytearray);
    deflater.finish();

    while (!deflater.finished()) {
      int count = deflater.deflate(buff);
      bos.write(buff, 0, count);
    }

    deflater.end();
    bos.flush();
    return bos.toByteArray();
  } catch (IOException e) {
    throw new DataFormatException(e.getMessage());
  }
}
 
源代码7 项目: openjdk-jdk9   文件: TIFFDeflater.java
public TIFFDeflater(String compressionType,
                    int compressionTagValue,
                    ImageWriteParam param,
                    int predictorValue) {
    super(compressionType, compressionTagValue, true);

    this.predictor = predictorValue;

    // Set the deflate level.
    int deflateLevel;
    if(param != null &&
       param.getCompressionMode() == ImageWriteParam.MODE_EXPLICIT) {
        float quality = param.getCompressionQuality();
        deflateLevel = (int)(1 + 8*quality);
    } else {
        deflateLevel = Deflater.DEFAULT_COMPRESSION;
    }

    this.deflater = new Deflater(deflateLevel);
}
 
源代码8 项目: Jupiter   文件: Zlib.java
public static byte[] deflate(byte[] data, int level) throws Exception {
    Deflater deflater = new Deflater(level);
    deflater.reset();
    deflater.setInput(data);
    deflater.finish();
    ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length);
    byte[] buf = new byte[1024];
    try {
        while (!deflater.finished()) {
            int i = deflater.deflate(buf);
            bos.write(buf, 0, i);
        }
    } finally {
        deflater.end();
        bos.close();
    }
    return bos.toByteArray();
}
 
源代码9 项目: TelePlus-Android   文件: Slice.java
private void storeData(ByteBuffer data) {
    try {
        final byte[] input = data.array();
        FileOutputStream fos = new FileOutputStream(file);

        final Deflater deflater = new Deflater(Deflater.BEST_SPEED, true);
        deflater.setInput(input, data.arrayOffset(), data.remaining());
        deflater.finish();

        byte[] buf = new byte[1024];
        while (!deflater.finished()) {
            int byteCount = deflater.deflate(buf);
            fos.write(buf, 0, byteCount);
        }
        deflater.end();

        fos.close();
    } catch (Exception e) {
        FileLog.e(e);
    }
}
 
源代码10 项目: gocd   文件: ZipUtilTest.java
@Test
void shouldZipFileContentsOnly() throws IOException {
    zipFile = zipUtil.zipFolderContents(srcDir, temporaryFolder.newFile(), Deflater.NO_COMPRESSION);
    assertThat(zipFile.isFile()).isTrue();

    zipUtil.unzip(zipFile, destDir);

    assertIsDirectory(new File(destDir, emptyDir.getName()));
    assertIsDirectory(new File(destDir, childDir1.getName()));

    File actual1 = new File(destDir, file1.getName());
    assertThat(actual1.isFile()).isTrue();
    assertThat(fileContent(actual1)).isEqualTo(fileContent(file1));

    File actual2 = new File(destDir, childDir1.getName() + File.separator + file2.getName());
    assertThat(actual2.isFile()).isTrue();
    assertThat(fileContent(actual2)).isEqualTo(fileContent(file2));
}
 
源代码11 项目: netty-4.1.22   文件: JdkZlibEncoder.java
/**
 * Creates a new zlib encoder with the specified {@code compressionLevel}
 * and the specified wrapper.使用指定的压缩级别和指定的包装器创建一个新的zlib编码器。
 *
 * @param compressionLevel
 *        {@code 1} yields the fastest compression and {@code 9} yields the
 *        best compression.  {@code 0} means no compression.  The default
 *        compression level is {@code 6}.
 *
 * @throws CompressionException if failed to initialize zlib
 */
public JdkZlibEncoder(ZlibWrapper wrapper, int compressionLevel) {
    if (compressionLevel < 0 || compressionLevel > 9) {
        throw new IllegalArgumentException(
                "compressionLevel: " + compressionLevel + " (expected: 0-9)");
    }
    if (wrapper == null) {
        throw new NullPointerException("wrapper");
    }
    if (wrapper == ZlibWrapper.ZLIB_OR_NONE) {
        throw new IllegalArgumentException(
                "wrapper '" + ZlibWrapper.ZLIB_OR_NONE + "' is not " +
                "allowed for compression.");
    }

    this.wrapper = wrapper;
    deflater = new Deflater(compressionLevel, wrapper != ZlibWrapper.ZLIB);
}
 
源代码12 项目: Nukkit   文件: ZlibOriginal.java
@Override
public byte[] deflate(byte[] data, int level) throws Exception {
    Deflater deflater = new Deflater(level);
    deflater.reset();
    deflater.setInput(data);
    deflater.finish();
    ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length);
    byte[] buf = new byte[1024];
    try {
        while (!deflater.finished()) {
            int i = deflater.deflate(buf);
            bos.write(buf, 0, i);
        }
    } finally {
        deflater.end();
    }
    return bos.toByteArray();
}
 
源代码13 项目: chart-fx   文件: WriteFxImageBenchmark.java
public static void testCompressionPerformance(final Image image, final String description) {
    LOGGER.atInfo().addArgument(description).log("Test compression level performance with image with {}");
    // precompute typical palette
    final PaletteQuantizer userPaletteRGBA = WriteFxImage.estimatePalette(image, true, DEFAULT_PALETTE_COLOR_COUNT);
    final PaletteQuantizer userPaletteRGB = WriteFxImage.estimatePalette(image, false, DEFAULT_PALETTE_COLOR_COUNT);

    for (final boolean alpha : new boolean[] { true, false }) {
        LOGGER.atInfo().addArgument(alpha ? "with" : "without").log("{} alpha channel");
        for (int compressionLevel = Deflater.NO_COMPRESSION; compressionLevel <= Deflater.BEST_COMPRESSION; compressionLevel++) {
            writeFxImage(image, alpha, true, compressionLevel, OLDREF);
            writeFxImage(image, alpha, true, compressionLevel, NEWREF);
            // compute palette on-the-fly
            writeFxImage(image, alpha, true, compressionLevel, PALETTE);
            // use pre-computed palette
            writeFxImage(image, alpha, true, compressionLevel, PALETTE, alpha ? userPaletteRGBA : userPaletteRGB);
            LOGGER.atInfo().log(" "); // deliberatly empty line for better readability
        }
    }
}
 
源代码14 项目: jstarcraft-core   文件: PressUtility.java
/**
 * 按照指定的级别压缩指定的数据
 * 
 * @param datas
 * @param level
 * @return
 */
public static byte[] zip(byte[] datas, int level) {
	if (level < Deflater.NO_COMPRESSION || level > Deflater.BEST_COMPRESSION) {
		String message = StringUtility.format("非法的压缩等级[{}]", level);
		LOGGER.error(message);
		throw new IllegalArgumentException(message);
	}
	Deflater deflater = new Deflater(level);
	deflater.setInput(datas);
	deflater.finish();
	try (ByteArrayOutputStream stream = new ByteArrayOutputStream(BUFFER_SIZE)) {
		byte[] bytes = new byte[BUFFER_SIZE];
		while (!deflater.finished()) {
			int count = deflater.deflate(bytes);
			stream.write(bytes, 0, count);
		}
		deflater.end();
		return stream.toByteArray();
	} catch (IOException exception) {
		throw new IllegalStateException("压缩异常", exception);
	}
}
 
源代码15 项目: j2objc   文件: OldAndroidDeflateTest.java
private void bigTest(int step, int expectedAdler)
        throws UnsupportedEncodingException, DataFormatException {
    byte[] input = new byte[128 * 1024];
    byte[] comp = new byte[128 * 1024 + 512];
    byte[] output = new byte[128 * 1024 + 512];
    Inflater inflater = new Inflater(false);
    Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION, false);

    createSample(input, step);

    compress(deflater, input, comp);
    expand(inflater, comp, (int) deflater.getBytesWritten(), output);

    assertEquals(inflater.getBytesWritten(), input.length);
    assertEquals(deflater.getAdler(), inflater.getAdler());
    assertEquals(deflater.getAdler(), expectedAdler);
}
 
源代码16 项目: openjdk-jdk9   文件: ZipPlugin.java
static byte[] compress(byte[] bytesIn) {
    Deflater deflater = new Deflater();
    deflater.setInput(bytesIn);
    ByteArrayOutputStream stream = new ByteArrayOutputStream(bytesIn.length);
    byte[] buffer = new byte[1024];

    deflater.finish();
    while (!deflater.finished()) {
        int count = deflater.deflate(buffer);
        stream.write(buffer, 0, count);
    }

    try {
        stream.close();
    } catch (IOException ex) {
        return bytesIn;
    }

    byte[] bytesOut = stream.toByteArray();
    deflater.end();

    return bytesOut;
}
 
源代码17 项目: gemfirexd-oss   文件: CliUtil.java
public static DeflaterInflaterData compressBytes(byte[] input) {
    Deflater compresser = new Deflater();
    compresser.setInput(input);
    compresser.finish();
    byte[] buffer = new byte[100];
    byte[] result = new byte[0];
    int compressedDataLength = 0;
    int totalCompressedDataLength = 0;
    do {
      byte[] newResult = new byte[result.length + buffer.length];
      System.arraycopy(result, 0, newResult, 0, result.length);

      compressedDataLength = compresser.deflate(buffer);
      totalCompressedDataLength += compressedDataLength;
//      System.out.println(compressedDataLength);
//      System.out.println("uc: b  "+buffer.length);
//      System.out.println("uc: r  "+result.length);
//      System.out.println("uc: nr "+newResult.length);
//      System.out.println();
      System.arraycopy(buffer, 0, newResult, result.length, buffer.length);
      result = newResult;
    } while (compressedDataLength != 0);
    return new DeflaterInflaterData(totalCompressedDataLength, result);
  }
 
源代码18 项目: Telegram-FOSS   文件: Slice.java
private void storeData(ByteBuffer data) {
    try {
        final byte[] input = data.array();
        FileOutputStream fos = new FileOutputStream(file);

        final Deflater deflater = new Deflater(Deflater.BEST_SPEED, true);
        deflater.setInput(input, data.arrayOffset(), data.remaining());
        deflater.finish();

        byte[] buf = new byte[1024];
        while (!deflater.finished()) {
            int byteCount = deflater.deflate(buf);
            fos.write(buf, 0, byteCount);
        }
        deflater.end();

        fos.close();
    } catch (Exception e) {
        FileLog.e(e);
    }
}
 
源代码19 项目: pentaho-reporting   文件: ZipContentItem.java
public ZipContentItem( final String name,
                       final ZipRepository repository,
                       final ZipContentLocation parent ) {
  if ( name == null ) {
    throw new NullPointerException();
  }
  if ( repository == null ) {
    throw new NullPointerException();
  }
  if ( parent == null ) {
    throw new NullPointerException();
  }

  this.time = System.currentTimeMillis();
  this.name = name;
  this.repository = repository;
  this.parent = parent;
  this.contentId = RepositoryUtilities.buildName( this, "/" );
  this.newItem = true;
  this.method = LibRepositoryBoot.ZIP_METHOD_DEFLATED;
  this.compression = Deflater.DEFAULT_COMPRESSION;
}
 
源代码20 项目: sambox   文件: FlateFilter.java
@Override
public void encode(InputStream input, OutputStream encoded, COSDictionary parameters)
        throws IOException
{
    int compressionLevel = getCompressionLevel();
    Deflater deflater = new Deflater(compressionLevel);
    try (DeflaterOutputStream out = new DeflaterOutputStream(encoded, deflater))
    {
        int amountRead;
        int mayRead = input.available();
        if (mayRead > 0)
        {
            byte[] buffer = new byte[Math.min(mayRead, BUFFER_SIZE)];
            while ((amountRead = input.read(buffer, 0, Math.min(mayRead, BUFFER_SIZE))) != -1)
            {
                out.write(buffer, 0, amountRead);
            }
        }
    }
    encoded.flush();
}
 
源代码21 项目: opencards   文件: StringCompressUtils.java
private static String compress2(String s) {
    Deflater defl = new Deflater(Deflater.BEST_COMPRESSION);
    defl.setInput(s.getBytes());
    defl.finish();
    boolean done = false;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    while (!done) {
        byte[] buf = new byte[256];
        int bufnum = defl.deflate(buf);
        bos.write(buf, 0, bufnum);
        if (bufnum < buf.length)
            done = true;
    }
    try {
        bos.flush();
        bos.close();
    } catch (IOException ioe) {
        System.err.println(ioe.toString());
    }
    return toHexString(bos.toByteArray());
}
 
源代码22 项目: netty4.0.27Learn   文件: JdkZlibEncoder.java
/**
 * Creates a new zlib encoder with the specified {@code compressionLevel}
 * and the specified wrapper.
 *
 * @param compressionLevel
 *        {@code 1} yields the fastest compression and {@code 9} yields the
 *        best compression.  {@code 0} means no compression.  The default
 *        compression level is {@code 6}.
 *
 * @throws CompressionException if failed to initialize zlib
 */
public JdkZlibEncoder(ZlibWrapper wrapper, int compressionLevel) {
    if (compressionLevel < 0 || compressionLevel > 9) {
        throw new IllegalArgumentException(
                "compressionLevel: " + compressionLevel + " (expected: 0-9)");
    }
    if (wrapper == null) {
        throw new NullPointerException("wrapper");
    }
    if (wrapper == ZlibWrapper.ZLIB_OR_NONE) {
        throw new IllegalArgumentException(
                "wrapper '" + ZlibWrapper.ZLIB_OR_NONE + "' is not " +
                "allowed for compression.");
    }

    this.wrapper = wrapper;
    deflater = new Deflater(compressionLevel, wrapper != ZlibWrapper.ZLIB);
}
 
源代码23 项目: xDrip-plus   文件: JoH.java
public static String compressString(String source) {
    try {

        Deflater deflater = new Deflater();
        deflater.setInput(source.getBytes(Charset.forName("UTF-8")));
        deflater.finish();

        byte[] buf = new byte[source.length() + 256];
        int count = deflater.deflate(buf);
        // check count
        deflater.end();
        return Base64.encodeToString(buf, 0, count, Base64.NO_WRAP);
    } catch (Exception e) {
        return null;
    }
}
 
源代码24 项目: Tomcat7.0.67   文件: FlushableGZIPOutputStream.java
@Override
public synchronized void flush() throws IOException {
    if (hasLastByte) {
        // - do not allow the gzip header to be flushed on its own
        // - do not do anything if there is no data to send

        // trick the deflater to flush
        /**
         * Now this is tricky: We force the Deflater to flush its data by
         * switching compression level. As yet, a perplexingly simple workaround
         * for
         * http://developer.java.sun.com/developer/bugParade/bugs/4255743.html
         */
        if (!def.finished()) {
            def.setLevel(Deflater.NO_COMPRESSION);
            flushLastByte();
            flagReenableCompression = true;
        }
    }
    out.flush();
}
 
源代码25 项目: CodeChickenLib   文件: PacketCustom.java
/**
 * Compresses the payload ByteBuf after the type byte
 */
private void do_compress() {
    Deflater deflater = new Deflater();
    try {
        readerIndex(1);
        int len = readableBytes();
        deflater.setInput(array(), readerIndex(), len);
        deflater.finish();
        byte[] out = new byte[len];
        int clen = deflater.deflate(out);
        if (clen >= len - 5 || !deflater.finished())//not worth compressing, gets larger
            return;
        clear();
        writeByte(type | 0x80);
        writeVarInt(len);
        writeByteArray(out);
    } catch (Exception e) {
        throw new EncoderException(e);
    } finally {
        readerIndex(0);
        deflater.end();
    }
}
 
@Override
protected ByteBuf getPayload(ChannelHandlerContext ctx, Batch batch) throws IOException {
    ByteBuf payload = super.getPayload(ctx, batch);
    try {
        try (ByteBufOutputStream output = new ByteBufOutputStream(ctx.alloc().buffer())) {
            DeflaterOutputStream outputDeflater = new DeflaterOutputStream(output, new Deflater());
            byte[] chunk = new byte[payload.readableBytes()];
            payload.readBytes(chunk);
            outputDeflater.write(chunk);
            outputDeflater.close();
            ByteBuf content = ctx.alloc().buffer(output.writtenBytes());
            content.writeByte(batch.getProtocol());
            content.writeByte('C');

            content.writeInt(output.writtenBytes());
            content.writeBytes(output.buffer());
            return content;
        }
    } finally {
        payload.release();
    }

}
 
源代码27 项目: netcdf-java   文件: TestGribCompressByBit.java
byte[] compress(byte[] data) {
  Deflater compresser = new Deflater(deflate_level);
  compresser.setInput(data);
  compresser.finish();

  byte[] output = new byte[data.length * 2];
  int size = compresser.deflate(output);
  compresser.end();

  // copy just the resulting bytes
  byte[] out = new byte[size];
  System.arraycopy(output, 0, out, 0, size);
  return out;
}
 
源代码28 项目: Angelia-core   文件: CompressionManager.java
/**
 * Compresses the given data with zlib (as used by minecrafts protocol)
 * 
 * @param data Data to compress
 * @return Compressed data
 * @throws IOException In case something goes wrong with the stream internally
 *                     used
 */
public static byte[] compressZLib(byte[] data) throws IOException {
	Deflater deflater = new Deflater();
	deflater.setInput(data);
	ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
	deflater.finish();
	byte[] buffer = new byte[1024];
	while (!deflater.finished()) {
		int count = deflater.deflate(buffer);
		outputStream.write(buffer, 0, count);
	}
	outputStream.close();
	byte[] output = outputStream.toByteArray();
	return output;
}
 
源代码29 项目: Flashtool   文件: RawTAJob.java
public void createFTA(String partition, String folder) {
  	File tadd = new File(folder+File.separator+tafilename);
  	String timestamp = OS.getTimeStamp();
File fta = new File(folder+File.separator+timestamp+".fta");
byte buffer[] = new byte[10240];
StringBuffer sbuf = new StringBuffer();
sbuf.append("Manifest-Version: 1.0\n");
sbuf.append("Created-By: FlashTool\n");
sbuf.append("serial: "+Devices.getCurrent().getSerial()+"\n");
sbuf.append("build: "+Devices.getCurrent().getBuildId()+"\n");
sbuf.append("partition: "+partition+"\n");
sbuf.append("md5: "+OS.getMD5(tadd).toUpperCase()+"\n");
sbuf.append("timestamp: "+timestamp+"\n");
try {
	Manifest manifest = new Manifest(new ByteArrayInputStream(sbuf.toString().getBytes("UTF-8")));
    FileOutputStream stream = new FileOutputStream(fta);
    JarOutputStream out = new JarOutputStream(stream, manifest);
    out.setLevel(Deflater.BEST_SPEED);
	logger.info("Creating backupset bundle");
    JarEntry jarAdd = new JarEntry(tafilename);
       out.putNextEntry(jarAdd);
       InputStream in = new FileInputStream(tadd);
       while (true) {
         int nRead = in.read(buffer, 0, buffer.length);
         if (nRead <= 0)
           break;
         out.write(buffer, 0, nRead);
       }
       in.close();
       out.flush();
       out.close();
       stream.flush();
    stream.close();
    tadd.delete();
    logger.info("Bundle "+fta.getAbsolutePath()+" creation finished");
}
catch (Exception e) {
	logger.error(e.getMessage());
}
  }
 
源代码30 项目: bluemix-parking-meter   文件: Spdy3.java
Writer(OutputStream out, boolean client) {
  this.out = new DataOutputStream(out);
  this.client = client;

  Deflater deflater = new Deflater();
  deflater.setDictionary(DICTIONARY);
  nameValueBlockBuffer = new ByteArrayOutputStream();
  nameValueBlockOut = new DataOutputStream(
      Platform.get().newDeflaterOutputStream(nameValueBlockBuffer, deflater, true));
}