Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ZInputStream.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
  2. /*
  3. Copyright (c) 2001 Lapo Luchini.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. 1. Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. 2. Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in
  10. the documentation and/or other materials provided with the distribution.
  11. 3. The names of the authors may not be used to endorse or promote products
  12. derived from this software without specific prior written permission.
  13. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  14. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  15. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS
  16. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
  17. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  18. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  19. OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  20. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  21. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  22. EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. /*
  25. * This program is based on zlib-1.1.3, so all credit should go authors
  26. * Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu)
  27. * and contributors of zlib.
  28. */
  29. package com.jcraft.jzlib;
  30. import java.io.*;
  31. public class ZInputStream extends FilterInputStream {
  32. protected ZStream z=new ZStream();
  33. protected int bufsize=512;
  34. protected int flush=JZlib.Z_NO_FLUSH;
  35. protected byte[] buf=new byte[bufsize],
  36. buf1=new byte[1];
  37. protected boolean compress;
  38. protected InputStream in=null;
  39. public ZInputStream(InputStream in) {
  40. this(in, false);
  41. }
  42. public ZInputStream(InputStream in, boolean nowrap) {
  43. super(in);
  44. this.in=in;
  45. z.inflateInit(nowrap);
  46. compress=false;
  47. z.next_in=buf;
  48. z.next_in_index=0;
  49. z.avail_in=0;
  50. }
  51. public ZInputStream(InputStream in, int level) {
  52. super(in);
  53. this.in=in;
  54. z.deflateInit(level);
  55. compress=true;
  56. z.next_in=buf;
  57. z.next_in_index=0;
  58. z.avail_in=0;
  59. }
  60. /*public int available() throws IOException {
  61. return inf.finished() ? 0 : 1;
  62. }*/
  63. public int read() throws IOException {
  64. if(read(buf1, 0, 1)==-1)
  65. return(-1);
  66. return(buf1[0]&0xFF);
  67. }
  68. private boolean nomoreinput=false;
  69. public int read(byte[] b, int off, int len) throws IOException {
  70. if(len==0)
  71. return(0);
  72. int err;
  73. z.next_out=b;
  74. z.next_out_index=off;
  75. z.avail_out=len;
  76. do {
  77. if((z.avail_in==0)&&(!nomoreinput)) { // if buffer is empty and more input is avaiable, refill it
  78. z.next_in_index=0;
  79. z.avail_in=in.read(buf, 0, bufsize);//(bufsize<z.avail_out ? bufsize : z.avail_out));
  80. if(z.avail_in==-1) {
  81. z.avail_in=0;
  82. nomoreinput=true;
  83. }
  84. }
  85. if(compress)
  86. err=z.deflate(flush);
  87. else
  88. err=z.inflate(flush);
  89. if(nomoreinput&&(err==JZlib.Z_BUF_ERROR))
  90. return(-1);
  91. if(err!=JZlib.Z_OK && err!=JZlib.Z_STREAM_END)
  92. throw new ZStreamException((compress ? "de" : "in")+"flating: "+z.msg);
  93. if((nomoreinput||err==JZlib.Z_STREAM_END)&&(z.avail_out==len))
  94. return(-1);
  95. }
  96. while(z.avail_out==len&&err==JZlib.Z_OK);
  97. //System.err.print("("+(len-z.avail_out)+")");
  98. return(len-z.avail_out);
  99. }
  100. public long skip(long n) throws IOException {
  101. int len=512;
  102. if(n<len)
  103. len=(int)n;
  104. byte[] tmp=new byte[len];
  105. return((long)read(tmp));
  106. }
  107. public int getFlushMode() {
  108. return(flush);
  109. }
  110. public void setFlushMode(int flush) {
  111. this.flush=flush;
  112. }
  113. /**
  114. * Returns the total number of bytes input so far.
  115. */
  116. public long getTotalIn() {
  117. return z.total_in;
  118. }
  119. /**
  120. * Returns the total number of bytes output so far.
  121. */
  122. public long getTotalOut() {
  123. return z.total_out;
  124. }
  125. public void close() throws IOException{
  126. in.close();
  127. }
  128. }