--- /dev/null
+/*******************************************************************************\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 static 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 = ByteBuffer.allocateDirect(newCapacity);\r
+ }\r
+ return buffer;\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
+}
\ No newline at end of file