예제 #1
0
  // copy as much as possible from the sliding window to the output area
  int inflate_flush(ZStream z, int r) {
    int n;
    int p;
    int q;

    // local copies of source and destination pointers
    p = z.next_out_index;
    q = read;

    // compute number of bytes to copy as far as end of window
    n = ((q <= write ? write : end) - q);
    if (n > z.avail_out) n = z.avail_out;
    if (n != 0 && r == Z_BUF_ERROR) r = Z_OK;

    // update counters
    z.avail_out -= n;
    z.total_out += n;

    // update check information
    if (checkfn != null) z.adler = check = z._adler.adler32(check, window, q, n);

    // copy as far as end of window
    System.arraycopy(window, q, z.next_out, p, n);
    p += n;
    q += n;

    // see if more to copy at beginning of window
    if (q == end) {
      // wrap pointers
      q = 0;
      if (write == end) write = 0;

      // compute bytes to copy
      n = write - q;
      if (n > z.avail_out) n = z.avail_out;
      if (n != 0 && r == Z_BUF_ERROR) r = Z_OK;

      // update counters
      z.avail_out -= n;
      z.total_out += n;

      // update check information
      if (checkfn != null) z.adler = check = z._adler.adler32(check, window, q, n);

      // copy
      System.arraycopy(window, q, z.next_out, p, n);
      p += n;
      q += n;
    }

    // update pointers
    z.next_out_index = p;
    read = q;

    // done
    return r;
  }
예제 #2
0
파일: Compression.java 프로젝트: hyee/JSch
  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;
  }
예제 #3
0
 @Override
 public void write(byte b[], int off, int len) throws IOException {
   if (len == 0) return;
   int err;
   z.next_in = b;
   z.next_in_index = off;
   z.avail_in = len;
   do {
     z.next_out = buf;
     z.next_out_index = 0;
     z.avail_out = bufsize;
     if (compress) err = z.deflate(flush);
     else err = z.inflate(flush);
     if (err != JZlib.Z_OK)
       throw new ZStreamException((compress ? "de" : "in") + "flating: " + z.msg);
     out.write(buf, 0, bufsize - z.avail_out);
   } while (z.avail_in > 0 || z.avail_out == 0);
 }
예제 #4
0
파일: Compression.java 프로젝트: hyee/JSch
  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
 public void finish() throws IOException {
   int err;
   do {
     z.next_out = buf;
     z.next_out_index = 0;
     z.avail_out = bufsize;
     if (compress) {
       err = z.deflate(JZlib.Z_FINISH);
     } else {
       err = z.inflate(JZlib.Z_FINISH);
     }
     if (err != JZlib.Z_STREAM_END && err != JZlib.Z_OK)
       throw new ZStreamException((compress ? "de" : "in") + "flating: " + z.msg);
     if (bufsize - z.avail_out > 0) {
       out.write(buf, 0, bufsize - z.avail_out);
     }
   } while (z.avail_in > 0 || z.avail_out == 0);
   flush();
 }
예제 #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;
  }