Skip to content

Commit

Permalink
ArrayIndexOutOfBoundsException in Tree.d_code
Browse files Browse the repository at this point in the history
  * #9
  * fixed a bug in calculating the size of the pending buffer.
  * added a test case based on https://github.com/jglick/jzlib-9-demo
  • Loading branch information
ymnk committed Sep 11, 2013
1 parent 0fa43af commit 8b205d6
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/jcraft/jzlib/Deflate.java
Expand Up @@ -1384,7 +1384,7 @@ else if(windowBits > 15){
pending_buf = new byte[lit_bufsize*4];
pending_buf_size = lit_bufsize*4;

d_buf = lit_bufsize/2;
d_buf = lit_bufsize;
l_buf = (1+2)*lit_bufsize;

this.level = level;
Expand Down
Binary file added src/test/resources/jzlib.fail.gz
Binary file not shown.
36 changes: 35 additions & 1 deletion src/test/scala/GZIPIOStreamTest.scala
Expand Up @@ -5,6 +5,10 @@ import org.scalatest._
import org.scalatest.matchers.ShouldMatchers

import java.io._
import java.util.zip.CheckedOutputStream
import java.util.zip.CheckedInputStream
import java.util.zip.{GZIPInputStream => juzGZIPInputStream}
import java.util.zip.{CRC32 => juzCRC32}

import JZlib._

Expand All @@ -16,7 +20,7 @@ class GZIPIOStreamTest extends FlatSpec with BeforeAndAfter with ShouldMatchers
after {
}

behavior of "GZipOutputStream and GZipInputStream"
behavior of "GZIPOutputStream and GZIPInputStream"

it can "deflate and infate data." in {

Expand Down Expand Up @@ -53,4 +57,34 @@ class GZIPIOStreamTest extends FlatSpec with BeforeAndAfter with ShouldMatchers

crc32.getValue should equal(gis.getCRC.asInstanceOf[Long])
}

behavior of "GZIPOutputStream"

// https://github.com/ymnk/jzlib/issues/9
// https://github.com/jglick/jzlib-9-demo
it can "deflate some file without AIOOBE." in {
val pos = new PipedOutputStream()
val pis = new PipedInputStream(pos)
val csOut = new juzCRC32()
val gos = new GZIPOutputStream(pos)
val cos = new CheckedOutputStream(gos, csOut)

val t = new Thread() {
override def run = {
val fail = "/jzlib.fail.gz".fromResource
val fis = new juzGZIPInputStream(new ByteArrayInputStream(fail))
fis -> cos
cos.close()
}
}
t.start();

val gis = new GZIPInputStream(pis)
val csIn = new juzCRC32();
new CheckedInputStream(gis, csIn) -> new ByteArrayOutputStream()

t.join()

csIn.getValue() should equal(csOut.getValue)
}
}
18 changes: 15 additions & 3 deletions src/test/scala/package.scala
Expand Up @@ -5,13 +5,25 @@ import java.io._

package object jzlib {
implicit def readIS(is: InputStream) = new {
def ->(out: OutputStream)(implicit buf: Array[Byte]) = {
Stream.continually(is.read(buf)).
takeWhile(-1 !=).foreach(i => out.write(buf, 0, i))
def ->(out: OutputStream)
(implicit buf: Array[Byte] = new Array[Byte](1024)) = {
Stream.
continually(is.read(buf)).
takeWhile(-1 !=).
foreach(i => out.write(buf, 0, i))
is.close
}
}

// reading a resource file
implicit def fromResource(str: String ) = new {
def fromResource: Array[Byte] =
io.Source.
fromURL(getClass.getResource(str))(io.Codec.ISO8859).
map(_.toByte).
toArray
}

implicit def readArray(is: Array[Byte]) = new {
def ->(out: OutputStream)(implicit buf: Array[Byte]) = {
new ByteArrayInputStream(is) -> (out)
Expand Down

0 comments on commit 8b205d6

Please sign in to comment.