You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by se...@apache.org on 2010/09/10 18:33:42 UTC
svn commit: r995859 [7/30] - in
/commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan: ./ color/
common/ common/byteSources/ common/mylzw/ formats/bmp/
formats/bmp/pixelparsers/ formats/bmp/writers/ formats/gif/ formats/ico/
formats/jpeg/ fo...
Modified: commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/byteSources/ByteSourceArray.java
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/byteSources/ByteSourceArray.java?rev=995859&r1=995858&r2=995859&view=diff
==============================================================================
--- commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/byteSources/ByteSourceArray.java (original)
+++ commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/byteSources/ByteSourceArray.java Fri Sep 10 16:33:35 2010
@@ -22,52 +22,52 @@ import java.io.InputStream;
public class ByteSourceArray extends ByteSource
{
- private final byte bytes[];
+ private final byte bytes[];
- public ByteSourceArray(String filename, byte bytes[])
- {
- super(filename);
- this.bytes = bytes;
- }
-
- public ByteSourceArray(byte bytes[])
- {
- super(null);
- this.bytes = bytes;
- }
-
- public InputStream getInputStream()
- {
- return new ByteArrayInputStream(bytes);
- }
-
- public byte[] getBlock(int start, int length) throws IOException
- {
- // We include a separate check for int overflow.
- if ((start < 0) || (length < 0) || (start + length < 0) || (start + length > bytes.length)) {
- throw new IOException("Could not read block (block start: " + start
- + ", block length: " + length + ", data length: "
- + bytes.length + ").");
- }
-
- byte result[] = new byte[length];
- System.arraycopy(bytes, start, result, 0, length);
- return result;
- }
-
- public long getLength()
- {
- return bytes.length;
- }
-
- public byte[] getAll() throws IOException
- {
- return bytes;
- }
-
- public String getDescription()
- {
- return bytes.length + " byte array";
- }
+ public ByteSourceArray(String filename, byte bytes[])
+ {
+ super(filename);
+ this.bytes = bytes;
+ }
+
+ public ByteSourceArray(byte bytes[])
+ {
+ super(null);
+ this.bytes = bytes;
+ }
+
+ public InputStream getInputStream()
+ {
+ return new ByteArrayInputStream(bytes);
+ }
+
+ public byte[] getBlock(int start, int length) throws IOException
+ {
+ // We include a separate check for int overflow.
+ if ((start < 0) || (length < 0) || (start + length < 0) || (start + length > bytes.length)) {
+ throw new IOException("Could not read block (block start: " + start
+ + ", block length: " + length + ", data length: "
+ + bytes.length + ").");
+ }
+
+ byte result[] = new byte[length];
+ System.arraycopy(bytes, start, result, 0, length);
+ return result;
+ }
+
+ public long getLength()
+ {
+ return bytes.length;
+ }
+
+ public byte[] getAll() throws IOException
+ {
+ return bytes;
+ }
+
+ public String getDescription()
+ {
+ return bytes.length + " byte array";
+ }
}
\ No newline at end of file
Modified: commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/byteSources/ByteSourceFile.java
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/byteSources/ByteSourceFile.java?rev=995859&r1=995858&r2=995859&view=diff
==============================================================================
--- commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/byteSources/ByteSourceFile.java (original)
+++ commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/byteSources/ByteSourceFile.java Fri Sep 10 16:33:35 2010
@@ -28,96 +28,96 @@ import org.apache.sanselan.util.Debug;
public class ByteSourceFile extends ByteSource
{
- private final File file;
+ private final File file;
- public ByteSourceFile(File file)
- {
- super(file.getName());
- this.file = file;
- }
-
- public InputStream getInputStream() throws IOException
- {
- FileInputStream is = null;
- BufferedInputStream bis = null;
- is = new FileInputStream(file);
- bis = new BufferedInputStream(is);
- return bis;
- }
-
- public byte[] getBlock(int start, int length) throws IOException
- {
-
- RandomAccessFile raf = null;
- try
- {
- raf = new RandomAccessFile(file, "r");
-
- // We include a separate check for int overflow.
- if ((start < 0) || (length < 0) || (start + length < 0) || (start + length > raf.length())) {
- throw new IOException("Could not read block (block start: " + start
- + ", block length: " + length + ", data length: "
- + raf.length() + ").");
- }
-
- return getRAFBytes(raf, start, length,
- "Could not read value from file");
- }
- finally
- {
- try
- {
- if (raf != null) {
- raf.close();
- }
- }
- catch (Exception e)
- {
- Debug.debug(e);
- }
-
- }
- }
-
- public long getLength()
- {
- return file.length();
- }
-
- public byte[] getAll() throws IOException
- {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
-
- InputStream is = null;
- try
- {
- is = new FileInputStream(file);
- is = new BufferedInputStream(is);
- byte buffer[] = new byte[1024];
- int read;
- while ((read = is.read(buffer)) > 0)
- {
- baos.write(buffer, 0, read);
- }
- return baos.toByteArray();
- }
- finally
- {
- try
- {
- if (null != is)
- is.close();
- }
- catch (IOException e)
- {
- // Debug.d
- }
- }
- }
-
- public String getDescription()
- {
- return "File: '" + file.getAbsolutePath() + "'";
- }
+ public ByteSourceFile(File file)
+ {
+ super(file.getName());
+ this.file = file;
+ }
+
+ public InputStream getInputStream() throws IOException
+ {
+ FileInputStream is = null;
+ BufferedInputStream bis = null;
+ is = new FileInputStream(file);
+ bis = new BufferedInputStream(is);
+ return bis;
+ }
+
+ public byte[] getBlock(int start, int length) throws IOException
+ {
+
+ RandomAccessFile raf = null;
+ try
+ {
+ raf = new RandomAccessFile(file, "r");
+
+ // We include a separate check for int overflow.
+ if ((start < 0) || (length < 0) || (start + length < 0) || (start + length > raf.length())) {
+ throw new IOException("Could not read block (block start: " + start
+ + ", block length: " + length + ", data length: "
+ + raf.length() + ").");
+ }
+
+ return getRAFBytes(raf, start, length,
+ "Could not read value from file");
+ }
+ finally
+ {
+ try
+ {
+ if (raf != null) {
+ raf.close();
+ }
+ }
+ catch (Exception e)
+ {
+ Debug.debug(e);
+ }
+
+ }
+ }
+
+ public long getLength()
+ {
+ return file.length();
+ }
+
+ public byte[] getAll() throws IOException
+ {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+
+ InputStream is = null;
+ try
+ {
+ is = new FileInputStream(file);
+ is = new BufferedInputStream(is);
+ byte buffer[] = new byte[1024];
+ int read;
+ while ((read = is.read(buffer)) > 0)
+ {
+ baos.write(buffer, 0, read);
+ }
+ return baos.toByteArray();
+ }
+ finally
+ {
+ try
+ {
+ if (null != is)
+ is.close();
+ }
+ catch (IOException e)
+ {
+ // Debug.d
+ }
+ }
+ }
+
+ public String getDescription()
+ {
+ return "File: '" + file.getAbsolutePath() + "'";
+ }
}
\ No newline at end of file
Modified: commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/byteSources/ByteSourceInputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/byteSources/ByteSourceInputStream.java?rev=995859&r1=995858&r2=995859&view=diff
==============================================================================
--- commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/byteSources/ByteSourceInputStream.java (original)
+++ commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/byteSources/ByteSourceInputStream.java Fri Sep 10 16:33:35 2010
@@ -23,210 +23,210 @@ import java.io.InputStream;
public class ByteSourceInputStream extends ByteSource
{
- private final InputStream is;
- private CacheBlock cacheHead = null;
- private static final int BLOCK_SIZE = 1024;
-
- public ByteSourceInputStream(InputStream is, String filename)
- {
- super(filename);
- this.is = new BufferedInputStream(is);
- }
-
- private class CacheBlock
- {
- public final byte bytes[];
- private CacheBlock next = null;
- private boolean triedNext = false;
-
- public CacheBlock(final byte[] bytes)
- {
- this.bytes = bytes;
- }
-
- public CacheBlock getNext() throws IOException
- {
- if (null != next)
- return next;
- if (triedNext)
- return null;
- triedNext = true;
- next = readBlock();
- return next;
- }
-
- }
-
- private byte readBuffer[] = null;
-
- private CacheBlock readBlock() throws IOException
- {
- if (null == readBuffer)
- readBuffer = new byte[BLOCK_SIZE];
-
- int read = is.read(readBuffer);
- if (read < 1)
- return null;
- else if (read < BLOCK_SIZE)
- {
- // return a copy.
- byte result[] = new byte[read];
- System.arraycopy(readBuffer, 0, result, 0, read);
- return new CacheBlock(result);
- }
- else
- {
- // return current buffer.
- byte result[] = readBuffer;
- readBuffer = null;
- return new CacheBlock(result);
- }
- }
-
- private CacheBlock getFirstBlock() throws IOException
- {
- if (null == cacheHead)
- cacheHead = readBlock();
- return cacheHead;
- }
-
- private class CacheReadingInputStream extends InputStream
- {
- private CacheBlock block = null;
- private boolean readFirst = false;
- private int blockIndex = 0;
-
- public int read() throws IOException
- {
- if (null == block)
- {
- if (readFirst)
- return -1;
- block = getFirstBlock();
- readFirst = true;
- }
-
- if (block != null && blockIndex >= block.bytes.length)
- {
- block = block.getNext();
- blockIndex = 0;
- }
-
- if (null == block)
- return -1;
-
- if (blockIndex >= block.bytes.length)
- return -1;
-
- return 0xff & block.bytes[blockIndex++];
- }
-
- public int read(byte b[], int off, int len) throws IOException
- {
- // first section copied verbatim from InputStream
- if (b == null)
- throw new NullPointerException();
- else if ((off < 0) || (off > b.length) || (len < 0)
- || ((off + len) > b.length) || ((off + len) < 0))
- throw new IndexOutOfBoundsException();
- else if (len == 0)
- return 0;
-
- // optimized block read
-
- if (null == block)
- {
- if (readFirst)
- return -1;
- block = getFirstBlock();
- readFirst = true;
- }
-
- if (block != null && blockIndex >= block.bytes.length)
- {
- block = block.getNext();
- blockIndex = 0;
- }
-
- if (null == block)
- return -1;
-
- if (blockIndex >= block.bytes.length)
- return -1;
-
- int readSize = Math.min(len, block.bytes.length - blockIndex);
- System.arraycopy(block.bytes, blockIndex, b, off, readSize);
- blockIndex += readSize;
- return readSize;
- }
-
- }
-
- public InputStream getInputStream() throws IOException
- {
- return new CacheReadingInputStream();
- }
-
- public byte[] getBlock(int blockStart, int blockLength) throws IOException
- {
- // We include a separate check for int overflow.
- if ((blockStart < 0)
- || (blockLength < 0)
- || (blockStart + blockLength < 0)
- || (blockStart + blockLength > streamLength.longValue())) {
- throw new IOException("Could not read block (block start: " + blockStart
- + ", block length: " + blockLength + ", data length: "
- + streamLength + ").");
- }
-
- InputStream is = getInputStream();
- is.skip(blockStart);
-
- byte bytes[] = new byte[blockLength];
- int total = 0;
- while (true)
- {
- int read = is.read(bytes, total, bytes.length - total);
- if (read < 1)
- throw new IOException("Could not read block.");
- total += read;
- if (total >= blockLength)
- return bytes;
- }
- }
-
- private Long streamLength = null;
-
- public long getLength() throws IOException
- {
- if (streamLength != null)
- return streamLength.longValue();
-
- InputStream is = getInputStream();
- long result = 0;
- long skipped;
- while ((skipped = is.skip(1024)) > 0)
- result += skipped;
- streamLength = new Long(result);
- return result;
- }
-
- public byte[] getAll() throws IOException
- {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
-
- CacheBlock block = getFirstBlock();
- while (block != null)
- {
- baos.write(block.bytes);
- block = block.getNext();
- }
- return baos.toByteArray();
- }
-
- public String getDescription()
- {
- return "Inputstream: '" + filename + "'";
- }
+ private final InputStream is;
+ private CacheBlock cacheHead = null;
+ private static final int BLOCK_SIZE = 1024;
+
+ public ByteSourceInputStream(InputStream is, String filename)
+ {
+ super(filename);
+ this.is = new BufferedInputStream(is);
+ }
+
+ private class CacheBlock
+ {
+ public final byte bytes[];
+ private CacheBlock next = null;
+ private boolean triedNext = false;
+
+ public CacheBlock(final byte[] bytes)
+ {
+ this.bytes = bytes;
+ }
+
+ public CacheBlock getNext() throws IOException
+ {
+ if (null != next)
+ return next;
+ if (triedNext)
+ return null;
+ triedNext = true;
+ next = readBlock();
+ return next;
+ }
+
+ }
+
+ private byte readBuffer[] = null;
+
+ private CacheBlock readBlock() throws IOException
+ {
+ if (null == readBuffer)
+ readBuffer = new byte[BLOCK_SIZE];
+
+ int read = is.read(readBuffer);
+ if (read < 1)
+ return null;
+ else if (read < BLOCK_SIZE)
+ {
+ // return a copy.
+ byte result[] = new byte[read];
+ System.arraycopy(readBuffer, 0, result, 0, read);
+ return new CacheBlock(result);
+ }
+ else
+ {
+ // return current buffer.
+ byte result[] = readBuffer;
+ readBuffer = null;
+ return new CacheBlock(result);
+ }
+ }
+
+ private CacheBlock getFirstBlock() throws IOException
+ {
+ if (null == cacheHead)
+ cacheHead = readBlock();
+ return cacheHead;
+ }
+
+ private class CacheReadingInputStream extends InputStream
+ {
+ private CacheBlock block = null;
+ private boolean readFirst = false;
+ private int blockIndex = 0;
+
+ public int read() throws IOException
+ {
+ if (null == block)
+ {
+ if (readFirst)
+ return -1;
+ block = getFirstBlock();
+ readFirst = true;
+ }
+
+ if (block != null && blockIndex >= block.bytes.length)
+ {
+ block = block.getNext();
+ blockIndex = 0;
+ }
+
+ if (null == block)
+ return -1;
+
+ if (blockIndex >= block.bytes.length)
+ return -1;
+
+ return 0xff & block.bytes[blockIndex++];
+ }
+
+ public int read(byte b[], int off, int len) throws IOException
+ {
+ // first section copied verbatim from InputStream
+ if (b == null)
+ throw new NullPointerException();
+ else if ((off < 0) || (off > b.length) || (len < 0)
+ || ((off + len) > b.length) || ((off + len) < 0))
+ throw new IndexOutOfBoundsException();
+ else if (len == 0)
+ return 0;
+
+ // optimized block read
+
+ if (null == block)
+ {
+ if (readFirst)
+ return -1;
+ block = getFirstBlock();
+ readFirst = true;
+ }
+
+ if (block != null && blockIndex >= block.bytes.length)
+ {
+ block = block.getNext();
+ blockIndex = 0;
+ }
+
+ if (null == block)
+ return -1;
+
+ if (blockIndex >= block.bytes.length)
+ return -1;
+
+ int readSize = Math.min(len, block.bytes.length - blockIndex);
+ System.arraycopy(block.bytes, blockIndex, b, off, readSize);
+ blockIndex += readSize;
+ return readSize;
+ }
+
+ }
+
+ public InputStream getInputStream() throws IOException
+ {
+ return new CacheReadingInputStream();
+ }
+
+ public byte[] getBlock(int blockStart, int blockLength) throws IOException
+ {
+ // We include a separate check for int overflow.
+ if ((blockStart < 0)
+ || (blockLength < 0)
+ || (blockStart + blockLength < 0)
+ || (blockStart + blockLength > streamLength.longValue())) {
+ throw new IOException("Could not read block (block start: " + blockStart
+ + ", block length: " + blockLength + ", data length: "
+ + streamLength + ").");
+ }
+
+ InputStream is = getInputStream();
+ is.skip(blockStart);
+
+ byte bytes[] = new byte[blockLength];
+ int total = 0;
+ while (true)
+ {
+ int read = is.read(bytes, total, bytes.length - total);
+ if (read < 1)
+ throw new IOException("Could not read block.");
+ total += read;
+ if (total >= blockLength)
+ return bytes;
+ }
+ }
+
+ private Long streamLength = null;
+
+ public long getLength() throws IOException
+ {
+ if (streamLength != null)
+ return streamLength.longValue();
+
+ InputStream is = getInputStream();
+ long result = 0;
+ long skipped;
+ while ((skipped = is.skip(1024)) > 0)
+ result += skipped;
+ streamLength = new Long(result);
+ return result;
+ }
+
+ public byte[] getAll() throws IOException
+ {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+
+ CacheBlock block = getFirstBlock();
+ while (block != null)
+ {
+ baos.write(block.bytes);
+ block = block.getNext();
+ }
+ return baos.toByteArray();
+ }
+
+ public String getDescription()
+ {
+ return "Inputstream: '" + filename + "'";
+ }
}
\ No newline at end of file
Modified: commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/mylzw/BitsToByteInputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/mylzw/BitsToByteInputStream.java?rev=995859&r1=995858&r2=995859&view=diff
==============================================================================
--- commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/mylzw/BitsToByteInputStream.java (original)
+++ commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/mylzw/BitsToByteInputStream.java Fri Sep 10 16:33:35 2010
@@ -21,38 +21,38 @@ import java.io.InputStream;
public class BitsToByteInputStream extends InputStream
{
- private final MyBitInputStream is;
- private final int desiredDepth;
+ private final MyBitInputStream is;
+ private final int desiredDepth;
- public BitsToByteInputStream(MyBitInputStream is, int desiredDepth)
- {
- this.is = is;
- this.desiredDepth = desiredDepth;
- }
-
- public int read() throws IOException
- {
- return readBits(8);
- }
-
- public int readBits(int bitCount) throws IOException
- {
- int i = is.readBits(bitCount);
- if (bitCount < desiredDepth)
- i <<= (desiredDepth - bitCount);
- else if (bitCount > desiredDepth)
- i >>= (bitCount - desiredDepth);
-
- return i;
- }
-
- public int[] readBitsArray(int sampleBits, int length) throws IOException
- {
- int result[] = new int[length];
+ public BitsToByteInputStream(MyBitInputStream is, int desiredDepth)
+ {
+ this.is = is;
+ this.desiredDepth = desiredDepth;
+ }
+
+ public int read() throws IOException
+ {
+ return readBits(8);
+ }
+
+ public int readBits(int bitCount) throws IOException
+ {
+ int i = is.readBits(bitCount);
+ if (bitCount < desiredDepth)
+ i <<= (desiredDepth - bitCount);
+ else if (bitCount > desiredDepth)
+ i >>= (bitCount - desiredDepth);
+
+ return i;
+ }
+
+ public int[] readBitsArray(int sampleBits, int length) throws IOException
+ {
+ int result[] = new int[length];
- for (int i = 0; i < length; i++)
- result[i] = readBits(sampleBits);
+ for (int i = 0; i < length; i++)
+ result[i] = readBits(sampleBits);
- return result;
- }
+ return result;
+ }
}
\ No newline at end of file
Modified: commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/mylzw/MyBitInputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/mylzw/MyBitInputStream.java?rev=995859&r1=995858&r2=995859&view=diff
==============================================================================
--- commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/mylzw/MyBitInputStream.java (original)
+++ commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/mylzw/MyBitInputStream.java Fri Sep 10 16:33:35 2010
@@ -23,92 +23,92 @@ import org.apache.sanselan.common.Binary
public class MyBitInputStream extends InputStream implements BinaryConstants
{
- private final InputStream is;
- private final int byteOrder;
- private boolean tiffLZWMode = false;
-
- public MyBitInputStream(InputStream is, int byteOrder)
- {
- this.byteOrder = byteOrder;
- this.is = is;
- }
-
- public int read() throws IOException
- {
- return readBits(8);
- }
-
- private long bytesRead = 0;
- private int bitsInCache = 0;
- private int bitCache = 0;
-
- public void setTiffLZWMode()
- {
- tiffLZWMode = true;
- }
-
- public int readBits(int SampleBits) throws IOException
- {
- while (bitsInCache < SampleBits)
- {
- int next = is.read();
-
- if (next < 0)
- {
- if (tiffLZWMode)
- {
- // pernicious special case!
- return 257;
- }
- return -1;
- }
-
- int newByte = (0xff & next);
-
- if (byteOrder == BYTE_ORDER_NETWORK) // MSB, so add to right
- bitCache = (bitCache << 8) | newByte;
- else if (byteOrder == BYTE_ORDER_INTEL) // LSB, so add to left
- bitCache = (newByte << bitsInCache) | bitCache;
- else
- throw new IOException("Unknown byte order: " + byteOrder);
-
- bytesRead++;
- bitsInCache += 8;
- }
- int sampleMask = (1 << SampleBits) - 1;
-
- int sample;
-
- if (byteOrder == BYTE_ORDER_NETWORK) // MSB, so read from left
- {
- sample = sampleMask & (bitCache >> (bitsInCache - SampleBits));
- }
- else if (byteOrder == BYTE_ORDER_INTEL) // LSB, so read from right
- {
- sample = sampleMask & bitCache;
- bitCache >>= SampleBits;
- }
- else
- throw new IOException("Unknown byte order: " + byteOrder);
-
- int result = sample;
-
- bitsInCache -= SampleBits;
- int remainderMask = (1 << bitsInCache) - 1;
- bitCache &= remainderMask;
-
- return result;
- }
-
- public void flushCache()
- {
- bitsInCache = 0;
- bitCache = 0;
- }
-
- public long getBytesRead()
- {
- return bytesRead;
- }
+ private final InputStream is;
+ private final int byteOrder;
+ private boolean tiffLZWMode = false;
+
+ public MyBitInputStream(InputStream is, int byteOrder)
+ {
+ this.byteOrder = byteOrder;
+ this.is = is;
+ }
+
+ public int read() throws IOException
+ {
+ return readBits(8);
+ }
+
+ private long bytesRead = 0;
+ private int bitsInCache = 0;
+ private int bitCache = 0;
+
+ public void setTiffLZWMode()
+ {
+ tiffLZWMode = true;
+ }
+
+ public int readBits(int SampleBits) throws IOException
+ {
+ while (bitsInCache < SampleBits)
+ {
+ int next = is.read();
+
+ if (next < 0)
+ {
+ if (tiffLZWMode)
+ {
+ // pernicious special case!
+ return 257;
+ }
+ return -1;
+ }
+
+ int newByte = (0xff & next);
+
+ if (byteOrder == BYTE_ORDER_NETWORK) // MSB, so add to right
+ bitCache = (bitCache << 8) | newByte;
+ else if (byteOrder == BYTE_ORDER_INTEL) // LSB, so add to left
+ bitCache = (newByte << bitsInCache) | bitCache;
+ else
+ throw new IOException("Unknown byte order: " + byteOrder);
+
+ bytesRead++;
+ bitsInCache += 8;
+ }
+ int sampleMask = (1 << SampleBits) - 1;
+
+ int sample;
+
+ if (byteOrder == BYTE_ORDER_NETWORK) // MSB, so read from left
+ {
+ sample = sampleMask & (bitCache >> (bitsInCache - SampleBits));
+ }
+ else if (byteOrder == BYTE_ORDER_INTEL) // LSB, so read from right
+ {
+ sample = sampleMask & bitCache;
+ bitCache >>= SampleBits;
+ }
+ else
+ throw new IOException("Unknown byte order: " + byteOrder);
+
+ int result = sample;
+
+ bitsInCache -= SampleBits;
+ int remainderMask = (1 << bitsInCache) - 1;
+ bitCache &= remainderMask;
+
+ return result;
+ }
+
+ public void flushCache()
+ {
+ bitsInCache = 0;
+ bitCache = 0;
+ }
+
+ public long getBytesRead()
+ {
+ return bytesRead;
+ }
}
\ No newline at end of file
Modified: commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/mylzw/MyBitOutputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/mylzw/MyBitOutputStream.java?rev=995859&r1=995858&r2=995859&view=diff
==============================================================================
--- commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/mylzw/MyBitOutputStream.java (original)
+++ commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/mylzw/MyBitOutputStream.java Fri Sep 10 16:33:35 2010
@@ -23,98 +23,98 @@ import org.apache.sanselan.common.Binary
public class MyBitOutputStream extends OutputStream implements BinaryConstants
{
- private final OutputStream os;
- private final int byteOrder;
+ private final OutputStream os;
+ private final int byteOrder;
- public MyBitOutputStream(OutputStream os, int byteOrder)
- {
- this.byteOrder = byteOrder;
- this.os = os;
- }
-
- public void write(int value) throws IOException
- {
- writeBits(value, 8);
- }
-
- private int bitsInCache = 0;
- private int bitCache = 0;
-
- // TODO: in and out streams CANNOT accurately read/write 32bits at a time,
- // as int will overflow. should have used a long
- public void writeBits(int value, int SampleBits) throws IOException
- {
- int sampleMask = (1 << SampleBits) - 1;
- value &= sampleMask;
-
- if (byteOrder == BYTE_ORDER_NETWORK) // MSB, so add to right
- {
- bitCache = (bitCache << SampleBits) | value;
- }
- else if (byteOrder == BYTE_ORDER_INTEL) // LSB, so add to left
- {
- bitCache = bitCache | (value << bitsInCache);
- }
- else
- throw new IOException("Unknown byte order: " + byteOrder);
- bitsInCache += SampleBits;
-
- while (bitsInCache >= 8)
- {
- if (byteOrder == BYTE_ORDER_NETWORK) // MSB, so write from left
- {
- int b = 0xff & (bitCache >> (bitsInCache - 8));
- actualWrite(b);
-
- bitsInCache -= 8;
- }
- else if (byteOrder == BYTE_ORDER_INTEL) // LSB, so write from right
- {
- int b = 0xff & bitCache;
- actualWrite(b);
-
- bitCache >>= 8;
- bitsInCache -= 8;
- }
- int remainderMask = (1 << bitsInCache) - 1; // unneccesary
- bitCache &= remainderMask; // unneccesary
- }
-
- }
-
- private int bytesWritten = 0;
-
- private void actualWrite(int value) throws IOException
- {
- os.write(value);
- bytesWritten++;
- }
-
- public void flushCache() throws IOException
- {
- if (bitsInCache > 0)
- {
- int bitMask = (1 << bitsInCache) - 1;
- int b = bitMask & bitCache;
-
- if (byteOrder == BYTE_ORDER_NETWORK) // MSB, so write from left
- {
- b <<= 8 - bitsInCache; // left align fragment.
- os.write(b);
- }
- else if (byteOrder == BYTE_ORDER_INTEL) // LSB, so write from right
- {
- os.write(b);
- }
- }
-
- bitsInCache = 0;
- bitCache = 0;
- }
-
- public int getBytesWritten()
- {
- return bytesWritten + ((bitsInCache > 0) ? 1 : 0);
- }
+ public MyBitOutputStream(OutputStream os, int byteOrder)
+ {
+ this.byteOrder = byteOrder;
+ this.os = os;
+ }
+
+ public void write(int value) throws IOException
+ {
+ writeBits(value, 8);
+ }
+
+ private int bitsInCache = 0;
+ private int bitCache = 0;
+
+ // TODO: in and out streams CANNOT accurately read/write 32bits at a time,
+ // as int will overflow. should have used a long
+ public void writeBits(int value, int SampleBits) throws IOException
+ {
+ int sampleMask = (1 << SampleBits) - 1;
+ value &= sampleMask;
+
+ if (byteOrder == BYTE_ORDER_NETWORK) // MSB, so add to right
+ {
+ bitCache = (bitCache << SampleBits) | value;
+ }
+ else if (byteOrder == BYTE_ORDER_INTEL) // LSB, so add to left
+ {
+ bitCache = bitCache | (value << bitsInCache);
+ }
+ else
+ throw new IOException("Unknown byte order: " + byteOrder);
+ bitsInCache += SampleBits;
+
+ while (bitsInCache >= 8)
+ {
+ if (byteOrder == BYTE_ORDER_NETWORK) // MSB, so write from left
+ {
+ int b = 0xff & (bitCache >> (bitsInCache - 8));
+ actualWrite(b);
+
+ bitsInCache -= 8;
+ }
+ else if (byteOrder == BYTE_ORDER_INTEL) // LSB, so write from right
+ {
+ int b = 0xff & bitCache;
+ actualWrite(b);
+
+ bitCache >>= 8;
+ bitsInCache -= 8;
+ }
+ int remainderMask = (1 << bitsInCache) - 1; // unneccesary
+ bitCache &= remainderMask; // unneccesary
+ }
+
+ }
+
+ private int bytesWritten = 0;
+
+ private void actualWrite(int value) throws IOException
+ {
+ os.write(value);
+ bytesWritten++;
+ }
+
+ public void flushCache() throws IOException
+ {
+ if (bitsInCache > 0)
+ {
+ int bitMask = (1 << bitsInCache) - 1;
+ int b = bitMask & bitCache;
+
+ if (byteOrder == BYTE_ORDER_NETWORK) // MSB, so write from left
+ {
+ b <<= 8 - bitsInCache; // left align fragment.
+ os.write(b);
+ }
+ else if (byteOrder == BYTE_ORDER_INTEL) // LSB, so write from right
+ {
+ os.write(b);
+ }
+ }
+
+ bitsInCache = 0;
+ bitCache = 0;
+ }
+
+ public int getBytesWritten()
+ {
+ return bytesWritten + ((bitsInCache > 0) ? 1 : 0);
+ }
}
\ No newline at end of file
Modified: commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/mylzw/MyLZWCompressor.java
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/mylzw/MyLZWCompressor.java?rev=995859&r1=995858&r2=995859&view=diff
==============================================================================
--- commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/mylzw/MyLZWCompressor.java (original)
+++ commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/mylzw/MyLZWCompressor.java Fri Sep 10 16:33:35 2010
@@ -24,270 +24,270 @@ import java.util.Map;
public class MyLZWCompressor
{
- // private static final int MAX_TABLE_SIZE = 1 << 12;
+ // private static final int MAX_TABLE_SIZE = 1 << 12;
- private int codeSize;
- private final int initialCodeSize;
- private int codes = -1;
-
- private final int byteOrder;
- private final boolean earlyLimit;
- private final int clearCode;
- private final int eoiCode;
- private final Listener listener;
-
- public MyLZWCompressor(int initialCodeSize, int byteOrder,
- boolean earlyLimit)
- {
- this(initialCodeSize, byteOrder, earlyLimit, null);
- }
-
- public MyLZWCompressor(int initialCodeSize, int byteOrder,
- boolean earlyLimit, Listener listener)
- {
- this.listener = listener;
- this.byteOrder = byteOrder;
- this.earlyLimit = earlyLimit;
-
- this.initialCodeSize = initialCodeSize;
-
- clearCode = 1 << initialCodeSize;
- eoiCode = clearCode + 1;
-
- if (null != listener)
- listener.init(clearCode, eoiCode);
-
- InitializeStringTable();
- }
-
- private final Map map = new HashMap();
-
- private final void InitializeStringTable()
- {
- codeSize = initialCodeSize;
-
- int intial_entries_count = (1 << codeSize) + 2;
-
- map.clear();
- for (codes = 0; codes < intial_entries_count; codes++)
- {
- if ((codes != clearCode) && (codes != eoiCode))
- {
- Object key = arrayToKey((byte) codes);
-
- map.put(key, new Integer(codes));
- }
- }
- }
-
- private final void clearTable()
- {
- InitializeStringTable();
- incrementCodeSize();
- }
-
- private final void incrementCodeSize()
- {
- if (codeSize != 12)
- codeSize++;
- }
-
- private final Object arrayToKey(byte b)
- {
- return arrayToKey(new byte[] { b, }, 0, 1);
- }
-
- private final static class ByteArray
- {
- private final byte bytes[];
- private final int start;
- private final int length;
- private final int hash;
-
- public ByteArray(byte bytes[])
- {
- this(bytes, 0, bytes.length);
- }
-
- public ByteArray(byte bytes[], int start, int length)
- {
- this.bytes = bytes;
- this.start = start;
- this.length = length;
-
- int tempHash = length;
-
- for (int i = 0; i < length; i++)
- {
- int b = 0xff & bytes[i + start];
- tempHash = tempHash + (tempHash << 8) ^ b ^ i;
- }
-
- hash = tempHash;
- }
-
- public final int hashCode()
- {
- return hash;
- }
-
- public final boolean equals(Object o)
- {
- ByteArray other = (ByteArray) o;
- if (other.hash != hash)
- return false;
- if (other.length != length)
- return false;
-
- for (int i = 0; i < length; i++)
- {
- if (other.bytes[i + other.start] != bytes[i + start])
- return false;
- }
-
- return true;
- }
- }
-
- private final Object arrayToKey(byte bytes[], int start, int length)
- {
- return new ByteArray(bytes, start, length);
- }
-
- private final void writeDataCode(MyBitOutputStream bos, int code)
- throws IOException
- {
- if (null != listener)
- listener.dataCode(code);
- writeCode(bos, code);
- }
-
-
- private final void writeClearCode(MyBitOutputStream bos) throws IOException
- {
- if (null != listener)
- listener.dataCode(clearCode);
- writeCode(bos, clearCode);
- }
-
- private final void writeEoiCode(MyBitOutputStream bos) throws IOException
- {
- if (null != listener)
- listener.eoiCode(eoiCode);
- writeCode(bos, eoiCode);
- }
-
- private final void writeCode(MyBitOutputStream bos, int code)
- throws IOException
- {
- bos.writeBits(code, codeSize);
- }
-
- private final boolean isInTable(byte bytes[], int start, int length)
- {
- Object key = arrayToKey(bytes, start, length);
-
- return map.containsKey(key);
- }
-
- private final int codeFromString(byte bytes[], int start, int length)
- throws IOException
- {
- Object key = arrayToKey(bytes, start, length);
- Object o = map.get(key);
- if (o == null)
- throw new IOException("CodeFromString");
- return ((Integer) o).intValue();
- }
-
- private final boolean addTableEntry(MyBitOutputStream bos, byte bytes[],
- int start, int length) throws IOException
- {
- Object key = arrayToKey(bytes, start, length);
- return addTableEntry(bos, key);
- }
-
- private final boolean addTableEntry(MyBitOutputStream bos, Object key)
- throws IOException
- {
- boolean cleared = false;
-
- {
- int limit = (1 << codeSize);
- if (earlyLimit)
- limit--;
-
- if (codes == limit)
- {
- if (codeSize < 12)
- incrementCodeSize();
- else
- {
- writeClearCode(bos);
- clearTable();
- cleared = true;
- }
- }
- }
-
- if (!cleared)
- {
- map.put(key, new Integer(codes));
- codes++;
- }
-
- return cleared;
- }
-
- public static interface Listener
- {
- public void dataCode(int code);
-
- public void eoiCode(int code);
-
- public void clearCode(int code);
-
- public void init(int clearCode, int eoiCode);
- }
-
- public byte[] compress(byte bytes[]) throws IOException
- {
- ByteArrayOutputStream baos = new ByteArrayOutputStream(bytes.length);
- MyBitOutputStream bos = new MyBitOutputStream(baos, byteOrder);
-
- InitializeStringTable();
- clearTable();
- writeClearCode(bos);
- boolean cleared = false;
-
- int w_start = 0;
- int w_length = 0;
-
- for (int i = 0; i < bytes.length; i++)
- {
- if (isInTable(bytes, w_start, w_length + 1))
- {
- w_length++;
-
- cleared = false;
- } else
- {
- int code = codeFromString(bytes, w_start, w_length);
- writeDataCode(bos, code);
- cleared = addTableEntry(bos, bytes, w_start, w_length + 1);
-
- w_start = i;
- w_length = 1;
- }
- } /* end of for loop */
-
- int code = codeFromString(bytes, w_start, w_length);
- writeDataCode(bos, code);
+ private int codeSize;
+ private final int initialCodeSize;
+ private int codes = -1;
+
+ private final int byteOrder;
+ private final boolean earlyLimit;
+ private final int clearCode;
+ private final int eoiCode;
+ private final Listener listener;
+
+ public MyLZWCompressor(int initialCodeSize, int byteOrder,
+ boolean earlyLimit)
+ {
+ this(initialCodeSize, byteOrder, earlyLimit, null);
+ }
+
+ public MyLZWCompressor(int initialCodeSize, int byteOrder,
+ boolean earlyLimit, Listener listener)
+ {
+ this.listener = listener;
+ this.byteOrder = byteOrder;
+ this.earlyLimit = earlyLimit;
+
+ this.initialCodeSize = initialCodeSize;
+
+ clearCode = 1 << initialCodeSize;
+ eoiCode = clearCode + 1;
+
+ if (null != listener)
+ listener.init(clearCode, eoiCode);
+
+ InitializeStringTable();
+ }
+
+ private final Map map = new HashMap();
+
+ private final void InitializeStringTable()
+ {
+ codeSize = initialCodeSize;
+
+ int intial_entries_count = (1 << codeSize) + 2;
+
+ map.clear();
+ for (codes = 0; codes < intial_entries_count; codes++)
+ {
+ if ((codes != clearCode) && (codes != eoiCode))
+ {
+ Object key = arrayToKey((byte) codes);
+
+ map.put(key, new Integer(codes));
+ }
+ }
+ }
+
+ private final void clearTable()
+ {
+ InitializeStringTable();
+ incrementCodeSize();
+ }
+
+ private final void incrementCodeSize()
+ {
+ if (codeSize != 12)
+ codeSize++;
+ }
+
+ private final Object arrayToKey(byte b)
+ {
+ return arrayToKey(new byte[] { b, }, 0, 1);
+ }
+
+ private final static class ByteArray
+ {
+ private final byte bytes[];
+ private final int start;
+ private final int length;
+ private final int hash;
+
+ public ByteArray(byte bytes[])
+ {
+ this(bytes, 0, bytes.length);
+ }
+
+ public ByteArray(byte bytes[], int start, int length)
+ {
+ this.bytes = bytes;
+ this.start = start;
+ this.length = length;
+
+ int tempHash = length;
+
+ for (int i = 0; i < length; i++)
+ {
+ int b = 0xff & bytes[i + start];
+ tempHash = tempHash + (tempHash << 8) ^ b ^ i;
+ }
+
+ hash = tempHash;
+ }
+
+ public final int hashCode()
+ {
+ return hash;
+ }
+
+ public final boolean equals(Object o)
+ {
+ ByteArray other = (ByteArray) o;
+ if (other.hash != hash)
+ return false;
+ if (other.length != length)
+ return false;
+
+ for (int i = 0; i < length; i++)
+ {
+ if (other.bytes[i + other.start] != bytes[i + start])
+ return false;
+ }
+
+ return true;
+ }
+ }
+
+ private final Object arrayToKey(byte bytes[], int start, int length)
+ {
+ return new ByteArray(bytes, start, length);
+ }
+
+ private final void writeDataCode(MyBitOutputStream bos, int code)
+ throws IOException
+ {
+ if (null != listener)
+ listener.dataCode(code);
+ writeCode(bos, code);
+ }
+
+
+ private final void writeClearCode(MyBitOutputStream bos) throws IOException
+ {
+ if (null != listener)
+ listener.dataCode(clearCode);
+ writeCode(bos, clearCode);
+ }
+
+ private final void writeEoiCode(MyBitOutputStream bos) throws IOException
+ {
+ if (null != listener)
+ listener.eoiCode(eoiCode);
+ writeCode(bos, eoiCode);
+ }
+
+ private final void writeCode(MyBitOutputStream bos, int code)
+ throws IOException
+ {
+ bos.writeBits(code, codeSize);
+ }
+
+ private final boolean isInTable(byte bytes[], int start, int length)
+ {
+ Object key = arrayToKey(bytes, start, length);
+
+ return map.containsKey(key);
+ }
+
+ private final int codeFromString(byte bytes[], int start, int length)
+ throws IOException
+ {
+ Object key = arrayToKey(bytes, start, length);
+ Object o = map.get(key);
+ if (o == null)
+ throw new IOException("CodeFromString");
+ return ((Integer) o).intValue();
+ }
+
+ private final boolean addTableEntry(MyBitOutputStream bos, byte bytes[],
+ int start, int length) throws IOException
+ {
+ Object key = arrayToKey(bytes, start, length);
+ return addTableEntry(bos, key);
+ }
+
+ private final boolean addTableEntry(MyBitOutputStream bos, Object key)
+ throws IOException
+ {
+ boolean cleared = false;
+
+ {
+ int limit = (1 << codeSize);
+ if (earlyLimit)
+ limit--;
+
+ if (codes == limit)
+ {
+ if (codeSize < 12)
+ incrementCodeSize();
+ else
+ {
+ writeClearCode(bos);
+ clearTable();
+ cleared = true;
+ }
+ }
+ }
+
+ if (!cleared)
+ {
+ map.put(key, new Integer(codes));
+ codes++;
+ }
+
+ return cleared;
+ }
+
+ public static interface Listener
+ {
+ public void dataCode(int code);
+
+ public void eoiCode(int code);
+
+ public void clearCode(int code);
+
+ public void init(int clearCode, int eoiCode);
+ }
+
+ public byte[] compress(byte bytes[]) throws IOException
+ {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream(bytes.length);
+ MyBitOutputStream bos = new MyBitOutputStream(baos, byteOrder);
+
+ InitializeStringTable();
+ clearTable();
+ writeClearCode(bos);
+ boolean cleared = false;
+
+ int w_start = 0;
+ int w_length = 0;
+
+ for (int i = 0; i < bytes.length; i++)
+ {
+ if (isInTable(bytes, w_start, w_length + 1))
+ {
+ w_length++;
+
+ cleared = false;
+ } else
+ {
+ int code = codeFromString(bytes, w_start, w_length);
+ writeDataCode(bos, code);
+ cleared = addTableEntry(bos, bytes, w_start, w_length + 1);
+
+ w_start = i;
+ w_length = 1;
+ }
+ } /* end of for loop */
+
+ int code = codeFromString(bytes, w_start, w_length);
+ writeDataCode(bos, code);
- writeEoiCode(bos);
+ writeEoiCode(bos);
- bos.flushCache();
+ bos.flushCache();
- return baos.toByteArray();
- }
+ return baos.toByteArray();
+ }
}
\ No newline at end of file
Modified: commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/mylzw/MyLZWDecompressor.java
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/mylzw/MyLZWDecompressor.java?rev=995859&r1=995858&r2=995859&view=diff
==============================================================================
--- commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/mylzw/MyLZWDecompressor.java (original)
+++ commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/common/mylzw/MyLZWDecompressor.java Fri Sep 10 16:33:35 2010
@@ -23,204 +23,204 @@ import java.io.OutputStream;
public final class MyLZWDecompressor
{
- private static final int MAX_TABLE_SIZE = 1 << 12;
+ private static final int MAX_TABLE_SIZE = 1 << 12;
- private final byte[][] table;
- private int codeSize;
- private final int initialCodeSize;
- private int codes = -1;
-
- private final int byteOrder;
-
- private final Listener listener;
-
- public static interface Listener
- {
- public void code(int code);
-
- public void init(int clearCode, int eoiCode);
- }
-
- public MyLZWDecompressor(int initialCodeSize, int byteOrder)
- {
- this(initialCodeSize, byteOrder, null);
- }
-
- public MyLZWDecompressor(int initialCodeSize, int byteOrder,
- Listener listener)
- {
- this.listener = listener;
- this.byteOrder = byteOrder;
-
- this.initialCodeSize = initialCodeSize;
-
- table = new byte[MAX_TABLE_SIZE][];
- clearCode = 1 << initialCodeSize;
- eoiCode = clearCode + 1;
-
- if (null != listener)
- listener.init(clearCode, eoiCode);
-
- InitializeTable();
- }
-
- private final void InitializeTable()
- {
- codeSize = initialCodeSize;
-
- int intial_entries_count = 1 << codeSize + 2;
-
- for (int i = 0; i < intial_entries_count; i++)
- table[i] = new byte[] { (byte) i, };
- }
-
- private final void clearTable()
- {
- codes = (1 << initialCodeSize) + 2;
- codeSize = initialCodeSize;
- incrementCodeSize();
- }
-
- private final int clearCode;
- private final int eoiCode;
-
- private final int getNextCode(MyBitInputStream is) throws IOException
- {
- int code = is.readBits(codeSize);
-
- if (null != listener)
- listener.code(code);
- return code;
- }
-
- private final byte[] stringFromCode(int code) throws IOException
- {
- if ((code >= codes) || (code < 0))
- throw new IOException("Bad Code: " + code + " codes: " + codes
- + " code_size: " + codeSize + ", table: " + table.length);
-
- return table[code];
- }
-
- private final boolean isInTable(int Code)
- {
- return Code < codes;
- }
-
- private final byte firstChar(byte bytes[])
- {
- return bytes[0];
- }
-
- private final void addStringToTable(byte bytes[]) throws IOException
- {
- if (codes < (1 << codeSize))
- {
- table[codes] = bytes;
- codes++;
- } else
- throw new IOException("AddStringToTable: codes: " + codes
- + " code_size: " + codeSize);
-
- checkCodeSize();
- }
-
- private final byte[] appendBytes(byte bytes[], byte b)
- {
- byte result[] = new byte[bytes.length + 1];
-
- System.arraycopy(bytes, 0, result, 0, bytes.length);
- result[result.length - 1] = b;
- return result;
- }
-
- private int written = 0;
-
- private final void writeToResult(OutputStream os, byte bytes[])
- throws IOException
- {
- os.write(bytes);
- written += bytes.length;
- }
-
- private boolean tiffLZWMode = false;
-
- public void setTiffLZWMode()
- {
- tiffLZWMode = true;
- }
-
- public byte[] decompress(InputStream is, int expectedLength)
- throws IOException
- {
- int code, oldCode = -1;
- MyBitInputStream mbis = new MyBitInputStream(is, byteOrder);
- if (tiffLZWMode)
- mbis.setTiffLZWMode();
-
- ByteArrayOutputStream baos = new ByteArrayOutputStream(expectedLength);
-
- clearTable();
-
- while ((code = getNextCode(mbis)) != eoiCode)
- {
- if (code == clearCode)
- {
- clearTable();
-
- if (written >= expectedLength)
- break;
- code = getNextCode(mbis);
-
- if (code == eoiCode)
- {
- break;
- }
- writeToResult(baos, stringFromCode(code));
-
- oldCode = code;
- } // end of ClearCode case
- else
- {
- if (isInTable(code))
- {
- writeToResult(baos, stringFromCode(code));
-
- addStringToTable(appendBytes(stringFromCode(oldCode),
- firstChar(stringFromCode(code))));
- oldCode = code;
- } else
- {
- byte OutString[] = appendBytes(stringFromCode(oldCode),
- firstChar(stringFromCode(oldCode)));
- writeToResult(baos, OutString);
- addStringToTable(OutString);
- oldCode = code;
- }
- } // end of not-ClearCode case
-
- if (written >= expectedLength)
- break;
- } // end of while loop
-
- byte result[] = baos.toByteArray();
-
- return result;
- }
-
- private final void checkCodeSize() // throws IOException
- {
- int limit = (1 << codeSize);
- if (tiffLZWMode)
- limit--;
-
- if (codes == limit)
- incrementCodeSize();
- }
-
- private final void incrementCodeSize() // throws IOException
- {
- if (codeSize != 12)
- codeSize++;
- }
+ private final byte[][] table;
+ private int codeSize;
+ private final int initialCodeSize;
+ private int codes = -1;
+
+ private final int byteOrder;
+
+ private final Listener listener;
+
+ public static interface Listener
+ {
+ public void code(int code);
+
+ public void init(int clearCode, int eoiCode);
+ }
+
+ public MyLZWDecompressor(int initialCodeSize, int byteOrder)
+ {
+ this(initialCodeSize, byteOrder, null);
+ }
+
+ public MyLZWDecompressor(int initialCodeSize, int byteOrder,
+ Listener listener)
+ {
+ this.listener = listener;
+ this.byteOrder = byteOrder;
+
+ this.initialCodeSize = initialCodeSize;
+
+ table = new byte[MAX_TABLE_SIZE][];
+ clearCode = 1 << initialCodeSize;
+ eoiCode = clearCode + 1;
+
+ if (null != listener)
+ listener.init(clearCode, eoiCode);
+
+ InitializeTable();
+ }
+
+ private final void InitializeTable()
+ {
+ codeSize = initialCodeSize;
+
+ int intial_entries_count = 1 << codeSize + 2;
+
+ for (int i = 0; i < intial_entries_count; i++)
+ table[i] = new byte[] { (byte) i, };
+ }
+
+ private final void clearTable()
+ {
+ codes = (1 << initialCodeSize) + 2;
+ codeSize = initialCodeSize;
+ incrementCodeSize();
+ }
+
+ private final int clearCode;
+ private final int eoiCode;
+
+ private final int getNextCode(MyBitInputStream is) throws IOException
+ {
+ int code = is.readBits(codeSize);
+
+ if (null != listener)
+ listener.code(code);
+ return code;
+ }
+
+ private final byte[] stringFromCode(int code) throws IOException
+ {
+ if ((code >= codes) || (code < 0))
+ throw new IOException("Bad Code: " + code + " codes: " + codes
+ + " code_size: " + codeSize + ", table: " + table.length);
+
+ return table[code];
+ }
+
+ private final boolean isInTable(int Code)
+ {
+ return Code < codes;
+ }
+
+ private final byte firstChar(byte bytes[])
+ {
+ return bytes[0];
+ }
+
+ private final void addStringToTable(byte bytes[]) throws IOException
+ {
+ if (codes < (1 << codeSize))
+ {
+ table[codes] = bytes;
+ codes++;
+ } else
+ throw new IOException("AddStringToTable: codes: " + codes
+ + " code_size: " + codeSize);
+
+ checkCodeSize();
+ }
+
+ private final byte[] appendBytes(byte bytes[], byte b)
+ {
+ byte result[] = new byte[bytes.length + 1];
+
+ System.arraycopy(bytes, 0, result, 0, bytes.length);
+ result[result.length - 1] = b;
+ return result;
+ }
+
+ private int written = 0;
+
+ private final void writeToResult(OutputStream os, byte bytes[])
+ throws IOException
+ {
+ os.write(bytes);
+ written += bytes.length;
+ }
+
+ private boolean tiffLZWMode = false;
+
+ public void setTiffLZWMode()
+ {
+ tiffLZWMode = true;
+ }
+
+ public byte[] decompress(InputStream is, int expectedLength)
+ throws IOException
+ {
+ int code, oldCode = -1;
+ MyBitInputStream mbis = new MyBitInputStream(is, byteOrder);
+ if (tiffLZWMode)
+ mbis.setTiffLZWMode();
+
+ ByteArrayOutputStream baos = new ByteArrayOutputStream(expectedLength);
+
+ clearTable();
+
+ while ((code = getNextCode(mbis)) != eoiCode)
+ {
+ if (code == clearCode)
+ {
+ clearTable();
+
+ if (written >= expectedLength)
+ break;
+ code = getNextCode(mbis);
+
+ if (code == eoiCode)
+ {
+ break;
+ }
+ writeToResult(baos, stringFromCode(code));
+
+ oldCode = code;
+ } // end of ClearCode case
+ else
+ {
+ if (isInTable(code))
+ {
+ writeToResult(baos, stringFromCode(code));
+
+ addStringToTable(appendBytes(stringFromCode(oldCode),
+ firstChar(stringFromCode(code))));
+ oldCode = code;
+ } else
+ {
+ byte OutString[] = appendBytes(stringFromCode(oldCode),
+ firstChar(stringFromCode(oldCode)));
+ writeToResult(baos, OutString);
+ addStringToTable(OutString);
+ oldCode = code;
+ }
+ } // end of not-ClearCode case
+
+ if (written >= expectedLength)
+ break;
+ } // end of while loop
+
+ byte result[] = baos.toByteArray();
+
+ return result;
+ }
+
+ private final void checkCodeSize() // throws IOException
+ {
+ int limit = (1 << codeSize);
+ if (tiffLZWMode)
+ limit--;
+
+ if (codes == limit)
+ incrementCodeSize();
+ }
+
+ private final void incrementCodeSize() // throws IOException
+ {
+ if (codeSize != 12)
+ codeSize++;
+ }
}
\ No newline at end of file
Modified: commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/bmp/BmpHeaderInfo.java
URL: http://svn.apache.org/viewvc/commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/bmp/BmpHeaderInfo.java?rev=995859&r1=995858&r2=995859&view=diff
==============================================================================
--- commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/bmp/BmpHeaderInfo.java (original)
+++ commons/proper/sanselan/trunk/src/main/java/org/apache/sanselan/formats/bmp/BmpHeaderInfo.java Fri Sep 10 16:33:35 2010
@@ -18,55 +18,55 @@ package org.apache.sanselan.formats.bmp;
public class BmpHeaderInfo
{
- // BM - Windows 3.1x, 95, NT,
- // BA - OS/2 Bitmap Array
- // CI - OS/2 Color Icon
- // CP - OS/2 Color Pointer
- // IC - OS/2 Icon
- // PT - OS/2 Pointer
- public final byte identifier1;
- public final byte identifier2;
-
- public final int fileSize;
- public final int reserved;
- public final int bitmapDataOffset;
-
- public final int bitmapHeaderSize;
- public final int width;
- public final int height;
- public final int planes;
- public final int bitsPerPixel;
- public final int compression;
- public final int bitmapDataSize;
- public final int hResolution;
- public final int vResolution;
- public final int colorsUsed;
- public final int colorsImportant;
-
- public BmpHeaderInfo(byte identifier1, byte identifier2, int fileSize,
- int reserved, int bitmapDataOffset,
- int bitmapHeaderSize, int width, int height, int planes,
- int bitsPerPixel, int compression, int bitmapDataSize,
- int hResolution, int vResolution, int colorsUsed,
- int colorsImportant)
- {
- this.identifier1 = identifier1;
- this.identifier2 = identifier2;
- this.fileSize = fileSize;
- this.reserved = reserved;
- this.bitmapDataOffset = bitmapDataOffset;
-
- this.bitmapHeaderSize = bitmapHeaderSize;
- this.width = width;
- this.height = height;
- this.planes = planes;
- this.bitsPerPixel = bitsPerPixel;
- this.compression = compression;
- this.bitmapDataSize = bitmapDataSize;
- this.hResolution = hResolution;
- this.vResolution = vResolution;
- this.colorsUsed = colorsUsed;
- this.colorsImportant = colorsImportant;
- }
+ // BM - Windows 3.1x, 95, NT,
+ // BA - OS/2 Bitmap Array
+ // CI - OS/2 Color Icon
+ // CP - OS/2 Color Pointer
+ // IC - OS/2 Icon
+ // PT - OS/2 Pointer
+ public final byte identifier1;
+ public final byte identifier2;
+
+ public final int fileSize;
+ public final int reserved;
+ public final int bitmapDataOffset;
+
+ public final int bitmapHeaderSize;
+ public final int width;
+ public final int height;
+ public final int planes;
+ public final int bitsPerPixel;
+ public final int compression;
+ public final int bitmapDataSize;
+ public final int hResolution;
+ public final int vResolution;
+ public final int colorsUsed;
+ public final int colorsImportant;
+
+ public BmpHeaderInfo(byte identifier1, byte identifier2, int fileSize,
+ int reserved, int bitmapDataOffset,
+ int bitmapHeaderSize, int width, int height, int planes,
+ int bitsPerPixel, int compression, int bitmapDataSize,
+ int hResolution, int vResolution, int colorsUsed,
+ int colorsImportant)
+ {
+ this.identifier1 = identifier1;
+ this.identifier2 = identifier2;
+ this.fileSize = fileSize;
+ this.reserved = reserved;
+ this.bitmapDataOffset = bitmapDataOffset;
+
+ this.bitmapHeaderSize = bitmapHeaderSize;
+ this.width = width;
+ this.height = height;
+ this.planes = planes;
+ this.bitsPerPixel = bitsPerPixel;
+ this.compression = compression;
+ this.bitmapDataSize = bitmapDataSize;
+ this.hResolution = hResolution;
+ this.vResolution = vResolution;
+ this.colorsUsed = colorsUsed;
+ this.colorsImportant = colorsImportant;
+ }
}
\ No newline at end of file