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;
 }
  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 all necessary data for this entry.
   *
   * @since 1.1
   */
  public void closeEntry() throws IOException {
    if (entry == null) {
      return;
    }

    long realCrc = crc.getValue();
    crc.reset();

    if (entry.getMethod() == DEFLATED) {
      def.finish();
      while (!def.finished()) {
        deflate();
      }

      entry.setSize(def.getTotalIn());
      entry.setComprSize(def.getTotalOut());
      entry.setCrc(realCrc);

      def.reset();

      written += entry.getCompressedSize();
    } else if (raf == null) {
      if (entry.getCrc() != realCrc) {
        throw new SwcException.BadCRC(Long.toHexString(entry.getCrc()), Long.toHexString(realCrc));
      }

      if (entry.getSize() != written - dataStart) {
        throw new SwcException.BadZipSize(
            entry.getName(), entry.getSize() + "", (written - dataStart) + "");
      }
    } else {
        /* method is STORED and we used RandomAccessFile */
      long size = written - dataStart;

      entry.setSize(size);
      entry.setComprSize(size);
      entry.setCrc(realCrc);
    }

    // If random access output, write the local file header containing
    // the correct CRC and compressed/uncompressed sizes
    if (raf != null) {
      long save = raf.getFilePointer();

      raf.seek(localDataStart);
      writeOut((new ZipLong(entry.getCrc())).getBytes());
      writeOut((new ZipLong(entry.getCompressedSize())).getBytes());
      writeOut((new ZipLong(entry.getSize())).getBytes());
      raf.seek(save);
    }

    writeDataDescriptor(entry);
    entry = null;
  }
  /**
   * Ensures the current entry's size and CRC information is set to the values just written,
   * verifies it isn't too big in the Zip64Mode.Never case and returns whether the entry would
   * require a Zip64 extra field.
   */
  private boolean handleSizesAndCrc(long bytesWritten, long crc, Zip64Mode effectiveMode)
      throws ZipException {
    if (entry.entry.getMethod() == DEFLATED) {
      /* It turns out def.getBytesRead() returns wrong values if
       * the size exceeds 4 GB on Java < Java7
      entry.entry.setSize(def.getBytesRead());
      */
      entry.entry.setSize(entry.bytesRead);
      entry.entry.setCompressedSize(bytesWritten);
      entry.entry.setCrc(crc);

      def.reset();
    } else if (raf == null) {
      if (entry.entry.getCrc() != crc) {
        throw new ZipException(
            "bad CRC checksum for entry "
                + entry.entry.getName()
                + ": "
                + Long.toHexString(entry.entry.getCrc())
                + " instead of "
                + Long.toHexString(crc));
      }

      if (entry.entry.getSize() != bytesWritten) {
        throw new ZipException(
            "bad size for entry "
                + entry.entry.getName()
                + ": "
                + entry.entry.getSize()
                + " instead of "
                + bytesWritten);
      }
    } else {
        /* method is STORED and we used RandomAccessFile */
      entry.entry.setSize(bytesWritten);
      entry.entry.setCompressedSize(bytesWritten);
      entry.entry.setCrc(crc);
    }

    final boolean actuallyNeedsZip64 =
        effectiveMode == Zip64Mode.Always
            || entry.entry.getSize() >= ZIP64_MAGIC
            || entry.entry.getCompressedSize() >= ZIP64_MAGIC;
    if (actuallyNeedsZip64 && effectiveMode == Zip64Mode.Never) {
      throw new Zip64RequiredException(Zip64RequiredException.getEntryTooBigMessage(entry.entry));
    }
    return actuallyNeedsZip64;
  }
示例#5
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);
     }
   }
 }
  /* (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));
  }
示例#8
0
文件: NetUtil.java 项目: niczy/hyo
 /**
  * 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) {
     }
   }
 }
示例#9
0
 private void startNewMessage() {
   firstCompressedFrameWritten = false;
   if (isServer && !serverContextTakeover || !isServer && !clientContextTakeover) {
     deflater.reset();
   }
 }