示例#1
0
  /**
   * Decompresses a compressed byte array.
   *
   * @param input The byte array to be decompressed.
   * @return The byte array in its decompressed, readable form.
   */
  public static byte[] decompress(byte[] input) {
    try {
      final Inflater inflater = new Inflater();
      final ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(input.length);
      final byte[] buffer = new byte[1024];
      inflater.setInput(input);

      while (!inflater.finished()) {
        final int count = inflater.inflate(buffer);
        byteOutput.write(buffer, 0, count);
      }

      byteOutput.close();
      return byteOutput.toByteArray();
    } catch (final DataFormatException e) {
      RadixCore.getInstance().quitWithException("Error decompressing byte array.", e);
      return null;
    } catch (final IOException e) {
      RadixCore.getInstance().quitWithException("Error decompressing byte array.", e);
      return null;
    }
  }
示例#2
0
  /**
   * Compresses the data in a byte array.
   *
   * @param input The byte array to be compressed.
   * @return The byte array in its compressed form.
   */
  public static byte[] compress(byte[] input) {
    try {
      final Deflater deflater = new Deflater();
      deflater.setLevel(Deflater.BEST_COMPRESSION);
      deflater.setInput(input);

      final ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(input.length);
      deflater.finish();

      final byte[] buffer = new byte[1024];

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

      byteOutput.close();
      return byteOutput.toByteArray();
    } catch (final IOException e) {
      RadixCore.getInstance().quitWithException("Error compressing byte array.", e);
      return null;
    }
  }