]> gerrit.simantics Code Review - simantics/platform.git/blob
8c7987177fb32145fe27d62a0c0a7585d998ab38
[simantics/platform.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2012 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.compressions.impl;\r
13 \r
14 import java.io.File;\r
15 import java.io.FileInputStream;\r
16 import java.io.FileNotFoundException;\r
17 import java.io.IOException;\r
18 import java.io.InputStream;\r
19 import java.nio.ByteBuffer;\r
20 import java.nio.ByteOrder;\r
21 import java.nio.IntBuffer;\r
22 import java.nio.channels.ReadableByteChannel;\r
23 \r
24 /**\r
25  * @author Hannu Niemistö\r
26  * @author Tuukka Lehtonen\r
27  */\r
28 public abstract class DecompressingInputStream extends InputStream {\r
29 \r
30     protected InputStream         stream;\r
31     protected ReadableByteChannel channel;\r
32     protected ByteBuffer          header;\r
33     protected IntBuffer           intHeader;\r
34     protected ByteBuffer          compressed;\r
35     protected ByteBuffer          uncompressed;\r
36     protected byte[]              compressedArray;\r
37 \r
38     public DecompressingInputStream(File file) throws FileNotFoundException {\r
39         this(new FileInputStream(file));\r
40     }\r
41 \r
42     public DecompressingInputStream(FileInputStream stream) {\r
43         this(stream, stream.getChannel());\r
44     }\r
45 \r
46     public DecompressingInputStream(InputStream stream) {\r
47         this(stream, null);\r
48     }\r
49 \r
50     public DecompressingInputStream(InputStream stream, ReadableByteChannel channel) {\r
51         this.stream = stream;\r
52         this.channel = channel;\r
53         header = ByteBuffer.allocate(8);\r
54         header.order(ByteOrder.LITTLE_ENDIAN);\r
55         intHeader = header.asIntBuffer();\r
56     }\r
57 \r
58     @Override\r
59     public int read() throws IOException {\r
60         if(uncompressed == null || uncompressed.remaining() == 0)\r
61             if(!fillBuffer())\r
62                 return -1;\r
63         return uncompressed.get();\r
64     }\r
65 \r
66     @Override\r
67     public int read(byte[] b, int off, int len) throws IOException {\r
68         int bytes = 0;\r
69         while(len > 0) {\r
70             if(uncompressed == null || uncompressed.remaining() == 0) {\r
71                 if(!fillBuffer()) {\r
72                     return bytes == 0 ? -1 : bytes;\r
73                 }\r
74             }\r
75             int s = Math.min(len, uncompressed.remaining());\r
76             uncompressed.get(b, off, s);\r
77             off += s;\r
78             len -= s;\r
79             bytes += s;\r
80         }\r
81         return bytes;\r
82     }\r
83 \r
84     @Override\r
85     public void close() throws IOException {\r
86         if (channel != null) {\r
87             channel.close();\r
88             channel = null;\r
89         }\r
90         if (stream != null) {\r
91             stream.close();\r
92             stream = null;\r
93         }\r
94     }\r
95 \r
96     private boolean fillBuffer() throws IOException {   \r
97         header.position(0);\r
98         if (channel != null) {\r
99             if (Channels.read(channel, header) < 8)\r
100                 return false;\r
101         } else {\r
102             if (Channels.readStream(stream, header.array(), 8) < 8)\r
103                 return false;\r
104         }\r
105 \r
106         intHeader.position(0);\r
107         int compressedSize = intHeader.get();\r
108         int uncompressedSize = intHeader.get();\r
109 \r
110         if(uncompressedSize == 0)\r
111             return false;\r
112 \r
113         uncompressed = ensureBufferSize(uncompressed, uncompressedSize);\r
114         compressed = ensureBufferSize(compressed, compressedSize);\r
115 \r
116         compressed.position(0);\r
117         compressed.limit(compressedSize);\r
118         if (channel != null) {\r
119             if (Channels.read(channel, compressed) < compressedSize)\r
120                 return false;\r
121         } else {\r
122             if (compressedArray == null || compressedArray.length < compressedSize) \r
123                 compressedArray = new byte[compressedSize];\r
124             if (Channels.readStream(stream, compressedArray, compressedSize) < 8)\r
125                 return false;\r
126             compressed.put(compressedArray, 0, compressedSize);\r
127         }\r
128 \r
129         decompress(compressed, 0, compressedSize, uncompressed, 0, uncompressedSize);\r
130 \r
131         uncompressed.position(0);\r
132         uncompressed.limit(uncompressedSize);\r
133 \r
134         return true;\r
135     }\r
136 \r
137     private static ByteBuffer ensureBufferSize(ByteBuffer buffer, int minCapacity) {\r
138         int oldCapacity = buffer != null ? buffer.capacity() : 0;\r
139         if (buffer == null || oldCapacity < minCapacity) {\r
140             int newCapacity = grow(oldCapacity, minCapacity);\r
141             //System.out.println("ensureBufferSize(" + oldCapacity + ", " + minCapacity + "), new capacity " + newCapacity);\r
142             buffer = ByteBuffer.allocateDirect(newCapacity);\r
143         }\r
144         return buffer;\r
145     }\r
146 \r
147     /**\r
148      * @param oldCapacity current capacity of a buffer\r
149      * @param minCapacity\r
150      * @return new capacity\r
151      */\r
152     private static int grow(int oldCapacity, int minCapacity) {\r
153         // overflow-conscious code\r
154         int newCapacity = oldCapacity + (oldCapacity >> 1);\r
155         if (newCapacity - minCapacity < 0)\r
156             newCapacity = minCapacity;\r
157         if (newCapacity < 0) // overflow\r
158             throw new OutOfMemoryError();\r
159         return newCapacity;\r
160     }\r
161 \r
162     public abstract void decompress(ByteBuffer compressed, int compressedOffset, int compressedSize,\r
163             ByteBuffer uncompressed, int uncompressedOffset, int uncompressedSize) throws IOException;\r
164 \r
165 }