예제 #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;
  }
  private boolean handleCompressedFrame(ChannelHandlerContext ctx, ByteBuf in, List<Object> out)
      throws Exception {
    if (!in.isReadable(FRAME_COMPRESS_HEADER_LENGTH)) {
      return false;
    }

    int compressedPayloadLength = in.readInt();
    if (!in.isReadable(compressedPayloadLength)) {
      return false;
    }

    // decompress payload
    Inflater inflater = new Inflater();
    if (in.hasArray()) {
      inflater.setInput(in.array(), in.arrayOffset() + in.readerIndex(), compressedPayloadLength);
      in.skipBytes(compressedPayloadLength);
    } else {
      byte[] array = new byte[compressedPayloadLength];
      in.readBytes(array);
      inflater.setInput(array);
    }

    while (!inflater.finished()) {
      ByteBuf decompressed = ctx.alloc().heapBuffer(1024, 1024);
      byte[] outArray = decompressed.array();
      int count =
          inflater.inflate(outArray, decompressed.arrayOffset(), decompressed.writableBytes());
      decompressed.writerIndex(count);
      // put data in the pipeline
      out.add(decompressed);
    }

    return true;
  }
  @Override
  public void decompress() {
    if (compressed) {
      Inflater decompressor = new Inflater();
      decompressor.setInput(data);

      ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length);

      byte[] buf = new byte[1024];
      while (!decompressor.finished()) {
        try {
          int count = decompressor.inflate(buf);
          bos.write(buf, 0, count);
        } catch (DataFormatException e) {

        }
      }
      try {
        bos.close();
      } catch (IOException e) {

      }

      data = bos.toByteArray();
    }
  }
예제 #4
0
  /**
   * @param compressed_size or use null if not known
   * @param uncompressed_size or use null if not known
   * @return uncompressed ByteArrayOutputStream
   * @throws IOException
   * @throws DataFormatException
   */
  private ByteArrayOutputStream unCompress(Integer compressed_size, Integer uncompressed_size)
      throws IOException, DataFormatException {
    byte[] uncompressed_data = null;
    byte[] input_data = null;
    ByteArrayOutputStream ret = new ByteArrayOutputStream();
    Inflater decompresser = new Inflater(false);
    long first_seek = fileChannel.position();
    Boolean uncompressing = true;
    while (uncompressing) {
      if (decompresser.needsInput()) {
        input_data = new byte[(compressed_size != null) ? compressed_size.intValue() : 1024];
        fileChannel.read(ByteBuffer.wrap(input_data));
        decompresser.setInput(input_data, 0, input_data.length);
      }
      uncompressed_data =
          new byte
              [(uncompressed_size != null)
                  ? uncompressed_size.intValue()
                  : (input_data.length * 4)];
      decompresser.inflate(uncompressed_data);
      int op = (int) (decompresser.getBytesWritten() - (long) ret.size());
      if (op > 0) ret.write(uncompressed_data, 0, op);

      if (decompresser.finished()) uncompressing = false;
    }
    fileChannel.position(
        (first_seek + decompresser.getBytesRead())); // move file pointer to start of next stream
    decompresser.end();
    return ret;
  }
  public String getSystraceHtml() {
    if (mSystraceIndex < 0) {
      return "";
    }

    String trace = "";
    if (mUncompress) {
      Inflater decompressor = new Inflater();
      decompressor.setInput(mAtraceOutput, mSystraceIndex, mAtraceLength - mSystraceIndex);

      byte[] buf = new byte[4096];
      int n;
      StringBuilder sb = new StringBuilder(1000);
      try {
        while ((n = decompressor.inflate(buf)) > 0) {
          sb.append(new String(buf, 0, n));
        }
      } catch (DataFormatException e) {
        throw new RuntimeException(e);
      }
      decompressor.end();

      trace = sb.toString();
    } else {
      trace = new String(mAtraceOutput, mSystraceIndex, mAtraceLength - mSystraceIndex);
    }

    // each line should end with the characters \n\ followed by a newline
    String html_out = trace.replaceAll("\n", "\\\\n\\\\\n");
    String header = String.format(mHtmlPrefix, mCss, mJs, "");
    String footer = mHtmlSuffix;
    return header + html_out + footer;
  }
  /* (non-Javadoc)
   * @see org.eclipse.jetty.websocket.AbstractExtension#onFrame(byte, byte, org.eclipse.jetty.io.Buffer)
   */
  @Override
  public void onFrame(byte flags, byte opcode, Buffer buffer) {
    if (getConnection().isControl(opcode) || !isFlag(flags, 1)) {
      super.onFrame(flags, opcode, buffer);
      return;
    }

    if (buffer.array() == null) buffer = buffer.asMutableBuffer();

    int length = 0xff & buffer.get();
    if (length >= 0x7e) {
      int b = (length == 0x7f) ? 8 : 2;
      length = 0;
      while (b-- > 0) length = 0x100 * length + (0xff & buffer.get());
    }

    // TODO check a max framesize

    _inflater.setInput(buffer.array(), buffer.getIndex(), buffer.length());
    ByteArrayBuffer buf = new ByteArrayBuffer(length);
    try {
      while (_inflater.getRemaining() > 0) {
        int inflated = _inflater.inflate(buf.array(), buf.putIndex(), buf.space());
        if (inflated == 0) throw new DataFormatException("insufficient data");
        buf.setPutIndex(buf.putIndex() + inflated);
      }

      super.onFrame(clearFlag(flags, 1), opcode, buf);
    } catch (DataFormatException e) {
      LOG.warn(e);
      getConnection().close(WebSocketConnectionRFC6455.CLOSE_BAD_PAYLOAD, e.toString());
    }
  }
예제 #7
0
  private final byte[] uncompress(final byte[] input) throws IOException {

    Inflater decompressor = new Inflater();
    decompressor.setInput(input);

    // Create an expandable byte array to hold the decompressed data
    ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);

    // Decompress the data
    byte[] buf = new byte[1024];
    while (!decompressor.finished()) {
      try {
        int count = decompressor.inflate(buf);
        bos.write(buf, 0, count);
      } catch (DataFormatException e) {
        // this will happen if the field is not compressed
        IOException newException =
            new IOException("field data are in wrong format: " + e.toString());
        newException.initCause(e);
        throw newException;
      }
    }

    decompressor.end();

    // Get the decompressed data
    return bos.toByteArray();
  }
 /**
  * inflater 解壓縮
  *
  * @param value
  * @return
  */
 public static byte[] inflater(byte[] value) {
   byte[] result = new byte[0];
   Inflater inflater = null;
   ByteArrayOutputStream out = null;
   try {
     inflater = new Inflater();
     inflater.setInput(value);
     //
     out = new ByteArrayOutputStream();
     byte[] buffer = new byte[1024];
     while (!inflater.finished()) {
       int count = inflater.inflate(buffer);
       out.write(buffer, 0, count);
     }
     //
     result = out.toByteArray();
   } catch (Exception ex) {
     ex.printStackTrace();
   } finally {
     if (inflater != null) {
       inflater.end(); // 不end也不會有oom,不過還是關了吧
     }
     IoHelper.close(out);
   }
   return result;
 }
예제 #9
0
  public static byte[] decompress(byte[] data) throws IOException, DataFormatException {

    Inflater inflater = new Inflater();

    inflater.setInput(data);

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);

    byte[] buffer = new byte[1024];

    while (!inflater.finished()) {

      int count = inflater.inflate(buffer);

      outputStream.write(buffer, 0, count);
    }
    outputStream.close();
    byte[] output = outputStream.toByteArray();

    System.out.println("Original: " + data.length);

    System.out.println("Compressed: " + output.length);

    return output;
  }
예제 #10
0
 void check(Inflater inf, byte[] tmp, long pos, int cnt) throws DataFormatException {
   // Unlike inflate() above the exact byte count is known by the caller.
   // Push all of it in a single invocation to avoid unnecessary loops.
   //
   inf.setInput(block, (int) (pos - start), cnt);
   while (inf.inflate(tmp, 0, tmp.length) > 0) continue;
 }
예제 #11
0
 public static String decompress(byte[] data) {
   int length = ByteBuffer.wrap(Arrays.copyOfRange(data, 0, 4)).getInt();
   if (length > 100000) {
     // This is a sanity check. More than 100kb of password settings make no sense.
     System.out.println("Decompression error: The trasferred length is too big.");
     return "";
   }
   Inflater inflater = new Inflater();
   inflater.setInput(data, 4, data.length - 4);
   byte[] decompressedBytes = new byte[length];
   try {
     if (inflater.inflate(decompressedBytes) != length) {
       throw new AssertionError();
     }
   } catch (DataFormatException e) {
     e.printStackTrace();
   }
   inflater.end();
   try {
     return new String(decompressedBytes, "UTF-8");
   } catch (UnsupportedEncodingException e) {
     System.out.println(
         "Decompression error: UTF-8 is not supported. " + "Using default encoding.");
     return new String(decompressedBytes);
   }
 }
예제 #12
0
  @Test
  public void testDecompressDeflateRequest() throws Exception {
    request.setMethod("POST");
    request.setURI("/banner");
    request.setHeader(HttpHeaders.CONTENT_ENCODING, "deflate");
    request.setHeader(HttpHeaders.CONTENT_TYPE, PLAIN_TEXT_UTF_8);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try (DeflaterOutputStream deflate = new DeflaterOutputStream(baos)) {
      Resources.copy(Resources.getResource("assets/new-banner.txt"), deflate);
    }
    byte[] output = baos.toByteArray();
    request.setContent(output);

    // Decompress the bytes
    Inflater decompresser = new Inflater();
    decompresser.setInput(output);

    byte[] result = new byte[4096];
    int resultLength = decompresser.inflate(result);
    decompresser.end();

    // Decode the bytes into a String
    System.out.println(new String(result, 0, resultLength, "UTF-8"));

    HttpTester.Response response =
        HttpTester.parseResponse(servletTester.getResponses(request.generate()));
    System.out.println(response.getStatus());
    System.out.println(response.getContent());
  }
예제 #13
0
  public static byte[] inflateObject(byte[] compressedData) throws Exception {
    // Create the decompressor and give it the data to compress
    Inflater decompressor = new Inflater();
    decompressor.setInput(compressedData);

    // Create an expandable byte array to hold the decompressed data
    ByteArrayOutputStream bos = new ByteArrayOutputStream(compressedData.length);

    // Decompress the data
    byte[] buf = new byte[1024];
    while (!decompressor.finished()) {
      try {
        int count = decompressor.inflate(buf);
        bos.write(buf, 0, count);
      } catch (DataFormatException e) {
        throw e;
      } finally {
        try {
          bos.close();
        } catch (IOException ioe) {
        }
      }
    }

    // Get the decompressed data
    return bos.toByteArray();
  }
예제 #14
0
 /**
  * Fills input buffer with more data to decompress.
  *
  * @exception IOException if an I/O error has occurred
  */
 protected void fill() throws IOException {
   ensureOpen();
   len = in.read(buf, 0, buf.length);
   if (len == -1) {
     throw new EOFException("Unexpected end of ZLIB input stream");
   }
   inf.setInput(buf, 0, len);
 }
예제 #15
0
  @Override
  public void readData(DataInputStream in) throws IOException {
    x = in.readInt();
    z = in.readInt();
    biomes = in.readBoolean();
    bitmask = in.readShort();
    additionalBitmask = in.readShort();
    int tempLength = in.readInt();

    byte[] compressedChunkData = new byte[tempLength];
    in.readFully(compressedChunkData, 0, tempLength);
    int i = 0;

    for (int j = 0; j < 16; j++) i += bitmask >> j & 1;

    int k = 12288 * i;

    if (biomes) k += 256;

    chunkData = new byte[k];
    Inflater inflater = new Inflater();
    inflater.setInput(compressedChunkData, 0, tempLength);

    try {
      inflater.inflate(chunkData);
    } catch (DataFormatException dataformatexception) {
      chunkData = null;
    } catch (OutOfMemoryError error) {
      System.gc();
      try {
        inflater.end();

        inflater = new Inflater();
        inflater.setInput(compressedChunkData, 0, tempLength);

        inflater.inflate(chunkData);
      } catch (DataFormatException dataformatexception) {
        chunkData = null;
      } catch (OutOfMemoryError error2) {
        chunkData = null;
      }
    } finally {
      inflater.end();
    }
  }
예제 #16
0
  /** Abstract. Reads the raw packet data from the data stream. */
  public void readPacketData(DataInputStream par1DataInputStream) throws IOException {
    short short1 = par1DataInputStream.readShort();
    this.dataLength = par1DataInputStream.readInt();
    this.skyLightSent = par1DataInputStream.readBoolean();
    this.chunkPostX = new int[short1];
    this.chunkPosZ = new int[short1];
    this.field_73590_a = new int[short1];
    this.field_73588_b = new int[short1];
    this.field_73584_f = new byte[short1][];

    if (chunkDataNotCompressed.length < this.dataLength) {
      chunkDataNotCompressed = new byte[this.dataLength];
    }

    par1DataInputStream.readFully(chunkDataNotCompressed, 0, this.dataLength);
    byte[] abyte = new byte[196864 * short1];
    Inflater inflater = new Inflater();
    inflater.setInput(chunkDataNotCompressed, 0, this.dataLength);

    try {
      inflater.inflate(abyte);
    } catch (DataFormatException dataformatexception) {
      throw new IOException("Bad compressed data format");
    } finally {
      inflater.end();
    }

    int i = 0;

    for (int j = 0; j < short1; ++j) {
      this.chunkPostX[j] = par1DataInputStream.readInt();
      this.chunkPosZ[j] = par1DataInputStream.readInt();
      this.field_73590_a[j] = par1DataInputStream.readShort();
      this.field_73588_b[j] = par1DataInputStream.readShort();
      int k = 0;
      int l = 0;
      int i1;

      for (i1 = 0; i1 < 16; ++i1) {
        k += this.field_73590_a[j] >> i1 & 1;
        l += this.field_73588_b[j] >> i1 & 1;
      }

      i1 = 2048 * 4 * k + 256;
      i1 += 2048 * l;

      if (this.skyLightSent) {
        i1 += 2048 * k;
      }

      this.field_73584_f[j] = new byte[i1];
      System.arraycopy(abyte, i, this.field_73584_f[j], 0, i1);
      i += i1;
    }
  }
  /**
   * Writes an array of bytes to the uncompressed output stream.
   *
   * @param b buffer containing compressed data to decompress and write to the output stream
   * @param off starting offset of the compressed data within {@code b}
   * @param len number of bytes to decompress from {@code b}
   * @throws IndexOutOfBoundsException if {@code off < 0}, or if {@code len < 0}, or if {@code len >
   *     b.length - off}
   * @throws IOException if an I/O error occurs or this stream is already closed
   * @throws NullPointerException if {@code b} is null
   * @throws ZipException if a compression (ZIP) format error occurs
   */
  public void write(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;
    }

    // Write uncompressed data to the output stream
    try {
      for (; ; ) {
        int n;

        // Fill the decompressor buffer with output data
        if (inf.needsInput()) {
          int part;

          if (len < 1) {
            break;
          }

          part = (len < 512 ? len : 512);
          inf.setInput(b, off, part);
          off += part;
          len -= part;
        }

        // Decompress and write blocks of output data
        do {
          n = inf.inflate(buf, 0, buf.length);
          if (n > 0) {
            out.write(buf, 0, n);
          }
        } while (n > 0);

        // Check the decompressor
        if (inf.finished()) {
          break;
        }
        if (inf.needsDictionary()) {
          throw new ZipException("ZLIB dictionary missing");
        }
      }
    } catch (DataFormatException ex) {
      // Improperly formatted compressed (ZIP) data
      String msg = ex.getMessage();
      if (msg == null) {
        msg = "Invalid ZLIB data format";
      }
      throw new ZipException(msg);
    }
  }
예제 #18
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;
 }
예제 #19
0
  int inflate(Inflater inf, long pos, byte[] dstbuf, int dstoff) throws DataFormatException {
    int ptr = (int) (pos - start);
    int in = Math.min(INFLATE_STRIDE, block.length - ptr);
    if (dstoff < dstbuf.length) in = Math.min(in, dstbuf.length - dstoff);
    inf.setInput(block, ptr, in);

    for (; ; ) {
      int out = inf.inflate(dstbuf, dstoff, dstbuf.length - dstoff);
      if (out == 0) {
        if (inf.needsInput()) {
          ptr += in;
          in = Math.min(INFLATE_STRIDE, block.length - ptr);
          if (in == 0) return dstoff;
          inf.setInput(block, ptr, in);
          continue;
        }
        return dstoff;
      }
      dstoff += out;
    }
  }
예제 #20
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;
    }
  }
예제 #21
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);
     }
   }
 }
예제 #22
0
 /** TIFF Adobe ZIP support contributed by Jason Newton. */
 public byte[] zipUncompress(byte[] input) {
   ByteArrayOutputStream imageBuffer = new ByteArrayOutputStream();
   byte[] buffer = new byte[1024];
   Inflater decompressor = new Inflater();
   decompressor.setInput(input);
   try {
     while (!decompressor.finished()) {
       int rlen = decompressor.inflate(buffer);
       imageBuffer.write(buffer, 0, rlen);
     }
   } catch (DataFormatException e) {
     IJ.log(e.toString());
   }
   decompressor.end();
   return imageBuffer.toByteArray();
 }
예제 #23
0
 public static byte[] decompress(byte[] compressedData) throws IOException {
   Inflater inflater = new Inflater();
   inflater.setInput(compressedData);
   ByteArrayOutputStream bos = new ByteArrayOutputStream(compressedData.length);
   byte[] buf = new byte[1024];
   while (!inflater.finished()) {
     try {
       int count = inflater.inflate(buf);
       bos.write(buf, 0, count);
     } catch (DataFormatException e) {
     }
   }
   bos.close();
   inflater.end();
   return bos.toByteArray();
 }
예제 #24
0
  /**
   * Decoding and deflating the encoded AuthReq
   *
   * @param encodedStr encoded AuthReq
   * @return decoded AuthReq
   */
  public static String decode(String encodedStr) throws SAMLSSOException {
    try {
      org.apache.commons.codec.binary.Base64 base64Decoder =
          new org.apache.commons.codec.binary.Base64();
      byte[] xmlBytes = encodedStr.getBytes("UTF-8");
      byte[] base64DecodedByteArray = base64Decoder.decode(xmlBytes);

      try {
        // TODO if the request came in POST, inflating is wrong
        Inflater inflater = new Inflater(true);
        inflater.setInput(base64DecodedByteArray);
        byte[] xmlMessageBytes = new byte[5000];
        int resultLength = inflater.inflate(xmlMessageBytes);

        if (inflater.getRemaining() > 0) {
          throw new RuntimeException("didn't allocate enough space to hold " + "decompressed data");
        }

        inflater.end();
        String decodedString = new String(xmlMessageBytes, 0, resultLength, "UTF-8");
        if (log.isDebugEnabled()) {
          log.debug("Request message " + decodedString);
        }
        return decodedString;

      } catch (DataFormatException e) {
        ByteArrayInputStream bais = new ByteArrayInputStream(base64DecodedByteArray);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        InflaterInputStream iis = new InflaterInputStream(bais);
        byte[] buf = new byte[1024];
        int count = iis.read(buf);
        while (count != -1) {
          baos.write(buf, 0, count);
          count = iis.read(buf);
        }
        iis.close();
        String decodedStr = new String(baos.toByteArray(), Charset.forName("UTF-8"));
        if (log.isDebugEnabled()) {
          log.debug("Request message " + decodedStr);
        }
        return decodedStr;
      }
    } catch (IOException e) {
      throw new SAMLSSOException("Error when decoding the SAML Request.", e);
    }
  }
예제 #25
0
  /**
   * Reads from the compressed stream and stores the resulting uncompressed data into the byte
   * array.
   *
   * @return number of bytes read, or -1 upon EOF
   */
  public int read(byte[] b, int off, int len) throws IOException {
    if (len <= 0 || off < 0 || off + len > b.length) return 0;

    if (_eof) return -1;

    // Read from uncompressed stream
    if (!_isGzip) return _in.read(b, off, len);

    try {
      int sublen;
      int length = 0;
      while (length < len) {
        if (_inflater.needsInput()) {
          _readBufferSize = _in.read(_readBuffer, 0, _readBuffer.length);
          if (_readBufferSize < 0) break;

          _inflater.setInput(_readBuffer, 0, _readBufferSize);
        }

        sublen = _inflater.inflate(b, off + length, len - length);

        _crc.update(b, off + length, sublen);
        _inputSize += sublen;
        _totalInputSize += sublen;

        length += sublen;

        // Unread gzip trailer and possibly beginning of appended gzip data.
        if (_inflater.finished()) {
          int remaining = _inflater.getRemaining();
          _in.unread(_readBuffer, _readBufferSize - remaining, remaining);

          readTrailer();

          int secondPart = read(b, off + length, len - length);

          return secondPart > 0 ? length + secondPart : length;
        }
      }

      return length;
    } catch (DataFormatException e) {
      throw new IOException(e.getMessage());
    }
  }
  @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:"));
  }
예제 #27
0
  /** Abstract. Reads the raw packet data from the data stream. */
  public void readPacketData(DataInputStream par1DataInputStream) throws IOException {
    this.xCh = par1DataInputStream.readInt();
    this.zCh = par1DataInputStream.readInt();
    this.includeInitialize = par1DataInputStream.readBoolean();
    this.yChMin = par1DataInputStream.readShort();
    this.yChMax = par1DataInputStream.readShort();
    this.tempLength = par1DataInputStream.readInt();

    if (temp.length < this.tempLength) {
      temp = new byte[this.tempLength];
    }

    par1DataInputStream.readFully(temp, 0, this.tempLength);
    int var2 = 0;
    int var3;

    for (var3 = 0; var3 < 16; ++var3) {
      var2 += this.yChMin >> var3 & 1;
    }

    var3 = 12288 * var2;

    if (this.includeInitialize) {
      var3 += 256;
    }

    this.field_73596_g = new byte[var3];
    Inflater var4 = new Inflater();
    var4.setInput(temp, 0, this.tempLength);

    try {
      var4.inflate(this.field_73596_g);
    } catch (DataFormatException var9) {
      throw new IOException("Bad compressed data format");
    } finally {
      var4.end();
    }
    // Spout Start
    SpoutClient.getInstance()
        .getPacketManager()
        .sendSpoutPacket(new PacketCustomBlockChunkOverride(xCh, zCh));
    // Spout End
  }
  /** Reads the raw packet data from the data stream. */
  public void readPacketData(PacketBuffer data) throws IOException {
    this.field_149284_a = data.readInt();
    this.field_149282_b = data.readInt();
    this.field_149279_g = data.readBoolean();
    this.field_149283_c = data.readShort();
    this.field_149280_d = data.readShort();
    this.field_149285_h = data.readInt();

    if (field_149286_i.length < this.field_149285_h) {
      field_149286_i = new byte[this.field_149285_h];
    }

    data.readBytes(field_149286_i, 0, this.field_149285_h);
    int i = 0;
    int j;
    int msb =
        0; // BugFix: MC does not read the MSB array from the packet properly, causing issues for
    // servers that use blocks > 256

    for (j = 0; j < 16; ++j) {
      i += this.field_149283_c >> j & 1;
      msb += this.field_149283_c >> j & 1;
    }

    j = 12288 * i;
    j += 2048 * msb;

    if (this.field_149279_g) {
      j += 256;
    }

    this.field_149278_f = new byte[j];
    Inflater inflater = new Inflater();
    inflater.setInput(field_149286_i, 0, this.field_149285_h);

    try {
      inflater.inflate(this.field_149278_f);
    } catch (DataFormatException dataformatexception) {
      throw new IOException("Bad compressed data format");
    } finally {
      inflater.end();
    }
  }
예제 #29
0
 /** Decompress the data bytes in the given message (in place). */
 private void _decompressMessageData(Message msg) {
   if ((msg.flags & Message.FLAGS_COMPRESSED) == 0) {
     throw new IllegalArgumentException("message data is not compressed");
   }
   Inflater decompresser = new Inflater();
   decompresser.setInput(msg.data);
   ByteArrayOutputStream bos = new ByteArrayOutputStream(msg.data.length);
   byte[] buffer = new byte[8192];
   try {
     while (!decompresser.finished()) {
       int size = decompresser.inflate(buffer);
       bos.write(buffer, 0, size);
     }
     msg.data = bos.toByteArray();
     msg.flags &= ~Message.FLAGS_COMPRESSED;
     decompresser.end();
   } catch (DataFormatException e) {
     throw new PyroException("invalid compressed data: ", e);
   }
 }
예제 #30
0
  private static byte[] inflate(byte[] input) throws IOException {

    ByteArrayOutputStream bos;
    Inflater decompressor = new Inflater();

    decompressor.setInput(input, 0, input.length);
    bos = new ByteArrayOutputStream(input.length);

    byte[] buf = new byte[CHUNKSIZE];

    try {
      while (!decompressor.finished()) {
        int count = decompressor.inflate(buf);
        bos.write(buf, 0, count);
      }
      return bos.toByteArray();
    } catch (DataFormatException e) {
      throw new IOException("failed to inflate data", e);
    }
  }