Java Code Examples for java.util.zip.Deflater#BEST_SPEED

The following examples show how to use java.util.zip.Deflater#BEST_SPEED . 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: Slice.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
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);
    }
}
 
Example 2
Source File: FileChangeEventBatchUtil.java    From codewind-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@SuppressWarnings("unused")
private final static byte[] compressString(String str) {

	ByteArrayOutputStream baos = new ByteArrayOutputStream();

	DeflaterOutputStream dos = new DeflaterOutputStream(baos, new Deflater(Deflater.BEST_SPEED));

	int uncompressedSize;
	try {
		byte[] strBytes = str.getBytes();
		dos.write(strBytes);
		dos.close();
		baos.close();
	} catch (IOException e) {
		log.logSevere("Unable to compress string contents", e, null);
		throw new RuntimeException(e);
	}

	byte[] result = baos.toByteArray();

	return result;
}
 
Example 3
Source File: Slice.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
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);
    }
}
 
Example 4
Source File: CompressionUtils.java    From rogue-cloud with Apache License 2.0 6 votes vote down vote up
private final static byte[] compressString(String str) {

		// If compression is disabled, then just return a byte array of a UTF-8 string
		if(!RCRuntime.ENABLE_DEFLATE_COMPRESSION) {
			return str.getBytes();
		}
		
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		
		DeflaterOutputStream dos = new DeflaterOutputStream(baos, new Deflater(Deflater.BEST_SPEED) );
		
		try {
			dos.write(str.getBytes());
			dos.close();
			baos.close();
		} catch (IOException e) {
			e.printStackTrace();
			log.severe("Exception on compress", e, null);
			throw new RuntimeException(e);
		}
		
		return baos.toByteArray();
	}
 
Example 5
Source File: DeflateCommonsCompressor.java    From yosegi with Apache License 2.0 5 votes vote down vote up
private int getCompressLevel( final CompressionPolicy compressionPolicy ) {
  switch ( compressionPolicy ) {
    case BEST_SPEED:
      return Deflater.BEST_SPEED;
    case SPEED:
      return 4;
    case DEFAULT:
      return 6;
    case BEST_COMPRESSION:
      return Deflater.BEST_COMPRESSION;
    default:
      return 6;
  }
}
 
Example 6
Source File: ZipFileObject.java    From rapidminer-studio with GNU Affero General Public License v3.0 5 votes vote down vote up
private int getZipCompressionLevel(FuzzyCompressionLevel compressionLevel) {
	switch (compressionLevel) {
		case BEST:
			return Deflater.BEST_COMPRESSION;
		case FASTEST:
			return Deflater.BEST_SPEED;
		case NONE:
			return Deflater.NO_COMPRESSION;
		case DEFAULT:
		default:
			return Deflater.DEFAULT_COMPRESSION;
	}
}
 
Example 7
Source File: ZipUTF8WriterBuilderImpl.java    From fastods with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Create a new builder
 */
public ZipUTF8WriterBuilderImpl() {
    this.level = Deflater.BEST_SPEED;
    this.writerBufferSize = ZipUTF8WriterBuilderImpl.DEFAULT_BUFFER;
    this.zipBufferSize = ZipUTF8WriterBuilderImpl.DEFAULT_BUFFER;
    this.xmlUtil = XMLUtil.create();
}
 
Example 8
Source File: CompressedWritable.java    From big-c with Apache License 2.0 5 votes vote down vote up
@Override
public final void write(DataOutput out) throws IOException {
  if (compressed == null) {
    ByteArrayOutputStream deflated = new ByteArrayOutputStream();
    Deflater deflater = new Deflater(Deflater.BEST_SPEED);
    DataOutputStream dout =
      new DataOutputStream(new DeflaterOutputStream(deflated, deflater));
    writeCompressed(dout);
    dout.close();
    deflater.end();
    compressed = deflated.toByteArray();
  }
  out.writeInt(compressed.length);
  out.write(compressed);
}
 
Example 9
Source File: TezUtils.java    From incubator-tez with Apache License 2.0 5 votes vote down vote up
private static byte[] compressBytesInflateDeflate(byte[] inBytes) {
  Deflater deflater = new Deflater(Deflater.BEST_SPEED);
  deflater.setInput(inBytes);
  ByteArrayOutputStream bos = new ByteArrayOutputStream(inBytes.length);
  deflater.finish();
  byte[] buffer = new byte[1024 * 8];
  while (!deflater.finished()) {
    int count = deflater.deflate(buffer);
    bos.write(buffer, 0, count);
  }
  byte[] output = bos.toByteArray();
  return output;
}
 
Example 10
Source File: CompressedWritable.java    From hadoop-gpu with Apache License 2.0 5 votes vote down vote up
public final void write(DataOutput out) throws IOException {
  if (compressed == null) {
    ByteArrayOutputStream deflated = new ByteArrayOutputStream();
    Deflater deflater = new Deflater(Deflater.BEST_SPEED);
    DataOutputStream dout =
      new DataOutputStream(new DeflaterOutputStream(deflated, deflater));
    writeCompressed(dout);
    dout.close();
    deflater.end();
    compressed = deflated.toByteArray();
  }
  out.writeInt(compressed.length);
  out.write(compressed);
}
 
Example 11
Source File: LobManager.java    From evosql with Apache License 2.0 5 votes vote down vote up
public void open() {

        lobBlockSize = database.logger.getLobBlockSize();
        cryptLobs    = database.logger.cryptLobs;
        compressLobs = database.logger.propCompressLobs;

        if (compressLobs || cryptLobs) {
            int largeBufferBlockSize = largeLobBlockSize + 4 * 1024;

            inflater   = new Inflater();
            deflater   = new Deflater(Deflater.BEST_SPEED);
            dataBuffer = new byte[largeBufferBlockSize];
        }

        if (database.getType() == DatabaseType.DB_RES) {
            lobStore = new LobStoreInJar(database, lobBlockSize);
        } else if (database.getType() == DatabaseType.DB_FILE) {
            lobStore = new LobStoreRAFile(database, lobBlockSize);

            if (!database.isFilesReadOnly()) {
                byteBuffer = new byte[lobBlockSize];

                initialiseLobSpace();
            }
        } else {
            lobStore   = new LobStoreMem(lobBlockSize);
            byteBuffer = new byte[lobBlockSize];

            initialiseLobSpace();
        }
    }
 
Example 12
Source File: TezUtilsInternal.java    From tez with Apache License 2.0 5 votes vote down vote up
private static byte[] compressBytesInflateDeflate(byte[] inBytes) {
  Deflater deflater = new Deflater(Deflater.BEST_SPEED);
  deflater.setInput(inBytes);
  NonSyncByteArrayOutputStream bos = new NonSyncByteArrayOutputStream(inBytes.length);
  deflater.finish();
  byte[] buffer = new byte[1024 * 8];
  while (!deflater.finished()) {
    int count = deflater.deflate(buffer);
    bos.write(buffer, 0, count);
  }
  byte[] output = bos.toByteArray();
  return output;
}
 
Example 13
Source File: WriteFxImageTests.java    From chart-fx with Apache License 2.0 5 votes vote down vote up
private static Stream<Arguments> testWritingImageByteBufferProvider() { // NOPMD -- is used in annotation /not detected by PMD
    List<Arguments> argumentList = new ArrayList<>();
    // N.B. limit to some selected sub-cases and not test all (full parameter space takes too much time for regular CI/CD checks
    for (int testImageID : new int[] { 0, 1, 2 }) {
        for (boolean encodeRGBA : new boolean[] { true, false }) {
            argumentList.add(Arguments.arguments(testImageID, true, encodeRGBA, Deflater.NO_COMPRESSION, FilterType.FILTER_NONE));
        }
        for (boolean allocateNewBuffer : new boolean[] { true, false }) {
            argumentList.add(Arguments.arguments(testImageID, allocateNewBuffer, true, Deflater.NO_COMPRESSION, FilterType.FILTER_NONE));
        }

        // loop through compression levels
        for (int compressionLevel : new int[] { Deflater.NO_COMPRESSION, Deflater.BEST_SPEED, Deflater.BEST_COMPRESSION }) {
            argumentList.add(Arguments.arguments(testImageID, true, true, compressionLevel, FilterType.FILTER_NONE));
        }

        // loop through PNG line filter settings
        for (FilterType filterType : new FilterType[] { FilterType.FILTER_NONE, FilterType.FILTER_PAETH }) {
            if (!FilterType.isValidStandard(filterType)) {
                continue;
            }
            argumentList.add(Arguments.arguments(testImageID, true, true, Deflater.NO_COMPRESSION, filterType));
        }
    }

    //return Stream.of("apple", "banana");
    return Stream.of(argumentList.toArray(new Arguments[0]));
}
 
Example 14
Source File: GzipCompressor.java    From yosegi with Apache License 2.0 5 votes vote down vote up
private int getCompressLevel( final CompressionPolicy compressionPolicy ) {
  switch ( compressionPolicy ) {
    case BEST_SPEED:
      return Deflater.BEST_SPEED;
    case SPEED:
      return 6 - 2;
    case DEFAULT:
      return 6;
    case BEST_COMPRESSION:
      return Deflater.BEST_COMPRESSION;
    default:
      return 6;
  }
}
 
Example 15
Source File: TezCommonUtils.java    From tez with Apache License 2.0 4 votes vote down vote up
@Private
public static Deflater newBestSpeedDeflater() {
  return new Deflater(Deflater.BEST_SPEED, NO_WRAP);
}
 
Example 16
Source File: CompressWrapper.java    From panama with MIT License 4 votes vote down vote up
public CompressWrapper() {
    this.wrapBuffer = new ByteArrayOutputStream();
    this.unwrapBuffer = new ByteArrayOutputStream();
    this.deflater = new Deflater(Deflater.BEST_SPEED);
    this.inflater = new Inflater();
}
 
Example 17
Source File: EntryAccounting.java    From buck with Apache License 2.0 4 votes vote down vote up
public long writeLocalFileHeader(OutputStream out) throws IOException {
  if (method == Method.DEFLATE && entry instanceof CustomZipEntry) {
    // See http://www.pkware.com/documents/casestudies/APPNOTE.TXT (section 4.4.4)
    // Essentially, we're about to set bits 1 and 2 to indicate to tools such as zipinfo which
    // level of compression we're using. If we've not set a compression level, then we're using
    // the default one, which is right. It turns out. For your viewing pleasure:
    //
    // +----------+-------+-------+
    // | Level    | Bit 1 | Bit 2 |
    // +----------+-------+-------+
    // | Fastest  |   0   |   1   |
    // | Normal   |   0   |   0   |
    // | Best     |   1   |   0   |
    // +----------+-------+-------+
    int level = ((CustomZipEntry) entry).getCompressionLevel();
    switch (level) {
      case Deflater.BEST_COMPRESSION:
        flags |= (1 << 1);
        break;

      case Deflater.BEST_SPEED:
        flags |= (1 << 2);
        break;
    }
  }

  if (requiresDataDescriptor()) {
    flags |= DATA_DESCRIPTOR_FLAG;
  }

  CountingOutputStream stream = new CountingOutputStream(out);
  ByteIo.writeInt(stream, ZipEntry.LOCSIG);

  boolean useZip64;
  if (!requiresDataDescriptor() && entry.getSize() >= ZipConstants.ZIP64_MAGICVAL) {
    useZip64 = true;
  } else {
    useZip64 = false;
  }

  ByteIo.writeShort(stream, getRequiredExtractVersion(useZip64));
  ByteIo.writeShort(stream, flags);
  ByteIo.writeShort(stream, getCompressionMethod());
  ByteIo.writeInt(stream, getTime());

  // If we don't know the size or CRC of the data in advance (such as when in deflate mode),
  // we write zeros now, and append the actual values (the data descriptor) after the entry
  // bytes has been fully written.
  if (requiresDataDescriptor()) {
    ByteIo.writeInt(stream, 0);
    ByteIo.writeInt(stream, 0);
    ByteIo.writeInt(stream, 0);
  } else {
    ByteIo.writeInt(stream, entry.getCrc());
    if (entry.getSize() >= ZipConstants.ZIP64_MAGICVAL) {
      ByteIo.writeInt(stream, ZipConstants.ZIP64_MAGICVAL);
      ByteIo.writeInt(stream, ZipConstants.ZIP64_MAGICVAL);
    } else {
      ByteIo.writeInt(stream, entry.getSize());
      ByteIo.writeInt(stream, entry.getSize());
    }
  }

  byte[] nameBytes = entry.getName().getBytes(Charsets.UTF_8);
  ByteIo.writeShort(stream, nameBytes.length);
  ByteIo.writeShort(stream, useZip64 ? ZipConstants.ZIP64_LOCHDR : 0);
  stream.write(nameBytes);
  if (useZip64) {
    ByteIo.writeShort(stream, ZipConstants.ZIP64_EXTID);
    ByteIo.writeShort(stream, 16);
    ByteIo.writeLong(stream, entry.getSize());
    ByteIo.writeLong(stream, entry.getSize());
  }

  return stream.getCount();
}
 
Example 18
Source File: CompressTest.java    From rogue-cloud with Apache License 2.0 3 votes vote down vote up
public static void main(String[] args) throws IOException {
		
		
		Random r = new Random();
		
		String str;
		
		long startTimeInNanos = System.nanoTime();
		
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		DeflaterOutputStream dos = new DeflaterOutputStream(baos, new Deflater(Deflater.BEST_SPEED) );
		
		long stringSize = 0;
		
		int x;
		for(x = 0; x < 1024 *1024*16; x++) {
		
			str = ""+r.nextInt();
			byte[] bytes = str.getBytes();
			stringSize += bytes.length;
			dos.write(bytes);
//			if(x % 1024 * 1024 == 0) {
//				System.out.println("hi: "+x);
//			}
			
		}
		dos.close();
		baos.close();
		
		long compressedSize = baos.size();
		System.out.println("compressedSize: "+compressedSize+" non-compressed: "+stringSize+" ratio: " + ((double)compressedSize/stringSize)   );
		
		long elapsedTime = TimeUnit.MILLISECONDS.convert(System.nanoTime() - startTimeInNanos, TimeUnit.NANOSECONDS);
		
		System.out.println(elapsedTime);
		
//		System.out.println(x / (elapsedTime/1000));
		
		
	}
 
Example 19
Source File: DeflaterSerializer.java    From moleculer-java with MIT License 2 votes vote down vote up
/**
 * Creates a JSON-based Serializer that compresses content above a specified
 * size (see the "compressAbove" parameter).
 * 
 * @param threshold
 *            Compress key and/or value above this size (BYTES), 0 = disable
 *            compression
 */
public DeflaterSerializer(int threshold) {
	this(null, threshold, Deflater.BEST_SPEED);
}
 
Example 20
Source File: DeflaterSerializer.java    From moleculer-java with MIT License 2 votes vote down vote up
/**
 * Creates a custom Serializer that compresses content above 1024 bytes size
 * with the compression level "1".
 * 
 * @param parent
 *            parent Serializer (eg. a JsonSerializer)
 */
public DeflaterSerializer(Serializer parent) {
	this(parent, 1024, Deflater.BEST_SPEED);
}