-/*******************************************************************************\r
- * Copyright (c) 2007, 2012 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.compressions.impl;\r
-\r
-import java.io.File;\r
-import java.io.FileInputStream;\r
-import java.io.FileNotFoundException;\r
-import java.io.IOException;\r
-import java.io.InputStream;\r
-import java.nio.ByteBuffer;\r
-import java.nio.ByteOrder;\r
-import java.nio.IntBuffer;\r
-import java.nio.channels.ReadableByteChannel;\r
-\r
-/**\r
- * @author Hannu Niemistö\r
- * @author Tuukka Lehtonen\r
- */\r
-public abstract class DecompressingInputStream extends InputStream {\r
-\r
- protected InputStream stream;\r
- protected ReadableByteChannel channel;\r
- protected ByteBuffer header;\r
- protected IntBuffer intHeader;\r
- protected ByteBuffer compressed;\r
- protected ByteBuffer uncompressed;\r
- protected byte[] compressedArray;\r
-\r
- public DecompressingInputStream(File file) throws FileNotFoundException {\r
- this(new FileInputStream(file));\r
- }\r
-\r
- public DecompressingInputStream(FileInputStream stream) {\r
- this(stream, stream.getChannel());\r
- }\r
-\r
- public DecompressingInputStream(InputStream stream) {\r
- this(stream, null);\r
- }\r
-\r
- public DecompressingInputStream(InputStream stream, ReadableByteChannel channel) {\r
- this.stream = stream;\r
- this.channel = channel;\r
- header = ByteBuffer.allocate(8);\r
- header.order(ByteOrder.LITTLE_ENDIAN);\r
- intHeader = header.asIntBuffer();\r
- }\r
-\r
- @Override\r
- public int read() throws IOException {\r
- if(uncompressed == null || uncompressed.remaining() == 0)\r
- if(!fillBuffer())\r
- return -1;\r
- return uncompressed.get();\r
- }\r
-\r
- @Override\r
- public int read(byte[] b, int off, int len) throws IOException {\r
- int bytes = 0;\r
- while(len > 0) {\r
- if(uncompressed == null || uncompressed.remaining() == 0) {\r
- if(!fillBuffer()) {\r
- return bytes == 0 ? -1 : bytes;\r
- }\r
- }\r
- int s = Math.min(len, uncompressed.remaining());\r
- uncompressed.get(b, off, s);\r
- off += s;\r
- len -= s;\r
- bytes += s;\r
- }\r
- return bytes;\r
- }\r
-\r
- @Override\r
- public void close() throws IOException {\r
- if (channel != null) {\r
- channel.close();\r
- channel = null;\r
- }\r
- if (stream != null) {\r
- stream.close();\r
- stream = null;\r
- }\r
- }\r
-\r
- private boolean fillBuffer() throws IOException { \r
- header.position(0);\r
- if (channel != null) {\r
- if (Channels.read(channel, header) < 8)\r
- return false;\r
- } else {\r
- if (Channels.readStream(stream, header.array(), 8) < 8)\r
- return false;\r
- }\r
-\r
- intHeader.position(0);\r
- int compressedSize = intHeader.get();\r
- int uncompressedSize = intHeader.get();\r
-\r
- if(uncompressedSize == 0)\r
- return false;\r
-\r
- uncompressed = ensureBufferSize(uncompressed, uncompressedSize);\r
- compressed = ensureBufferSize(compressed, compressedSize);\r
-\r
- compressed.position(0);\r
- compressed.limit(compressedSize);\r
- if (channel != null) {\r
- if (Channels.read(channel, compressed) < compressedSize)\r
- return false;\r
- } else {\r
- if (compressedArray == null || compressedArray.length < compressedSize) \r
- compressedArray = new byte[compressedSize];\r
- if (Channels.readStream(stream, compressedArray, compressedSize) < 8)\r
- return false;\r
- compressed.put(compressedArray, 0, compressedSize);\r
- }\r
-\r
- decompress(compressed, 0, compressedSize, uncompressed, 0, uncompressedSize);\r
-\r
- uncompressed.position(0);\r
- uncompressed.limit(uncompressedSize);\r
-\r
- return true;\r
- }\r
-\r
- private ByteBuffer ensureBufferSize(ByteBuffer buffer, int minCapacity) {\r
- int oldCapacity = buffer != null ? buffer.capacity() : 0;\r
- if (buffer == null || oldCapacity < minCapacity) {\r
- int newCapacity = grow(oldCapacity, minCapacity);\r
- //System.out.println("ensureBufferSize(" + oldCapacity + ", " + minCapacity + "), new capacity " + newCapacity);\r
- buffer = allocateBuffer(newCapacity);\r
- }\r
- return buffer;\r
- }\r
-\r
- protected ByteBuffer allocateBuffer(int capacity) {\r
- return ByteBuffer.allocateDirect(capacity);\r
- }\r
- \r
- /**\r
- * @param oldCapacity current capacity of a buffer\r
- * @param minCapacity\r
- * @return new capacity\r
- */\r
- private static int grow(int oldCapacity, int minCapacity) {\r
- // overflow-conscious code\r
- int newCapacity = oldCapacity + (oldCapacity >> 1);\r
- if (newCapacity - minCapacity < 0)\r
- newCapacity = minCapacity;\r
- if (newCapacity < 0) // overflow\r
- throw new OutOfMemoryError();\r
- return newCapacity;\r
- }\r
-\r
- public abstract void decompress(ByteBuffer compressed, int compressedOffset, int compressedSize,\r
- ByteBuffer uncompressed, int uncompressedOffset, int uncompressedSize) throws IOException;\r
-\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2012 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.compressions.impl;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.IntBuffer;
+import java.nio.channels.ReadableByteChannel;
+
+/**
+ * @author Hannu Niemistö
+ * @author Tuukka Lehtonen
+ */
+public abstract class DecompressingInputStream extends InputStream {
+
+ protected InputStream stream;
+ protected ReadableByteChannel channel;
+ protected ByteBuffer header;
+ protected IntBuffer intHeader;
+ protected ByteBuffer compressed;
+ protected ByteBuffer uncompressed;
+ protected byte[] compressedArray;
+
+ public DecompressingInputStream(File file) throws FileNotFoundException {
+ this(new FileInputStream(file));
+ }
+
+ public DecompressingInputStream(FileInputStream stream) {
+ this(stream, stream.getChannel());
+ }
+
+ public DecompressingInputStream(InputStream stream) {
+ this(stream, null);
+ }
+
+ public DecompressingInputStream(InputStream stream, ReadableByteChannel channel) {
+ this.stream = stream;
+ this.channel = channel;
+ header = ByteBuffer.allocate(8);
+ header.order(ByteOrder.LITTLE_ENDIAN);
+ intHeader = header.asIntBuffer();
+ }
+
+ @Override
+ public int read() throws IOException {
+ if(uncompressed == null || uncompressed.remaining() == 0)
+ if(!fillBuffer())
+ return -1;
+ return uncompressed.get();
+ }
+
+ @Override
+ public int read(byte[] b, int off, int len) throws IOException {
+ int bytes = 0;
+ while(len > 0) {
+ if(uncompressed == null || uncompressed.remaining() == 0) {
+ if(!fillBuffer()) {
+ return bytes == 0 ? -1 : bytes;
+ }
+ }
+ int s = Math.min(len, uncompressed.remaining());
+ uncompressed.get(b, off, s);
+ off += s;
+ len -= s;
+ bytes += s;
+ }
+ return bytes;
+ }
+
+ @Override
+ public void close() throws IOException {
+ if (channel != null) {
+ channel.close();
+ channel = null;
+ }
+ if (stream != null) {
+ stream.close();
+ stream = null;
+ }
+ }
+
+ private boolean fillBuffer() throws IOException {
+ header.position(0);
+ if (channel != null) {
+ if (Channels.read(channel, header) < 8)
+ return false;
+ } else {
+ if (Channels.readStream(stream, header.array(), 8) < 8)
+ return false;
+ }
+
+ intHeader.position(0);
+ int compressedSize = intHeader.get();
+ int uncompressedSize = intHeader.get();
+
+ if(uncompressedSize == 0)
+ return false;
+
+ uncompressed = ensureBufferSize(uncompressed, uncompressedSize);
+ compressed = ensureBufferSize(compressed, compressedSize);
+
+ compressed.position(0);
+ compressed.limit(compressedSize);
+ if (channel != null) {
+ if (Channels.read(channel, compressed) < compressedSize)
+ return false;
+ } else {
+ if (compressedArray == null || compressedArray.length < compressedSize)
+ compressedArray = new byte[compressedSize];
+ if (Channels.readStream(stream, compressedArray, compressedSize) < 8)
+ return false;
+ compressed.put(compressedArray, 0, compressedSize);
+ }
+
+ decompress(compressed, 0, compressedSize, uncompressed, 0, uncompressedSize);
+
+ uncompressed.position(0);
+ uncompressed.limit(uncompressedSize);
+
+ return true;
+ }
+
+ private ByteBuffer ensureBufferSize(ByteBuffer buffer, int minCapacity) {
+ int oldCapacity = buffer != null ? buffer.capacity() : 0;
+ if (buffer == null || oldCapacity < minCapacity) {
+ int newCapacity = grow(oldCapacity, minCapacity);
+ //System.out.println("ensureBufferSize(" + oldCapacity + ", " + minCapacity + "), new capacity " + newCapacity);
+ buffer = allocateBuffer(newCapacity);
+ }
+ return buffer;
+ }
+
+ protected ByteBuffer allocateBuffer(int capacity) {
+ return ByteBuffer.allocateDirect(capacity);
+ }
+
+ /**
+ * @param oldCapacity current capacity of a buffer
+ * @param minCapacity
+ * @return new capacity
+ */
+ private static int grow(int oldCapacity, int minCapacity) {
+ // overflow-conscious code
+ int newCapacity = oldCapacity + (oldCapacity >> 1);
+ if (newCapacity - minCapacity < 0)
+ newCapacity = minCapacity;
+ if (newCapacity < 0) // overflow
+ throw new OutOfMemoryError();
+ return newCapacity;
+ }
+
+ public abstract void decompress(ByteBuffer compressed, int compressedOffset, int compressedSize,
+ ByteBuffer uncompressed, int uncompressedOffset, int uncompressedSize) throws IOException;
+
}
\ No newline at end of file