示例#1
0
  public byte[] compress(byte[] buf, int start, int[] len) {
    stream.next_in = buf;
    stream.next_in_index = start;
    stream.avail_in = len[0] - start;
    int status;
    int outputlen = start;
    byte[] outputbuf = buf;
    int tmp = 0;

    do {
      stream.next_out = tmpbuf;
      stream.next_out_index = 0;
      stream.avail_out = BUF_SIZE;
      status = stream.deflate(JZlib.Z_PARTIAL_FLUSH);
      switch (status) {
        case JZlib.Z_OK:
          tmp = BUF_SIZE - stream.avail_out;
          if (outputbuf.length < outputlen + tmp + buffer_margin) {
            byte[] foo = new byte[(outputlen + tmp + buffer_margin) * 2];
            System.arraycopy(outputbuf, 0, foo, 0, outputbuf.length);
            outputbuf = foo;
          }
          System.arraycopy(tmpbuf, 0, outputbuf, outputlen, tmp);
          outputlen += tmp;
          break;
        default:
          System.err.println("compress: deflate returnd " + status);
      }
    } while (stream.avail_out == 0);

    len[0] = outputlen;
    return outputbuf;
  }
示例#2
0
  public int compress(byte[] buf, int start, int len, byte[] output) {
    deflate.next_in = buf;
    deflate.next_in_index = start;
    deflate.avail_in = len - start;

    if ((buf.length + 1024) > deflate_tmpbuf.length) {
      deflate_tmpbuf = new byte[buf.length + 1024];
    }

    deflate.next_out = deflate_tmpbuf;
    deflate.next_out_index = 0;
    deflate.avail_out = output.length;

    if (deflate.deflate(JZlib.Z_PARTIAL_FLUSH) != JZlib.Z_OK) {
      System.err.println("compress: compression failure");
    }

    if (deflate.avail_in > 0) {
      System.err.println("compress: deflated data too large");
    }

    int outputlen = output.length - deflate.avail_out;

    System.arraycopy(deflate_tmpbuf, 0, output, 0, outputlen);

    return outputlen;
  }
示例#3
0
 @Override
 public void compress(Buffer buffer) {
   stream.next_in = buffer.array();
   stream.next_in_index = buffer.rpos();
   stream.avail_in = buffer.available();
   buffer.wpos(buffer.rpos());
   do {
     stream.next_out = tempBuf;
     stream.next_out_index = 0;
     stream.avail_out = BUF_SIZE;
     final int status = stream.deflate(JZlib.Z_PARTIAL_FLUSH);
     if (status == JZlib.Z_OK) {
       buffer.putRawBytes(tempBuf, 0, BUF_SIZE - stream.avail_out);
     } else {
       throw new SSHRuntimeException("compress: deflate returned " + status);
     }
   } while (stream.avail_out == 0);
 }
示例#4
0
  public byte[] uncompress(byte[] buffer, int start, int[] length) {
    int inflated_end = 0;

    stream.next_in = buffer;
    stream.next_in_index = start;
    stream.avail_in = length[0];

    while (true) {
      stream.next_out = tmpbuf;
      stream.next_out_index = 0;
      stream.avail_out = BUF_SIZE;
      int status = stream.inflate(JZlib.Z_PARTIAL_FLUSH);
      switch (status) {
        case JZlib.Z_OK:
          if (inflated_buf.length < inflated_end + BUF_SIZE - stream.avail_out) {
            int len = inflated_buf.length * 2;
            if (len < inflated_end + BUF_SIZE - stream.avail_out)
              len = inflated_end + BUF_SIZE - stream.avail_out;
            byte[] foo = new byte[len];
            System.arraycopy(inflated_buf, 0, foo, 0, inflated_end);
            inflated_buf = foo;
          }
          System.arraycopy(tmpbuf, 0, inflated_buf, inflated_end, BUF_SIZE - stream.avail_out);
          inflated_end += (BUF_SIZE - stream.avail_out);
          length[0] = inflated_end;
          break;
        case JZlib.Z_BUF_ERROR:
          if (inflated_end > buffer.length - start) {
            byte[] foo = new byte[inflated_end + start];
            System.arraycopy(buffer, 0, foo, 0, start);
            System.arraycopy(inflated_buf, 0, foo, start, inflated_end);
            buffer = foo;
          } else {
            System.arraycopy(inflated_buf, 0, buffer, start, inflated_end);
          }
          length[0] = inflated_end;
          return buffer;
        default:
          System.err.println("uncompress: inflate returnd " + status);
          return null;
      }
    }
  }
示例#5
0
 @Override
 public void uncompress(Buffer from, Buffer to) throws TransportException {
   stream.next_in = from.array();
   stream.next_in_index = from.rpos();
   stream.avail_in = from.available();
   while (true) {
     stream.next_out = tempBuf;
     stream.next_out_index = 0;
     stream.avail_out = BUF_SIZE;
     final int status = stream.inflate(JZlib.Z_PARTIAL_FLUSH);
     switch (status) {
       case JZlib.Z_OK:
         to.putRawBytes(tempBuf, 0, BUF_SIZE - stream.avail_out);
         break;
       case JZlib.Z_BUF_ERROR:
         return;
       default:
         throw new TransportException(
             DisconnectReason.COMPRESSION_ERROR, "uncompress: inflate returned " + status);
     }
   }
 }
示例#6
0
  public int compress(byte[] buf, int start, int len) {
    stream.next_in = buf;
    stream.next_in_index = start;
    stream.avail_in = len - start;
    int status;
    int outputlen = start;

    do {
      stream.next_out = tmpbuf;
      stream.next_out_index = 0;
      stream.avail_out = BUF_SIZE;
      status = stream.deflate(JZlib.Z_PARTIAL_FLUSH);
      switch (status) {
        case JZlib.Z_OK:
          System.arraycopy(tmpbuf, 0, buf, outputlen, BUF_SIZE - stream.avail_out);
          outputlen += (BUF_SIZE - stream.avail_out);
          break;
        default:
          System.err.println("compress: deflate returnd " + status);
      }
    } while (stream.avail_out == 0);
    return outputlen;
  }