예제 #1
0
  @Override
  public TransformationResult getMoreData(byte opCode, boolean fin, int rsv, ByteBuffer dest)
      throws IOException {
    // Control frames are never compressed and may appear in the middle of
    // a WebSocket method. Pass them straight through.
    if (Util.isControl(opCode)) {
      return next.getMoreData(opCode, fin, rsv, dest);
    }

    if (!Util.isContinuation(opCode)) {
      // First frame in new message
      skipDecompression = (rsv & RSV_BITMASK) == 0;
    }

    // Pass uncompressed frames straight through.
    if (skipDecompression) {
      return next.getMoreData(opCode, fin, rsv, dest);
    }

    int written;
    boolean usedEomBytes = false;

    while (dest.remaining() > 0) {
      // Space available in destination. Try and fill it.
      try {
        written =
            inflater.inflate(dest.array(), dest.arrayOffset() + dest.position(), dest.remaining());
      } catch (DataFormatException e) {
        throw new IOException(sm.getString("perMessageDeflate.deflateFailed"), e);
      }
      dest.position(dest.position() + written);

      if (inflater.needsInput() && !usedEomBytes) {
        if (dest.hasRemaining()) {
          readBuffer.clear();
          TransformationResult nextResult =
              next.getMoreData(opCode, fin, (rsv ^ RSV_BITMASK), readBuffer);
          inflater.setInput(readBuffer.array(), readBuffer.arrayOffset(), readBuffer.position());
          if (TransformationResult.UNDERFLOW.equals(nextResult)) {
            return nextResult;
          } else if (TransformationResult.END_OF_FRAME.equals(nextResult)
              && readBuffer.position() == 0) {
            if (fin) {
              inflater.setInput(EOM_BYTES);
              usedEomBytes = true;
            } else {
              return TransformationResult.END_OF_FRAME;
            }
          }
        }
      } else if (written == 0) {
        if (fin && (isServer && !clientContextTakeover || !isServer && !serverContextTakeover)) {
          inflater.reset();
        }
        return TransformationResult.END_OF_FRAME;
      }
    }

    return TransformationResult.OVERFLOW;
  }
예제 #2
0
 private void reset() {
   inflater.reset();
   Arrays.fill(bytes, (byte) 0);
   output = null;
   state = State.INITIAL;
   size = 0;
   value = 0;
   flags = 0;
 }
예제 #3
0
  /**
   * Release an inflater previously obtained from this cache.
   *
   * @param i the inflater to return. May be null, in which case this method does nothing.
   */
  public static void release(final Inflater i) {
    if (i == null) return;

    if (openInflaterCount == SZ) {
      i.end();
      return;
    }

    i.reset();
    releaseImpl(i);
  }
예제 #4
0
 public static byte[] decompress(byte buffer[]) throws IOException {
   byte result[] = new byte[Byteconvert.convertInt(buffer, 8)];
   inflater.setInput(buffer, 12, buffer.length - 12);
   try {
     inflater.inflate(result);
   } catch (DataFormatException e) {
     throw new IOException();
   }
   inflater.reset();
   return result;
 }
예제 #5
0
  private int inflate(byte[] src, byte[] dest) {
    Inflater inflater = (inflaterRef == null ? null : inflaterRef.get());

    // construct the inflater object or reuse an existing one
    if (inflater == null) inflaterRef = new SoftReference<Inflater>(inflater = new Inflater(true));

    inflater.reset();
    inflater.setInput(src);
    try {
      return inflater.inflate(dest);
    } catch (DataFormatException ex) {
      return -1;
    }
  }
예제 #6
0
 public byte[] uncompress(byte[] bytes) throws DataFormatException {
   try {
     inflater.setInput(bytes);
     int len = inflater.inflate(buffer);
     byte[] uncompressedBytes = new byte[len];
     System.arraycopy(buffer, 0, uncompressedBytes, 0, len);
     return uncompressedBytes;
   } finally {
     try {
       inflater.reset();
     } catch (Exception e) {
       logger.warn("inflater.reset failed", e);
     }
   }
 }
  @Test
  public void testInflateBasics() throws Exception {
    // should result in "info:" text if properly inflated
    byte rawbuf[] = TypeUtil.fromHexString("CaCc4bCbB70200"); // what pywebsocket produces
    // byte rawbuf[] = TypeUtil.fromHexString("CbCc4bCbB70200"); // what java produces

    Inflater inflater = new Inflater(true);
    inflater.reset();
    inflater.setInput(rawbuf, 0, rawbuf.length);

    byte outbuf[] = new byte[64];
    int len = inflater.inflate(outbuf);
    inflater.end();
    Assert.assertThat("Inflated length", len, greaterThan(4));

    String actual = StringUtil.toUTF8String(outbuf, 0, len);
    Assert.assertThat("Inflated text", actual, is("info:"));
  }
예제 #8
0
파일: NetUtil.java 프로젝트: niczy/hyo
 /**
  * decompress 0..len in data
  *
  * @param data
  * @param len length of data for decompress in data[]
  * @return
  * @throws java.util.zip.DataFormatException
  */
 public static byte[] defalteDecompress(byte[] data) throws DataFormatException {
   ByteArrayOutputStream o = new ByteArrayOutputStream(data.length);
   Inflater decompresser = new Inflater();
   try {
     decompresser.reset();
     decompresser.setInput(data, 0, data.length);
     byte[] buf = new byte[1024];
     while (!decompresser.finished()) {
       int i = decompresser.inflate(buf);
       o.write(buf, 0, i);
     }
     return o.toByteArray();
   } finally {
     decompresser.end();
     try {
       o.close();
     } catch (IOException e) {
     }
   }
 }
예제 #9
0
  /**
   * Look up the byte code definition of the named class.
   *
   * <p>Created: Mar 30, 2004
   *
   * @param className
   * @return - byte code for the class definition.
   */
  private byte[] lookupClassData(String className) {

    if (isJar(moduleParentLocator)) {
      String fileName;
      if (LECCMachineConfiguration.generateBytecode()) {
        fileName = className.replace('.', '/') + COMPRESSED_CLASS_FILE_EXTENSION;
      } else {
        fileName = className.replace('.', '/') + CLASS_FILE_EXTENSION;
      }
      return loadJarData((ProgramResourceLocator.File) moduleParentLocator, fileName);

    } else {

      ProgramResourceLocator.File fileLocator;
      if (LECCMachineConfiguration.generateBytecode()) {
        String extension =
            className.substring(className.lastIndexOf('.') + 1)
                + CALClassLoader.COMPRESSED_CLASS_FILE_EXTENSION;
        fileLocator = ((ProgramResourceLocator.Folder) moduleParentLocator).extendFile(extension);
      } else {
        String extension =
            className.substring(className.lastIndexOf('.') + 1)
                + CALClassLoader.CLASS_FILE_EXTENSION;
        fileLocator = ((ProgramResourceLocator.Folder) moduleParentLocator).extendFile(extension);
      }

      byte[] fileData = loadFileData(fileLocator);
      if (LECCMachineConfiguration.generateBytecode()) {
        if (fileData == null) {
          return null;
        }

        // uncompress the data..
        inflater.reset();
        inflater.setInput(fileData);

        // Reusing this baos should be ok, since its only other use is in loadFileData(), which
        // we've finished calling.
        ByteArrayOutputStream baos = threadLocalByteArrayOutputStream.get();
        baos.reset();

        try {
          // Reusing this bytearray should be ok, since its only other use is in loadFileData(),
          // which we've finished calling.
          byte[] buf = threadLocalByteArray.get();
          while (!inflater.finished()) {
            int len = inflater.inflate(buf);
            baos.write(buf, 0, len);
          }

        } catch (DataFormatException e) {
          // Can't read the compressed class..

          // return null;
          throw new IllegalStateException("Can't read compressed class: " + className);
        }

        return baos.toByteArray();

        // Not necessary to close the ByteArrayOutputStream.

      } else {
        // Source generator.
        return fileData;
      }
    }
  }
예제 #10
0
  /** Inits/resets this class to be ready to read the start of a gzip stream. */
  private void init() throws IOException {
    _inflater.reset();
    _crc.reset();
    _inputSize = 0;
    _readBufferSize = 0;

    byte flg;

    int length = _in.read(_tbuffer, 0, 10);

    if (length < 0) {
      _isGzip = false;
      return;
    } else if (length != 10) {
      _isGzip = false;
      _in.unread(_tbuffer, 0, length);
      return;
    }

    if (_tbuffer[0] != (byte) 0x1f || _tbuffer[1] != (byte) 0x8b) {
      _isGzip = false;
      _in.unread(_tbuffer, 0, length);
      return;
    }

    flg = _tbuffer[3];

    // Skip optional field
    if ((flg & (byte) 0x04) > 0) {
      length = _in.read(_tbuffer, 0, 2);
      if (length != 2) throw new IOException("Bad GZIP (FEXTRA) header.");
      length = (((int) _tbuffer[1]) << 4) | _tbuffer[0];
      _in.skip(length);
    }

    int c;

    // Skip optional field
    if ((flg & (byte) 0x08) > 0) {
      c = _in.read();
      while (c != 0) {
        if (c < 0) throw new IOException("Bad GZIP (FNAME) header.");
        c = _in.read();
      }
    }

    // Skip optional field
    if ((flg & 0x10) > 0) {
      c = _in.read();
      while (c != 0) {
        if (c < 0) throw new IOException("Bad GZIP (FCOMMENT) header.");

        c = _in.read();
      }
    }

    // Skip optional field
    if ((flg & 0x02) > 0) {
      length = _in.read(_tbuffer, 0, 2);
      if (length != 2) throw new IOException("Bad GZIP (FHCRC) header.");
    }

    _isGzip = true;
  }