public int encode(
      byte[] b, int off, int width, int height, int[] bitsPerSample, int scanlineStride)
      throws IOException {

    int inputSize = height * scanlineStride;
    int blocks = (inputSize + 32767) / 32768;

    // Worst case for Zlib deflate is input size + 5 bytes per 32k
    // block, plus 6 header bytes
    byte[] compData = new byte[inputSize + 5 * blocks + 6];

    int numCompressedBytes = 0;
    if (predictor == BaselineTIFFTagSet.PREDICTOR_HORIZONTAL_DIFFERENCING) {
      int samplesPerPixel = bitsPerSample.length;
      int bitsPerPixel = 0;
      for (int i = 0; i < samplesPerPixel; i++) {
        bitsPerPixel += bitsPerSample[i];
      }
      int bytesPerRow = (bitsPerPixel * width + 7) / 8;
      byte[] rowBuf = new byte[bytesPerRow];

      int maxRow = height - 1;
      for (int i = 0; i < height; i++) {
        // Cannot modify b[] in place as it might be a data
        // array from the image being written so make a copy.
        System.arraycopy(b, off, rowBuf, 0, bytesPerRow);
        for (int j = bytesPerRow - 1; j >= samplesPerPixel; j--) {
          rowBuf[j] -= rowBuf[j - samplesPerPixel];
        }

        deflater.setInput(rowBuf);
        if (i == maxRow) {
          deflater.finish();
        }

        int numBytes = 0;
        while ((numBytes =
                deflater.deflate(
                    compData, numCompressedBytes, compData.length - numCompressedBytes))
            != 0) {
          numCompressedBytes += numBytes;
        }

        off += scanlineStride;
      }
    } else {
      deflater.setInput(b, off, height * scanlineStride);
      deflater.finish();

      numCompressedBytes = deflater.deflate(compData);
    }

    deflater.reset();

    stream.write(compData, 0, numCompressedBytes);

    return numCompressedBytes;
  }
 /**
  * Writes next block of compressed data to the output stream.
  *
  * @throws IOException on error
  */
 protected final void deflate() throws IOException {
   int len = def.deflate(buf, 0, buf.length);
   if (len > 0) {
     writeOut(buf, 0, len);
     written += len;
   }
 }
Esempio n. 3
0
  @Deprecated
  private static synchronized byte[] zipFileName(String fileName) throws IOException {
    Deflater deflater = new Deflater();

    byte[] bytes = fileName.getBytes();
    deflater.setInput(bytes);

    deflater.finish();

    ByteArrayOutputStream bos = new ByteArrayOutputStream(bytes.length);
    byte[] buffer = new byte[1024];

    while (!deflater.finished()) {
      int bytesCompressed = deflater.deflate(buffer);
      bos.write(buffer, 0, bytesCompressed);
    }
    bos.close();

    int count = 0;
    for (int i = 0; i < buffer.length; i++) {
      if (buffer[i] != 0) count++;
    }

    byte[] result = new byte[count];
    for (int i = 0; i < result.length; i++) {
      result[i] = buffer[i];
    }

    return result;
  }
 /**
  * deflater 壓縮
  *
  * @param value
  * @return
  */
 public static byte[] deflater(byte[] value) {
   byte[] result = new byte[0];
   Deflater deflater = null;
   ByteArrayOutputStream out = null;
   //
   try {
     deflater = new Deflater();
     deflater.setLevel(Deflater.BEST_SPEED); // fast
     deflater.setInput(value);
     deflater.finish();
     //
     out = new ByteArrayOutputStream();
     byte[] buff = new byte[1024];
     while (!deflater.finished()) {
       int count = deflater.deflate(buff);
       out.write(buff, 0, count);
     }
     //
     result = out.toByteArray();
   } catch (Exception ex) {
     ex.printStackTrace();
   } finally {
     if (deflater != null) {
       deflater.end(); // 需end,不然會有oom(約5000次時)
     }
     IoHelper.close(out);
   }
   return result;
 }
Esempio n. 5
0
  public static byte[] deflateObject(byte[] uncompressedData) throws Exception {
    // Create the compressor with highest level of compression
    Deflater deflater = new Deflater();

    deflater.setLevel(Deflater.BEST_COMPRESSION);

    deflater.setInput(uncompressedData);
    deflater.finish();

    // Create an expandable byte array to hold the compressed data.
    // You cannot use an array that's the same size as the orginal because
    // there is no guarantee that the compressed data will be smaller than
    // the uncompressed data.
    ByteArrayOutputStream bos = new ByteArrayOutputStream(uncompressedData.length);

    // Compress the data
    byte[] buf = new byte[1024];
    try {
      while (!deflater.finished()) {
        int count = deflater.deflate(buf);
        bos.write(buf, 0, count);
      }
    } finally {
      try {
        bos.close();
      } catch (IOException io) {
      }
    }

    // Get the compressed data
    return bos.toByteArray();
  }
Esempio n. 6
0
  @Test
  public void testInflaterWriter() throws Exception {
    String inputString = "blahblahblah??blahblahblahblahblah??blablahblah??blablahblah??bla";
    byte[] input = inputString.getBytes("UTF-8");
    byte[] output = new byte[30];
    Deflater compresser = new Deflater();
    compresser.setInput(input);
    compresser.finish();
    int compressedDataLength = compresser.deflate(output);

    byte[] zipBytes = new byte[compressedDataLength];

    System.arraycopy(output, 0, zipBytes, 0, compressedDataLength);
    ByteArrayInputStream byteInput = new ByteArrayInputStream(zipBytes);

    ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
    InflaterWriter writer = new InflaterWriter(byteOutput);

    byte[] zipBuffer = new byte[12];

    int n = byteInput.read(zipBuffer);
    while (n > 0) {
      writer.write(zipBuffer, 0, n);
      n = byteInput.read(zipBuffer);
    }

    writer.close();

    byte[] outcome = byteOutput.toByteArray();
    String outStr = new String(outcome);

    assertEquals(inputString, outStr);
  }
Esempio n. 7
0
  @Test
  public void testDeflaterReader() throws Exception {
    String inputString = "blahblahblah??blahblahblahblahblah??blablahblah??blablahblah??bla";
    byte[] input = inputString.getBytes("UTF-8");

    ByteArrayInputStream inputStream = new ByteArrayInputStream(input);

    AtomicLong counter = new AtomicLong(0);
    DeflaterReader reader = new DeflaterReader(inputStream, counter);

    ArrayList<Integer> zipHolder = new ArrayList<Integer>();
    int b = reader.read();

    while (b != -1) {
      zipHolder.add(b);
      b = reader.read();
    }

    assertEquals(input.length, counter.get());

    byte[] allCompressed = new byte[zipHolder.size()];
    for (int i = 0; i < allCompressed.length; i++) {
      allCompressed[i] = (byte) zipHolder.get(i).intValue();
    }

    byte[] output = new byte[30];
    Deflater compresser = new Deflater();
    compresser.setInput(input);
    compresser.finish();
    int compressedDataLength = compresser.deflate(output);

    compareByteArray(allCompressed, output, compressedDataLength);
    reader.close();
  }
 @Override
 public void encode(ChannelBuffer compressed) {
   while (!compressor.needsInput()) {
     int numBytes = compressor.deflate(out, 0, out.length, Deflater.SYNC_FLUSH);
     compressed.writeBytes(out, 0, numBytes);
   }
 }
Esempio n. 9
0
  @Test
  public void testInflaterReader() throws Exception {
    String inputString = "blahblahblah??blahblahblahblahblah??blablahblah??blablahblah??bla";
    byte[] input = inputString.getBytes("UTF-8");
    byte[] output = new byte[30];
    Deflater compresser = new Deflater();
    compresser.setInput(input);
    compresser.finish();
    int compressedDataLength = compresser.deflate(output);

    byte[] zipBytes = new byte[compressedDataLength];

    System.arraycopy(output, 0, zipBytes, 0, compressedDataLength);
    ByteArrayInputStream byteInput = new ByteArrayInputStream(zipBytes);

    InflaterReader inflater = new InflaterReader(byteInput);
    ArrayList<Integer> holder = new ArrayList<Integer>();
    int read = inflater.read();

    while (read != -1) {
      holder.add(read);
      read = inflater.read();
    }

    byte[] result = new byte[holder.size()];

    for (int i = 0; i < result.length; i++) {
      result[i] = holder.get(i).byteValue();
    }

    String txt = new String(result);

    assertEquals(inputString, txt);
    inflater.close();
  }
Esempio n. 10
0
 private byte[] compressData(byte[] rawBytes, AtomicInteger size) {
   deflater.reset();
   deflater.setInput(rawBytes);
   deflater.finish();
   byte[] compressedData = new byte[rawBytes.length];
   size.set(deflater.deflate(compressedData));
   return compressedData;
 }
Esempio n. 11
0
 public static byte[] compress(byte data[], String signature, String version) {
   byte header[] = ArrayUtil.mergeArrays(signature.getBytes(), version.getBytes());
   header = ArrayUtil.mergeArrays(header, Byteconvert.convertBack(data.length));
   byte result[] = ArrayUtil.resizeArray(header, data.length * 2);
   Deflater deflater = new Deflater();
   deflater.setInput(data);
   deflater.finish();
   int clength = deflater.deflate(result, 12, result.length - 12);
   return ArrayUtil.getSubArray(result, 0, clength + 12);
 }
 private static void deflate(TemporaryBuffer.Heap tinyPack, final byte[] content)
     throws IOException {
   final Deflater deflater = new Deflater();
   final byte[] buf = new byte[128];
   deflater.setInput(content, 0, content.length);
   deflater.finish();
   do {
     final int n = deflater.deflate(buf, 0, buf.length);
     if (n > 0) tinyPack.write(buf, 0, n);
   } while (!deflater.finished());
 }
 private void deflate() {
   Deflater deflater = new Deflater(-1);
   try {
     deflater.setInput(this.field_149278_f, 0, this.field_149278_f.length);
     deflater.finish();
     byte[] deflated = new byte[this.field_149278_f.length];
     this.field_149285_h = deflater.deflate(deflated);
     this.field_149281_e = deflated;
   } finally {
     deflater.end();
   }
 }
Esempio n. 14
0
  public static byte[] zlibCompress(String what) throws UnsupportedEncodingException {
    byte[] input = what.getBytes("UTF-8");

    // Compress the bytes
    byte[] output = new byte[4096];
    Deflater compresser = new Deflater();
    compresser.setInput(input);
    compresser.finish();
    compresser.deflate(output);

    return output;
  }
Esempio n. 15
0
  private void flushInputToStream(final OutputStream pStream) throws IOException {
    System.out.println("DeflateEncoder.flushInputToStream");

    if (deflater.needsInput()) {
      System.out.println("Foo");
    }

    while (!deflater.needsInput()) {
      int deflated = deflater.deflate(buffer, 0, buffer.length);
      pStream.write(buffer, 0, deflated);
      System.out.println("flushed " + deflated);
    }
  }
Esempio n. 16
0
 public static byte[] compress(byte[] input) throws IOException {
   Deflater compressor = new Deflater();
   compressor.setLevel(Deflater.BEST_SPEED);
   compressor.setInput(input);
   compressor.finish();
   ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length / 10);
   byte[] buf = new byte[input.length / 10];
   while (!compressor.finished()) {
     int count = compressor.deflate(buf);
     bos.write(buf, 0, count);
   }
   bos.close();
   compressor.end();
   return bos.toByteArray();
 }
Esempio n. 17
0
 @Override
 public byte[] compress(final byte[] data, final CodecOptions options) throws FormatException {
   if (data == null || data.length == 0) throw new IllegalArgumentException("No data to compress");
   final Deflater deflater = new Deflater();
   deflater.setInput(data);
   deflater.finish();
   final byte[] buf = new byte[8192];
   final ByteVector bytes = new ByteVector();
   int r = 0;
   // compress until eof reached
   while ((r = deflater.deflate(buf, 0, buf.length)) > 0) {
     bytes.add(buf, 0, r);
   }
   return bytes.toByteArray();
 }
Esempio n. 18
0
  public final byte[] s2b(String data) {
    // return str.getBytes();
    // Create the compressor with highest level of compression

    byte[] retval = org.apache.axis.encoding.Base64.decode(data);
    byte[] compressedBuff = org.apache.axis.encoding.Base64.decode(data);
    int count = compressor.deflate(data.getBytes());
    compressedBuff = org.apache.axis.encoding.Base64.decode(new String(compressedBuff));
    int diff = retval.length - compressedBuff.length;
    log.trace(
        "diff:" + (diff) + "   (((" + diffTotal + "/" + sizeTotal + "))     count = " + count);
    sizeTotal += retval.length;
    diffTotal += diff;

    return retval;
  }
Esempio n. 19
0
 public byte[] compress(final byte[] bytes) {
   try {
     deflater.setInput(bytes);
     deflater.finish();
     int len = deflater.deflate(buffer);
     byte[] compressedBytes = new byte[len];
     System.arraycopy(buffer, 0, compressedBytes, 0, len);
     return compressedBytes;
   } finally {
     try {
       deflater.reset();
     } catch (Exception e) {
       logger.warn("deflater.reset failed", e);
     }
   }
 }
Esempio n. 20
0
  private static byte[] deflate(byte[] input) {

    Deflater compressor = new Deflater(Deflater.DEFLATED);
    compressor.setInput(input);
    compressor.finish();

    ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);

    byte[] buf = new byte[CHUNKSIZE];

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

    return bos.toByteArray();
  }
Esempio n. 21
0
  private void deflate() {
    byte[] data = new byte[maxLen];
    int offset = 0;
    for (int x = 0; x < field_73584_f.length; x++) {
      System.arraycopy(field_73584_f[x], 0, data, offset, field_73584_f[x].length);
      offset += field_73584_f[x].length;
    }

    Deflater deflater = new Deflater(-1);

    try {
      deflater.setInput(data, 0, maxLen);
      deflater.finish();
      byte[] deflated = new byte[maxLen];
      this.dataLength = deflater.deflate(deflated);
      this.chunkDataBuffer = deflated;
    } finally {
      deflater.end();
    }
  }
Esempio n. 22
0
  public Packet51MapChunk(Chunk par1Chunk, boolean par2, int par3) {
    this.isChunkDataPacket = true;
    this.xCh = par1Chunk.xPosition;
    this.zCh = par1Chunk.zPosition;
    this.includeInitialize = par2;
    Packet51MapChunkData var4 = func_73594_a(par1Chunk, par2, par3);
    Deflater var5 = new Deflater(-1);
    this.yChMax = var4.field_74581_c;
    this.yChMin = var4.field_74580_b;

    try {
      this.field_73596_g = var4.field_74582_a;
      var5.setInput(var4.field_74582_a, 0, var4.field_74582_a.length);
      var5.finish();
      this.chunkData = new byte[var4.field_74582_a.length];
      this.tempLength = var5.deflate(this.chunkData);
    } finally {
      var5.end();
    }
  }
  /* (non-Javadoc)
   * @see org.eclipse.jetty.websocket.AbstractExtension#addFrame(byte, byte, byte[], int, int)
   */
  @Override
  public void addFrame(byte flags, byte opcode, byte[] content, int offset, int length)
      throws IOException {
    if (getConnection().isControl(opcode) || length < _minLength) {
      super.addFrame(clearFlag(flags, 1), opcode, content, offset, length);
      return;
    }

    // prepare the uncompressed input
    _deflater.reset();
    _deflater.setInput(content, offset, length);
    _deflater.finish();

    // prepare the output buffer
    byte[] out = new byte[length];
    int out_offset = 0;

    // write the uncompressed length
    if (length > 0xffff) {
      out[out_offset++] = 0x7f;
      out[out_offset++] = (byte) 0;
      out[out_offset++] = (byte) 0;
      out[out_offset++] = (byte) 0;
      out[out_offset++] = (byte) 0;
      out[out_offset++] = (byte) ((length >> 24) & 0xff);
      out[out_offset++] = (byte) ((length >> 16) & 0xff);
      out[out_offset++] = (byte) ((length >> 8) & 0xff);
      out[out_offset++] = (byte) (length & 0xff);
    } else if (length >= 0x7e) {
      out[out_offset++] = 0x7e;
      out[out_offset++] = (byte) (length >> 8);
      out[out_offset++] = (byte) (length & 0xff);
    } else {
      out[out_offset++] = (byte) (length & 0x7f);
    }

    int l = _deflater.deflate(out, out_offset, length - out_offset);

    if (_deflater.finished()) super.addFrame(setFlag(flags, 1), opcode, out, 0, l + out_offset);
    else super.addFrame(clearFlag(flags, 1), opcode, content, offset, length);
  }
  @Test
  public void testDeflateBasics() throws Exception {
    // Setup deflater basics
    boolean nowrap = true;
    Deflater compressor = new Deflater(Deflater.BEST_COMPRESSION, nowrap);
    compressor.setStrategy(Deflater.DEFAULT_STRATEGY);

    // Text to compress
    String text = "info:";
    byte uncompressed[] = StringUtil.getUtf8Bytes(text);

    // Prime the compressor
    compressor.reset();
    compressor.setInput(uncompressed, 0, uncompressed.length);
    compressor.finish();

    // Perform compression
    ByteBuffer outbuf = ByteBuffer.allocate(64);
    BufferUtil.clearToFill(outbuf);

    while (!compressor.finished()) {
      byte out[] = new byte[64];
      int len = compressor.deflate(out, 0, out.length, Deflater.SYNC_FLUSH);
      if (len > 0) {
        outbuf.put(out, 0, len);
      }
    }
    compressor.end();

    BufferUtil.flipToFlush(outbuf, 0);
    byte b0 = outbuf.get(0);
    if ((b0 & 1) != 0) {
      outbuf.put(0, (b0 ^= 1));
    }
    byte compressed[] = BufferUtil.toArray(outbuf);

    String actual = TypeUtil.toHexString(compressed);
    String expected = "CaCc4bCbB70200"; // what pywebsocket produces

    Assert.assertThat("Compressed data", actual, is(expected));
  }
Esempio n. 25
0
 /**
  * compress a byte array to a new byte array by {@link java.util.zip.Deflater}
  *
  * @param data input data to compress
  * @param len the 0..length in data is for compress
  * @return compressed byte array
  */
 public static byte[] deflateCompress(byte[] data) {
   Deflater compresser = new Deflater();
   ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length);
   try {
     compresser.reset();
     compresser.setInput(data, 0, data.length);
     compresser.finish();
     byte[] buf = new byte[1024];
     while (!compresser.finished()) {
       int i = compresser.deflate(buf);
       bos.write(buf, 0, i);
     }
     return bos.toByteArray();
   } finally {
     compresser.end();
     try {
       bos.close();
     } catch (IOException e) {
     }
   }
 }
  public Packet51MapChunk(Chunk chunk, boolean flag, int i) {
    this.lowPriority = true;
    this.a = chunk.x;
    this.b = chunk.z;
    this.e = flag;
    ChunkMap chunkmap = a(chunk, flag, i);
    Deflater deflater = new Deflater(-1);

    this.d = chunkmap.c;
    this.c = chunkmap.b;

    try {
      this.inflatedBuffer = chunkmap.a;
      deflater.setInput(chunkmap.a, 0, chunkmap.a.length);
      deflater.finish();
      this.buffer = new byte[chunkmap.a.length];
      this.size = deflater.deflate(this.buffer);
    } finally {
      deflater.end();
    }
  }
Esempio n. 27
0
  public static byte[] compress(byte[] data) throws Exception {
    Deflater def = new Deflater();
    def.setInput(data);

    ByteArrayOutputStream baos = new ByteArrayOutputStream(data.length);

    def.finish();

    byte[] buf = new byte[1024];

    while (!def.finished()) {

      int compressed = def.deflate(buf);

      baos.write(buf, 0, compressed);
    }

    baos.close();

    return baos.toByteArray();
  }
Esempio n. 28
0
 // TODO move to separate thread?
 public void compress() {
   if (!compressed) {
     Deflater deflater = new Deflater();
     deflater.setInput(fileData);
     deflater.setLevel(Deflater.BEST_COMPRESSION);
     deflater.finish();
     ByteArrayOutputStream bos = new ByteArrayOutputStream(fileData.length);
     byte[] buffer = new byte[1024];
     while (!deflater.finished()) {
       int bytesCompressed = deflater.deflate(buffer);
       bos.write(buffer, 0, bytesCompressed);
     }
     try {
       bos.close();
     } catch (IOException e) {
       e.printStackTrace();
     }
     fileData = bos.toByteArray();
     compressed = true;
   }
 }
  public Packet56MapChunks(List p_i3324_1_) {
    int i = p_i3324_1_.size();
    field_73589_c = new int[i];
    field_73586_d = new int[i];
    field_73590_a = new int[i];
    field_73588_b = new int[i];
    field_73584_f = new byte[i][];
    int j = 0;
    for (int k = 0; k < i; k++) {
      Chunk chunk = (Chunk) p_i3324_1_.get(k);
      Packet51MapChunkData packet51mapchunkdata = Packet51MapChunk.func_73594_a(chunk, true, 65535);
      if (field_73591_h.length < j + packet51mapchunkdata.field_74582_a.length) {
        byte abyte0[] = new byte[j + packet51mapchunkdata.field_74582_a.length];
        System.arraycopy(field_73591_h, 0, abyte0, 0, field_73591_h.length);
        field_73591_h = abyte0;
      }
      System.arraycopy(
          packet51mapchunkdata.field_74582_a,
          0,
          field_73591_h,
          j,
          packet51mapchunkdata.field_74582_a.length);
      j += packet51mapchunkdata.field_74582_a.length;
      field_73589_c[k] = chunk.field_76635_g;
      field_73586_d[k] = chunk.field_76647_h;
      field_73590_a[k] = packet51mapchunkdata.field_74580_b;
      field_73588_b[k] = packet51mapchunkdata.field_74581_c;
      field_73584_f[k] = packet51mapchunkdata.field_74582_a;
    }

    Deflater deflater = new Deflater(-1);
    try {
      deflater.setInput(field_73591_h, 0, j);
      deflater.finish();
      field_73587_e = new byte[j];
      field_73585_g = deflater.deflate(field_73587_e);
    } finally {
      deflater.end();
    }
  }
Esempio n. 30
0
  /**
   * Reads compressed data into a byte array. This method will block until some input can be read
   * and compressed.
   *
   * @param b buffer into which the data is read
   * @param off starting offset of the data within {@code b}
   * @param len maximum number of compressed bytes to read into {@code b}
   * @return the actual number of bytes read, or -1 if the end of the uncompressed input stream is
   *     reached
   * @throws IndexOutOfBoundsException if {@code len} > {@code b.length - off}
   * @throws IOException if an I/O error occurs or if this input stream is already closed
   */
  public int read(byte[] b, int off, int len) throws IOException {
    // Sanity checks
    ensureOpen();
    if (b == null) {
      throw new NullPointerException("Null buffer for read");
    } else if (off < 0 || len < 0 || len > b.length - off) {
      throw new IndexOutOfBoundsException();
    } else if (len == 0) {
      return 0;
    }

    // Read and compress (deflate) input data bytes
    int cnt = 0;
    while (len > 0 && !def.finished()) {
      int n;

      // Read data from the input stream
      if (def.needsInput()) {
        n = in.read(buf, 0, buf.length);
        if (n < 0) {
          // End of the input stream reached
          def.finish();
        } else if (n > 0) {
          def.setInput(buf, 0, n);
        }
      }

      // Compress the input data, filling the read buffer
      n = def.deflate(b, off, len);
      cnt += n;
      off += n;
      len -= n;
    }
    if (cnt == 0 && def.finished()) {
      reachEOF = true;
      cnt = -1;
    }

    return cnt;
  }