commons-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject svn commit: r1006212 [3/16] - in /websites/production/commons/content/proper/commons-compress: ./ apidocs/org/apache/commons/compress/compressors/lz4/ apidocs/org/apache/commons/compress/compressors/snappy/ apidocs/src-html/org/apache/commons/compress/...
Date Sat, 04 Feb 2017 16:19:38 GMT
Modified: websites/production/commons/content/proper/commons-compress/apidocs/src-html/org/apache/commons/compress/compressors/lz4/FramedLZ4CompressorInputStream.html
==============================================================================
--- websites/production/commons/content/proper/commons-compress/apidocs/src-html/org/apache/commons/compress/compressors/lz4/FramedLZ4CompressorInputStream.html (original)
+++ websites/production/commons/content/proper/commons-compress/apidocs/src-html/org/apache/commons/compress/compressors/lz4/FramedLZ4CompressorInputStream.html Sat Feb  4 16:19:37 2017
@@ -28,263 +28,262 @@
 <span class="sourceLineNo">020</span><a name="line.20"></a>
 <span class="sourceLineNo">021</span>import java.io.IOException;<a name="line.21"></a>
 <span class="sourceLineNo">022</span>import java.io.InputStream;<a name="line.22"></a>
-<span class="sourceLineNo">023</span>import java.io.PushbackInputStream;<a name="line.23"></a>
-<span class="sourceLineNo">024</span>import java.util.Arrays;<a name="line.24"></a>
-<span class="sourceLineNo">025</span><a name="line.25"></a>
-<span class="sourceLineNo">026</span>import org.apache.commons.compress.compressors.CompressorInputStream;<a name="line.26"></a>
-<span class="sourceLineNo">027</span>import org.apache.commons.compress.utils.BoundedInputStream;<a name="line.27"></a>
-<span class="sourceLineNo">028</span>import org.apache.commons.compress.utils.ByteUtils;<a name="line.28"></a>
-<span class="sourceLineNo">029</span>import org.apache.commons.compress.utils.IOUtils;<a name="line.29"></a>
-<span class="sourceLineNo">030</span><a name="line.30"></a>
-<span class="sourceLineNo">031</span>/**<a name="line.31"></a>
-<span class="sourceLineNo">032</span> * CompressorInputStream for the LZ4 frame format.<a name="line.32"></a>
-<span class="sourceLineNo">033</span> *<a name="line.33"></a>
-<span class="sourceLineNo">034</span> * &lt;p&gt;Based on the "spec" in the version "1.5.1 (31/03/2015)"&lt;/p&gt;<a name="line.34"></a>
-<span class="sourceLineNo">035</span> *<a name="line.35"></a>
-<span class="sourceLineNo">036</span> * @see &lt;a href="http://lz4.github.io/lz4/lz4_Frame_format.html"&gt;LZ4 Frame Format Description&lt;/a&gt;<a name="line.36"></a>
-<span class="sourceLineNo">037</span> * @since 1.14<a name="line.37"></a>
-<span class="sourceLineNo">038</span> * @NotThreadSafe<a name="line.38"></a>
-<span class="sourceLineNo">039</span> */<a name="line.39"></a>
-<span class="sourceLineNo">040</span>public class FramedLZ4CompressorInputStream extends CompressorInputStream {<a name="line.40"></a>
-<span class="sourceLineNo">041</span>    /*<a name="line.41"></a>
-<span class="sourceLineNo">042</span>     * TODO before releasing 1.14:<a name="line.42"></a>
-<span class="sourceLineNo">043</span>     *<a name="line.43"></a>
-<span class="sourceLineNo">044</span>     * + xxhash32 checksum validation<a name="line.44"></a>
-<span class="sourceLineNo">045</span>     * + skippable frames<a name="line.45"></a>
-<span class="sourceLineNo">046</span>     * + decompressConcatenated<a name="line.46"></a>
-<span class="sourceLineNo">047</span>     * + block dependence<a name="line.47"></a>
-<span class="sourceLineNo">048</span>     */<a name="line.48"></a>
-<span class="sourceLineNo">049</span><a name="line.49"></a>
-<span class="sourceLineNo">050</span>    // used by FramedLZ4CompressorOutputStream as well<a name="line.50"></a>
-<span class="sourceLineNo">051</span>    static final byte[] LZ4_SIGNATURE = new byte[] { //NOSONAR<a name="line.51"></a>
-<span class="sourceLineNo">052</span>        4, 0x22, 0x4d, 0x18<a name="line.52"></a>
-<span class="sourceLineNo">053</span>    };<a name="line.53"></a>
-<span class="sourceLineNo">054</span><a name="line.54"></a>
-<span class="sourceLineNo">055</span>    static final int VERSION_MASK = 0xC0;<a name="line.55"></a>
-<span class="sourceLineNo">056</span>    static final int SUPPORTED_VERSION = 0x40;<a name="line.56"></a>
-<span class="sourceLineNo">057</span>    static final int BLOCK_INDEPENDENCE_MASK = 0x20;<a name="line.57"></a>
-<span class="sourceLineNo">058</span>    static final int BLOCK_CHECKSUM_MASK = 0x10;<a name="line.58"></a>
-<span class="sourceLineNo">059</span>    static final int CONTENT_SIZE_MASK = 0x08;<a name="line.59"></a>
-<span class="sourceLineNo">060</span>    static final int CONTENT_CHECKSUM_MASK = 0x04;<a name="line.60"></a>
-<span class="sourceLineNo">061</span>    static final int BLOCK_MAX_SIZE_MASK = 0x70;<a name="line.61"></a>
-<span class="sourceLineNo">062</span>    static final int UNCOMPRESSED_FLAG_MASK = 0x80000000;<a name="line.62"></a>
-<span class="sourceLineNo">063</span><a name="line.63"></a>
-<span class="sourceLineNo">064</span>    // used in no-arg read method<a name="line.64"></a>
-<span class="sourceLineNo">065</span>    private final byte[] oneByte = new byte[1];<a name="line.65"></a>
-<span class="sourceLineNo">066</span><a name="line.66"></a>
-<span class="sourceLineNo">067</span>    private final ByteUtils.ByteSupplier supplier = new ByteUtils.ByteSupplier() {<a name="line.67"></a>
-<span class="sourceLineNo">068</span>        @Override<a name="line.68"></a>
-<span class="sourceLineNo">069</span>        public int getAsByte() throws IOException {<a name="line.69"></a>
-<span class="sourceLineNo">070</span>            return readOneByte();<a name="line.70"></a>
-<span class="sourceLineNo">071</span>        }<a name="line.71"></a>
-<span class="sourceLineNo">072</span>    };<a name="line.72"></a>
-<span class="sourceLineNo">073</span><a name="line.73"></a>
-<span class="sourceLineNo">074</span>    private final InputStream in;<a name="line.74"></a>
-<span class="sourceLineNo">075</span><a name="line.75"></a>
-<span class="sourceLineNo">076</span>    private boolean expectBlockChecksum;<a name="line.76"></a>
-<span class="sourceLineNo">077</span>    private boolean expectContentSize;<a name="line.77"></a>
-<span class="sourceLineNo">078</span>    private boolean expectContentChecksum;<a name="line.78"></a>
-<span class="sourceLineNo">079</span><a name="line.79"></a>
-<span class="sourceLineNo">080</span>    private InputStream currentBlock;<a name="line.80"></a>
-<span class="sourceLineNo">081</span>    private boolean endReached, inUncompressed;<a name="line.81"></a>
-<span class="sourceLineNo">082</span><a name="line.82"></a>
-<span class="sourceLineNo">083</span>    // used for frame header checksum and content checksum, if present<a name="line.83"></a>
-<span class="sourceLineNo">084</span>    private final XXHash32 contentHash = new XXHash32();<a name="line.84"></a>
-<span class="sourceLineNo">085</span><a name="line.85"></a>
-<span class="sourceLineNo">086</span>    /**<a name="line.86"></a>
-<span class="sourceLineNo">087</span>     * Creates a new input stream that decompresses streams compressed<a name="line.87"></a>
-<span class="sourceLineNo">088</span>     * using the LZ4 frame format.<a name="line.88"></a>
-<span class="sourceLineNo">089</span>     * @param in  the InputStream from which to read the compressed data<a name="line.89"></a>
-<span class="sourceLineNo">090</span>     * @throws IOException if reading fails<a name="line.90"></a>
-<span class="sourceLineNo">091</span>     */<a name="line.91"></a>
-<span class="sourceLineNo">092</span>    public FramedLZ4CompressorInputStream(InputStream in) throws IOException {<a name="line.92"></a>
-<span class="sourceLineNo">093</span>        this.in = in;<a name="line.93"></a>
-<span class="sourceLineNo">094</span>        readSignature();<a name="line.94"></a>
-<span class="sourceLineNo">095</span>        readFrameDescriptor();<a name="line.95"></a>
-<span class="sourceLineNo">096</span>        nextBlock();<a name="line.96"></a>
-<span class="sourceLineNo">097</span>    }<a name="line.97"></a>
-<span class="sourceLineNo">098</span><a name="line.98"></a>
-<span class="sourceLineNo">099</span>    /** {@inheritDoc} */<a name="line.99"></a>
-<span class="sourceLineNo">100</span>    @Override<a name="line.100"></a>
-<span class="sourceLineNo">101</span>    public int read() throws IOException {<a name="line.101"></a>
-<span class="sourceLineNo">102</span>        return read(oneByte, 0, 1) == -1 ? -1 : oneByte[0] &amp; 0xFF;<a name="line.102"></a>
-<span class="sourceLineNo">103</span>    }<a name="line.103"></a>
-<span class="sourceLineNo">104</span><a name="line.104"></a>
-<span class="sourceLineNo">105</span>    /** {@inheritDoc} */<a name="line.105"></a>
-<span class="sourceLineNo">106</span>    @Override<a name="line.106"></a>
-<span class="sourceLineNo">107</span>    public void close() throws IOException {<a name="line.107"></a>
-<span class="sourceLineNo">108</span>        if (currentBlock != null) {<a name="line.108"></a>
-<span class="sourceLineNo">109</span>            currentBlock.close();<a name="line.109"></a>
-<span class="sourceLineNo">110</span>            currentBlock = null;<a name="line.110"></a>
-<span class="sourceLineNo">111</span>        }<a name="line.111"></a>
-<span class="sourceLineNo">112</span>        in.close();<a name="line.112"></a>
-<span class="sourceLineNo">113</span>    }<a name="line.113"></a>
-<span class="sourceLineNo">114</span><a name="line.114"></a>
-<span class="sourceLineNo">115</span>    /** {@inheritDoc} */<a name="line.115"></a>
-<span class="sourceLineNo">116</span>    @Override<a name="line.116"></a>
-<span class="sourceLineNo">117</span>    public int read(final byte[] b, final int off, final int len) throws IOException {<a name="line.117"></a>
-<span class="sourceLineNo">118</span>        if (endReached) {<a name="line.118"></a>
-<span class="sourceLineNo">119</span>            return -1;<a name="line.119"></a>
-<span class="sourceLineNo">120</span>        }<a name="line.120"></a>
-<span class="sourceLineNo">121</span>        int r = readOnce(b, off, len);<a name="line.121"></a>
-<span class="sourceLineNo">122</span>        if (r == -1) {<a name="line.122"></a>
-<span class="sourceLineNo">123</span>            nextBlock();<a name="line.123"></a>
-<span class="sourceLineNo">124</span>            if (!endReached) {<a name="line.124"></a>
-<span class="sourceLineNo">125</span>                r = readOnce(b, off, len);<a name="line.125"></a>
-<span class="sourceLineNo">126</span>            }<a name="line.126"></a>
-<span class="sourceLineNo">127</span>        }<a name="line.127"></a>
-<span class="sourceLineNo">128</span>        if (expectContentChecksum &amp;&amp; r != -1) {<a name="line.128"></a>
-<span class="sourceLineNo">129</span>            contentHash.update(b, off, r);<a name="line.129"></a>
-<span class="sourceLineNo">130</span>        }<a name="line.130"></a>
-<span class="sourceLineNo">131</span>        return r;<a name="line.131"></a>
-<span class="sourceLineNo">132</span>    }<a name="line.132"></a>
-<span class="sourceLineNo">133</span><a name="line.133"></a>
-<span class="sourceLineNo">134</span>    private void readSignature() throws IOException {<a name="line.134"></a>
-<span class="sourceLineNo">135</span>        final byte[] b = new byte[4];<a name="line.135"></a>
-<span class="sourceLineNo">136</span>        final int read = IOUtils.readFully(in, b);<a name="line.136"></a>
-<span class="sourceLineNo">137</span>        count(read);<a name="line.137"></a>
-<span class="sourceLineNo">138</span>        if (4 != read || !matches(b, 4)) {<a name="line.138"></a>
-<span class="sourceLineNo">139</span>            throw new IOException("Not a LZ4 frame stream");<a name="line.139"></a>
-<span class="sourceLineNo">140</span>        }<a name="line.140"></a>
-<span class="sourceLineNo">141</span>    }<a name="line.141"></a>
-<span class="sourceLineNo">142</span><a name="line.142"></a>
-<span class="sourceLineNo">143</span>    private void readFrameDescriptor() throws IOException {<a name="line.143"></a>
-<span class="sourceLineNo">144</span>        int flags = readOneByte();<a name="line.144"></a>
-<span class="sourceLineNo">145</span>        if (flags == -1) {<a name="line.145"></a>
-<span class="sourceLineNo">146</span>            throw new IOException("Premature end of stream while reading frame flags");<a name="line.146"></a>
-<span class="sourceLineNo">147</span>        }<a name="line.147"></a>
-<span class="sourceLineNo">148</span>        contentHash.update(flags);<a name="line.148"></a>
-<span class="sourceLineNo">149</span>        if ((flags &amp; VERSION_MASK) != SUPPORTED_VERSION) {<a name="line.149"></a>
-<span class="sourceLineNo">150</span>            throw new IOException("Unsupported version " + (flags &gt;&gt; 6));<a name="line.150"></a>
-<span class="sourceLineNo">151</span>        }<a name="line.151"></a>
-<span class="sourceLineNo">152</span>        if ((flags &amp; BLOCK_INDEPENDENCE_MASK) == 0) {<a name="line.152"></a>
-<span class="sourceLineNo">153</span>            throw new IOException("Block dependence is not supported");<a name="line.153"></a>
-<span class="sourceLineNo">154</span>        }<a name="line.154"></a>
-<span class="sourceLineNo">155</span>        expectBlockChecksum = (flags &amp; BLOCK_CHECKSUM_MASK) != 0;<a name="line.155"></a>
-<span class="sourceLineNo">156</span>        expectContentSize = (flags &amp; CONTENT_SIZE_MASK) != 0;<a name="line.156"></a>
-<span class="sourceLineNo">157</span>        expectContentChecksum = (flags &amp; CONTENT_CHECKSUM_MASK) != 0;<a name="line.157"></a>
-<span class="sourceLineNo">158</span>        int bdByte = readOneByte();<a name="line.158"></a>
-<span class="sourceLineNo">159</span>        if (bdByte == -1) { // max size is irrelevant for this implementation<a name="line.159"></a>
-<span class="sourceLineNo">160</span>            throw new IOException("Premature end of stream while reading frame BD byte");<a name="line.160"></a>
-<span class="sourceLineNo">161</span>        }<a name="line.161"></a>
-<span class="sourceLineNo">162</span>        contentHash.update(bdByte);<a name="line.162"></a>
-<span class="sourceLineNo">163</span>        if (expectContentSize) { // for now we don't care, contains the uncompressed size<a name="line.163"></a>
-<span class="sourceLineNo">164</span>            byte[] contentSize = new byte[8];<a name="line.164"></a>
-<span class="sourceLineNo">165</span>            int skipped = (int) IOUtils.readFully(in, contentSize);<a name="line.165"></a>
-<span class="sourceLineNo">166</span>            count(skipped);<a name="line.166"></a>
-<span class="sourceLineNo">167</span>            if (8 != skipped) {<a name="line.167"></a>
-<span class="sourceLineNo">168</span>                throw new IOException("Premature end of stream while reading content size");<a name="line.168"></a>
-<span class="sourceLineNo">169</span>            }<a name="line.169"></a>
-<span class="sourceLineNo">170</span>            contentHash.update(contentSize, 0, contentSize.length);<a name="line.170"></a>
-<span class="sourceLineNo">171</span>        }<a name="line.171"></a>
-<span class="sourceLineNo">172</span>        int headerHash = readOneByte();<a name="line.172"></a>
-<span class="sourceLineNo">173</span>        if (headerHash == -1) { // partial hash of header.<a name="line.173"></a>
-<span class="sourceLineNo">174</span>            throw new IOException("Premature end of stream while reading frame header checksum");<a name="line.174"></a>
-<span class="sourceLineNo">175</span>        }<a name="line.175"></a>
-<span class="sourceLineNo">176</span>        int expectedHash = (int) ((contentHash.getValue() &gt;&gt; 8) &amp; 0xff);<a name="line.176"></a>
-<span class="sourceLineNo">177</span>        contentHash.reset();<a name="line.177"></a>
-<span class="sourceLineNo">178</span>        if (headerHash != expectedHash) {<a name="line.178"></a>
-<span class="sourceLineNo">179</span>            throw new IOException("frame header checksum mismatch.");<a name="line.179"></a>
-<span class="sourceLineNo">180</span>        }<a name="line.180"></a>
-<span class="sourceLineNo">181</span>    }<a name="line.181"></a>
-<span class="sourceLineNo">182</span><a name="line.182"></a>
-<span class="sourceLineNo">183</span>    private void nextBlock() throws IOException {<a name="line.183"></a>
-<span class="sourceLineNo">184</span>        maybeFinishCurrentBlock();<a name="line.184"></a>
-<span class="sourceLineNo">185</span>        long len = ByteUtils.fromLittleEndian(supplier, 4);<a name="line.185"></a>
-<span class="sourceLineNo">186</span>        boolean uncompressed = (len &amp; UNCOMPRESSED_FLAG_MASK) != 0;<a name="line.186"></a>
-<span class="sourceLineNo">187</span>        int realLen = (int) (len &amp; (~UNCOMPRESSED_FLAG_MASK));<a name="line.187"></a>
-<span class="sourceLineNo">188</span>        if (realLen == 0) {<a name="line.188"></a>
-<span class="sourceLineNo">189</span>            endReached = true;<a name="line.189"></a>
-<span class="sourceLineNo">190</span>            verifyContentChecksum();<a name="line.190"></a>
-<span class="sourceLineNo">191</span>            return;<a name="line.191"></a>
-<span class="sourceLineNo">192</span>        }<a name="line.192"></a>
-<span class="sourceLineNo">193</span>        InputStream capped = new BoundedInputStream(in, realLen);<a name="line.193"></a>
-<span class="sourceLineNo">194</span>        if (uncompressed) {<a name="line.194"></a>
-<span class="sourceLineNo">195</span>            inUncompressed = true;<a name="line.195"></a>
-<span class="sourceLineNo">196</span>            currentBlock = capped;<a name="line.196"></a>
-<span class="sourceLineNo">197</span>        } else {<a name="line.197"></a>
-<span class="sourceLineNo">198</span>            inUncompressed = false;<a name="line.198"></a>
-<span class="sourceLineNo">199</span>            currentBlock = new BlockLZ4CompressorInputStream(capped);<a name="line.199"></a>
-<span class="sourceLineNo">200</span>        }<a name="line.200"></a>
-<span class="sourceLineNo">201</span>    }<a name="line.201"></a>
-<span class="sourceLineNo">202</span><a name="line.202"></a>
-<span class="sourceLineNo">203</span>    private void maybeFinishCurrentBlock() throws IOException {<a name="line.203"></a>
-<span class="sourceLineNo">204</span>        if (currentBlock != null) {<a name="line.204"></a>
-<span class="sourceLineNo">205</span>            currentBlock.close();<a name="line.205"></a>
-<span class="sourceLineNo">206</span>            currentBlock = null;<a name="line.206"></a>
-<span class="sourceLineNo">207</span>            if (expectBlockChecksum) {<a name="line.207"></a>
-<span class="sourceLineNo">208</span>                int skipped = (int) IOUtils.skip(in, 4);<a name="line.208"></a>
-<span class="sourceLineNo">209</span>                count(skipped);<a name="line.209"></a>
-<span class="sourceLineNo">210</span>                if (4 != skipped) {<a name="line.210"></a>
-<span class="sourceLineNo">211</span>                    throw new IOException("Premature end of stream while reading block checksum");<a name="line.211"></a>
-<span class="sourceLineNo">212</span>                }<a name="line.212"></a>
-<span class="sourceLineNo">213</span>            }<a name="line.213"></a>
-<span class="sourceLineNo">214</span>        }<a name="line.214"></a>
-<span class="sourceLineNo">215</span>    }<a name="line.215"></a>
-<span class="sourceLineNo">216</span><a name="line.216"></a>
-<span class="sourceLineNo">217</span>    private void verifyContentChecksum() throws IOException {<a name="line.217"></a>
-<span class="sourceLineNo">218</span>        if (expectContentChecksum) {<a name="line.218"></a>
-<span class="sourceLineNo">219</span>            byte[] checksum = new byte[4];<a name="line.219"></a>
-<span class="sourceLineNo">220</span>            int read = IOUtils.readFully(in, checksum);<a name="line.220"></a>
-<span class="sourceLineNo">221</span>            count(read);<a name="line.221"></a>
-<span class="sourceLineNo">222</span>            if (4 != read) {<a name="line.222"></a>
-<span class="sourceLineNo">223</span>                throw new IOException("Premature end of stream while reading content checksum");<a name="line.223"></a>
-<span class="sourceLineNo">224</span>            }<a name="line.224"></a>
-<span class="sourceLineNo">225</span>            long expectedHash = contentHash.getValue();<a name="line.225"></a>
-<span class="sourceLineNo">226</span>            if (expectedHash != ByteUtils.fromLittleEndian(checksum)) {<a name="line.226"></a>
-<span class="sourceLineNo">227</span>                throw new IOException("content checksum mismatch.");<a name="line.227"></a>
-<span class="sourceLineNo">228</span>            }<a name="line.228"></a>
-<span class="sourceLineNo">229</span>            contentHash.reset();<a name="line.229"></a>
-<span class="sourceLineNo">230</span>        }<a name="line.230"></a>
-<span class="sourceLineNo">231</span>    }<a name="line.231"></a>
-<span class="sourceLineNo">232</span><a name="line.232"></a>
-<span class="sourceLineNo">233</span>    private int readOneByte() throws IOException {<a name="line.233"></a>
-<span class="sourceLineNo">234</span>        final int b = in.read();<a name="line.234"></a>
-<span class="sourceLineNo">235</span>        if (b != -1) {<a name="line.235"></a>
-<span class="sourceLineNo">236</span>            count(1);<a name="line.236"></a>
-<span class="sourceLineNo">237</span>            return b &amp; 0xFF;<a name="line.237"></a>
-<span class="sourceLineNo">238</span>        }<a name="line.238"></a>
-<span class="sourceLineNo">239</span>        return -1;<a name="line.239"></a>
-<span class="sourceLineNo">240</span>    }<a name="line.240"></a>
-<span class="sourceLineNo">241</span><a name="line.241"></a>
-<span class="sourceLineNo">242</span>    private int readOnce(byte[] b, int off, int len) throws IOException {<a name="line.242"></a>
-<span class="sourceLineNo">243</span>        if (inUncompressed) {<a name="line.243"></a>
-<span class="sourceLineNo">244</span>            int cnt = currentBlock.read(b, off, len);<a name="line.244"></a>
-<span class="sourceLineNo">245</span>            count(cnt);<a name="line.245"></a>
-<span class="sourceLineNo">246</span>            return cnt;<a name="line.246"></a>
-<span class="sourceLineNo">247</span>        } else {<a name="line.247"></a>
-<span class="sourceLineNo">248</span>            BlockLZ4CompressorInputStream l = (BlockLZ4CompressorInputStream) currentBlock;<a name="line.248"></a>
-<span class="sourceLineNo">249</span>            long before = l.getBytesRead();<a name="line.249"></a>
-<span class="sourceLineNo">250</span>            int cnt = currentBlock.read(b, off, len);<a name="line.250"></a>
-<span class="sourceLineNo">251</span>            count(l.getBytesRead() - before);<a name="line.251"></a>
-<span class="sourceLineNo">252</span>            return cnt;<a name="line.252"></a>
-<span class="sourceLineNo">253</span>        }<a name="line.253"></a>
-<span class="sourceLineNo">254</span>    }<a name="line.254"></a>
-<span class="sourceLineNo">255</span><a name="line.255"></a>
-<span class="sourceLineNo">256</span>    /**<a name="line.256"></a>
-<span class="sourceLineNo">257</span>     * Checks if the signature matches what is expected for a .lz4 file.<a name="line.257"></a>
-<span class="sourceLineNo">258</span>     *<a name="line.258"></a>
-<span class="sourceLineNo">259</span>     * &lt;p&gt;.lz4 files start with a four byte signature.&lt;/p&gt;<a name="line.259"></a>
-<span class="sourceLineNo">260</span>     *<a name="line.260"></a>
-<span class="sourceLineNo">261</span>     * @param signature the bytes to check<a name="line.261"></a>
-<span class="sourceLineNo">262</span>     * @param length    the number of bytes to check<a name="line.262"></a>
-<span class="sourceLineNo">263</span>     * @return          true if this is a .sz stream, false otherwise<a name="line.263"></a>
-<span class="sourceLineNo">264</span>     */<a name="line.264"></a>
-<span class="sourceLineNo">265</span>    public static boolean matches(final byte[] signature, final int length) {<a name="line.265"></a>
-<span class="sourceLineNo">266</span><a name="line.266"></a>
-<span class="sourceLineNo">267</span>        if (length &lt; LZ4_SIGNATURE.length) {<a name="line.267"></a>
-<span class="sourceLineNo">268</span>            return false;<a name="line.268"></a>
-<span class="sourceLineNo">269</span>        }<a name="line.269"></a>
-<span class="sourceLineNo">270</span><a name="line.270"></a>
-<span class="sourceLineNo">271</span>        byte[] shortenedSig = signature;<a name="line.271"></a>
-<span class="sourceLineNo">272</span>        if (signature.length &gt; LZ4_SIGNATURE.length) {<a name="line.272"></a>
-<span class="sourceLineNo">273</span>            shortenedSig = new byte[LZ4_SIGNATURE.length];<a name="line.273"></a>
-<span class="sourceLineNo">274</span>            System.arraycopy(signature, 0, shortenedSig, 0, LZ4_SIGNATURE.length);<a name="line.274"></a>
-<span class="sourceLineNo">275</span>        }<a name="line.275"></a>
-<span class="sourceLineNo">276</span><a name="line.276"></a>
-<span class="sourceLineNo">277</span>        return Arrays.equals(shortenedSig, LZ4_SIGNATURE);<a name="line.277"></a>
-<span class="sourceLineNo">278</span>    }<a name="line.278"></a>
-<span class="sourceLineNo">279</span>}<a name="line.279"></a>
+<span class="sourceLineNo">023</span>import java.util.Arrays;<a name="line.23"></a>
+<span class="sourceLineNo">024</span><a name="line.24"></a>
+<span class="sourceLineNo">025</span>import org.apache.commons.compress.compressors.CompressorInputStream;<a name="line.25"></a>
+<span class="sourceLineNo">026</span>import org.apache.commons.compress.utils.BoundedInputStream;<a name="line.26"></a>
+<span class="sourceLineNo">027</span>import org.apache.commons.compress.utils.ByteUtils;<a name="line.27"></a>
+<span class="sourceLineNo">028</span>import org.apache.commons.compress.utils.IOUtils;<a name="line.28"></a>
+<span class="sourceLineNo">029</span><a name="line.29"></a>
+<span class="sourceLineNo">030</span>/**<a name="line.30"></a>
+<span class="sourceLineNo">031</span> * CompressorInputStream for the LZ4 frame format.<a name="line.31"></a>
+<span class="sourceLineNo">032</span> *<a name="line.32"></a>
+<span class="sourceLineNo">033</span> * &lt;p&gt;Based on the "spec" in the version "1.5.1 (31/03/2015)"&lt;/p&gt;<a name="line.33"></a>
+<span class="sourceLineNo">034</span> *<a name="line.34"></a>
+<span class="sourceLineNo">035</span> * @see &lt;a href="http://lz4.github.io/lz4/lz4_Frame_format.html"&gt;LZ4 Frame Format Description&lt;/a&gt;<a name="line.35"></a>
+<span class="sourceLineNo">036</span> * @since 1.14<a name="line.36"></a>
+<span class="sourceLineNo">037</span> * @NotThreadSafe<a name="line.37"></a>
+<span class="sourceLineNo">038</span> */<a name="line.38"></a>
+<span class="sourceLineNo">039</span>public class FramedLZ4CompressorInputStream extends CompressorInputStream {<a name="line.39"></a>
+<span class="sourceLineNo">040</span>    /*<a name="line.40"></a>
+<span class="sourceLineNo">041</span>     * TODO before releasing 1.14:<a name="line.41"></a>
+<span class="sourceLineNo">042</span>     *<a name="line.42"></a>
+<span class="sourceLineNo">043</span>     * + xxhash32 checksum validation<a name="line.43"></a>
+<span class="sourceLineNo">044</span>     * + skippable frames<a name="line.44"></a>
+<span class="sourceLineNo">045</span>     * + decompressConcatenated<a name="line.45"></a>
+<span class="sourceLineNo">046</span>     * + block dependence<a name="line.46"></a>
+<span class="sourceLineNo">047</span>     */<a name="line.47"></a>
+<span class="sourceLineNo">048</span><a name="line.48"></a>
+<span class="sourceLineNo">049</span>    // used by FramedLZ4CompressorOutputStream as well<a name="line.49"></a>
+<span class="sourceLineNo">050</span>    static final byte[] LZ4_SIGNATURE = new byte[] { //NOSONAR<a name="line.50"></a>
+<span class="sourceLineNo">051</span>        4, 0x22, 0x4d, 0x18<a name="line.51"></a>
+<span class="sourceLineNo">052</span>    };<a name="line.52"></a>
+<span class="sourceLineNo">053</span><a name="line.53"></a>
+<span class="sourceLineNo">054</span>    static final int VERSION_MASK = 0xC0;<a name="line.54"></a>
+<span class="sourceLineNo">055</span>    static final int SUPPORTED_VERSION = 0x40;<a name="line.55"></a>
+<span class="sourceLineNo">056</span>    static final int BLOCK_INDEPENDENCE_MASK = 0x20;<a name="line.56"></a>
+<span class="sourceLineNo">057</span>    static final int BLOCK_CHECKSUM_MASK = 0x10;<a name="line.57"></a>
+<span class="sourceLineNo">058</span>    static final int CONTENT_SIZE_MASK = 0x08;<a name="line.58"></a>
+<span class="sourceLineNo">059</span>    static final int CONTENT_CHECKSUM_MASK = 0x04;<a name="line.59"></a>
+<span class="sourceLineNo">060</span>    static final int BLOCK_MAX_SIZE_MASK = 0x70;<a name="line.60"></a>
+<span class="sourceLineNo">061</span>    static final int UNCOMPRESSED_FLAG_MASK = 0x80000000;<a name="line.61"></a>
+<span class="sourceLineNo">062</span><a name="line.62"></a>
+<span class="sourceLineNo">063</span>    // used in no-arg read method<a name="line.63"></a>
+<span class="sourceLineNo">064</span>    private final byte[] oneByte = new byte[1];<a name="line.64"></a>
+<span class="sourceLineNo">065</span><a name="line.65"></a>
+<span class="sourceLineNo">066</span>    private final ByteUtils.ByteSupplier supplier = new ByteUtils.ByteSupplier() {<a name="line.66"></a>
+<span class="sourceLineNo">067</span>        @Override<a name="line.67"></a>
+<span class="sourceLineNo">068</span>        public int getAsByte() throws IOException {<a name="line.68"></a>
+<span class="sourceLineNo">069</span>            return readOneByte();<a name="line.69"></a>
+<span class="sourceLineNo">070</span>        }<a name="line.70"></a>
+<span class="sourceLineNo">071</span>    };<a name="line.71"></a>
+<span class="sourceLineNo">072</span><a name="line.72"></a>
+<span class="sourceLineNo">073</span>    private final InputStream in;<a name="line.73"></a>
+<span class="sourceLineNo">074</span><a name="line.74"></a>
+<span class="sourceLineNo">075</span>    private boolean expectBlockChecksum;<a name="line.75"></a>
+<span class="sourceLineNo">076</span>    private boolean expectContentSize;<a name="line.76"></a>
+<span class="sourceLineNo">077</span>    private boolean expectContentChecksum;<a name="line.77"></a>
+<span class="sourceLineNo">078</span><a name="line.78"></a>
+<span class="sourceLineNo">079</span>    private InputStream currentBlock;<a name="line.79"></a>
+<span class="sourceLineNo">080</span>    private boolean endReached, inUncompressed;<a name="line.80"></a>
+<span class="sourceLineNo">081</span><a name="line.81"></a>
+<span class="sourceLineNo">082</span>    // used for frame header checksum and content checksum, if present<a name="line.82"></a>
+<span class="sourceLineNo">083</span>    private final XXHash32 contentHash = new XXHash32();<a name="line.83"></a>
+<span class="sourceLineNo">084</span><a name="line.84"></a>
+<span class="sourceLineNo">085</span>    /**<a name="line.85"></a>
+<span class="sourceLineNo">086</span>     * Creates a new input stream that decompresses streams compressed<a name="line.86"></a>
+<span class="sourceLineNo">087</span>     * using the LZ4 frame format.<a name="line.87"></a>
+<span class="sourceLineNo">088</span>     * @param in  the InputStream from which to read the compressed data<a name="line.88"></a>
+<span class="sourceLineNo">089</span>     * @throws IOException if reading fails<a name="line.89"></a>
+<span class="sourceLineNo">090</span>     */<a name="line.90"></a>
+<span class="sourceLineNo">091</span>    public FramedLZ4CompressorInputStream(InputStream in) throws IOException {<a name="line.91"></a>
+<span class="sourceLineNo">092</span>        this.in = in;<a name="line.92"></a>
+<span class="sourceLineNo">093</span>        readSignature();<a name="line.93"></a>
+<span class="sourceLineNo">094</span>        readFrameDescriptor();<a name="line.94"></a>
+<span class="sourceLineNo">095</span>        nextBlock();<a name="line.95"></a>
+<span class="sourceLineNo">096</span>    }<a name="line.96"></a>
+<span class="sourceLineNo">097</span><a name="line.97"></a>
+<span class="sourceLineNo">098</span>    /** {@inheritDoc} */<a name="line.98"></a>
+<span class="sourceLineNo">099</span>    @Override<a name="line.99"></a>
+<span class="sourceLineNo">100</span>    public int read() throws IOException {<a name="line.100"></a>
+<span class="sourceLineNo">101</span>        return read(oneByte, 0, 1) == -1 ? -1 : oneByte[0] &amp; 0xFF;<a name="line.101"></a>
+<span class="sourceLineNo">102</span>    }<a name="line.102"></a>
+<span class="sourceLineNo">103</span><a name="line.103"></a>
+<span class="sourceLineNo">104</span>    /** {@inheritDoc} */<a name="line.104"></a>
+<span class="sourceLineNo">105</span>    @Override<a name="line.105"></a>
+<span class="sourceLineNo">106</span>    public void close() throws IOException {<a name="line.106"></a>
+<span class="sourceLineNo">107</span>        if (currentBlock != null) {<a name="line.107"></a>
+<span class="sourceLineNo">108</span>            currentBlock.close();<a name="line.108"></a>
+<span class="sourceLineNo">109</span>            currentBlock = null;<a name="line.109"></a>
+<span class="sourceLineNo">110</span>        }<a name="line.110"></a>
+<span class="sourceLineNo">111</span>        in.close();<a name="line.111"></a>
+<span class="sourceLineNo">112</span>    }<a name="line.112"></a>
+<span class="sourceLineNo">113</span><a name="line.113"></a>
+<span class="sourceLineNo">114</span>    /** {@inheritDoc} */<a name="line.114"></a>
+<span class="sourceLineNo">115</span>    @Override<a name="line.115"></a>
+<span class="sourceLineNo">116</span>    public int read(final byte[] b, final int off, final int len) throws IOException {<a name="line.116"></a>
+<span class="sourceLineNo">117</span>        if (endReached) {<a name="line.117"></a>
+<span class="sourceLineNo">118</span>            return -1;<a name="line.118"></a>
+<span class="sourceLineNo">119</span>        }<a name="line.119"></a>
+<span class="sourceLineNo">120</span>        int r = readOnce(b, off, len);<a name="line.120"></a>
+<span class="sourceLineNo">121</span>        if (r == -1) {<a name="line.121"></a>
+<span class="sourceLineNo">122</span>            nextBlock();<a name="line.122"></a>
+<span class="sourceLineNo">123</span>            if (!endReached) {<a name="line.123"></a>
+<span class="sourceLineNo">124</span>                r = readOnce(b, off, len);<a name="line.124"></a>
+<span class="sourceLineNo">125</span>            }<a name="line.125"></a>
+<span class="sourceLineNo">126</span>        }<a name="line.126"></a>
+<span class="sourceLineNo">127</span>        if (expectContentChecksum &amp;&amp; r != -1) {<a name="line.127"></a>
+<span class="sourceLineNo">128</span>            contentHash.update(b, off, r);<a name="line.128"></a>
+<span class="sourceLineNo">129</span>        }<a name="line.129"></a>
+<span class="sourceLineNo">130</span>        return r;<a name="line.130"></a>
+<span class="sourceLineNo">131</span>    }<a name="line.131"></a>
+<span class="sourceLineNo">132</span><a name="line.132"></a>
+<span class="sourceLineNo">133</span>    private void readSignature() throws IOException {<a name="line.133"></a>
+<span class="sourceLineNo">134</span>        final byte[] b = new byte[4];<a name="line.134"></a>
+<span class="sourceLineNo">135</span>        final int read = IOUtils.readFully(in, b);<a name="line.135"></a>
+<span class="sourceLineNo">136</span>        count(read);<a name="line.136"></a>
+<span class="sourceLineNo">137</span>        if (4 != read || !matches(b, 4)) {<a name="line.137"></a>
+<span class="sourceLineNo">138</span>            throw new IOException("Not a LZ4 frame stream");<a name="line.138"></a>
+<span class="sourceLineNo">139</span>        }<a name="line.139"></a>
+<span class="sourceLineNo">140</span>    }<a name="line.140"></a>
+<span class="sourceLineNo">141</span><a name="line.141"></a>
+<span class="sourceLineNo">142</span>    private void readFrameDescriptor() throws IOException {<a name="line.142"></a>
+<span class="sourceLineNo">143</span>        int flags = readOneByte();<a name="line.143"></a>
+<span class="sourceLineNo">144</span>        if (flags == -1) {<a name="line.144"></a>
+<span class="sourceLineNo">145</span>            throw new IOException("Premature end of stream while reading frame flags");<a name="line.145"></a>
+<span class="sourceLineNo">146</span>        }<a name="line.146"></a>
+<span class="sourceLineNo">147</span>        contentHash.update(flags);<a name="line.147"></a>
+<span class="sourceLineNo">148</span>        if ((flags &amp; VERSION_MASK) != SUPPORTED_VERSION) {<a name="line.148"></a>
+<span class="sourceLineNo">149</span>            throw new IOException("Unsupported version " + (flags &gt;&gt; 6));<a name="line.149"></a>
+<span class="sourceLineNo">150</span>        }<a name="line.150"></a>
+<span class="sourceLineNo">151</span>        if ((flags &amp; BLOCK_INDEPENDENCE_MASK) == 0) {<a name="line.151"></a>
+<span class="sourceLineNo">152</span>            throw new IOException("Block dependence is not supported");<a name="line.152"></a>
+<span class="sourceLineNo">153</span>        }<a name="line.153"></a>
+<span class="sourceLineNo">154</span>        expectBlockChecksum = (flags &amp; BLOCK_CHECKSUM_MASK) != 0;<a name="line.154"></a>
+<span class="sourceLineNo">155</span>        expectContentSize = (flags &amp; CONTENT_SIZE_MASK) != 0;<a name="line.155"></a>
+<span class="sourceLineNo">156</span>        expectContentChecksum = (flags &amp; CONTENT_CHECKSUM_MASK) != 0;<a name="line.156"></a>
+<span class="sourceLineNo">157</span>        int bdByte = readOneByte();<a name="line.157"></a>
+<span class="sourceLineNo">158</span>        if (bdByte == -1) { // max size is irrelevant for this implementation<a name="line.158"></a>
+<span class="sourceLineNo">159</span>            throw new IOException("Premature end of stream while reading frame BD byte");<a name="line.159"></a>
+<span class="sourceLineNo">160</span>        }<a name="line.160"></a>
+<span class="sourceLineNo">161</span>        contentHash.update(bdByte);<a name="line.161"></a>
+<span class="sourceLineNo">162</span>        if (expectContentSize) { // for now we don't care, contains the uncompressed size<a name="line.162"></a>
+<span class="sourceLineNo">163</span>            byte[] contentSize = new byte[8];<a name="line.163"></a>
+<span class="sourceLineNo">164</span>            int skipped = (int) IOUtils.readFully(in, contentSize);<a name="line.164"></a>
+<span class="sourceLineNo">165</span>            count(skipped);<a name="line.165"></a>
+<span class="sourceLineNo">166</span>            if (8 != skipped) {<a name="line.166"></a>
+<span class="sourceLineNo">167</span>                throw new IOException("Premature end of stream while reading content size");<a name="line.167"></a>
+<span class="sourceLineNo">168</span>            }<a name="line.168"></a>
+<span class="sourceLineNo">169</span>            contentHash.update(contentSize, 0, contentSize.length);<a name="line.169"></a>
+<span class="sourceLineNo">170</span>        }<a name="line.170"></a>
+<span class="sourceLineNo">171</span>        int headerHash = readOneByte();<a name="line.171"></a>
+<span class="sourceLineNo">172</span>        if (headerHash == -1) { // partial hash of header.<a name="line.172"></a>
+<span class="sourceLineNo">173</span>            throw new IOException("Premature end of stream while reading frame header checksum");<a name="line.173"></a>
+<span class="sourceLineNo">174</span>        }<a name="line.174"></a>
+<span class="sourceLineNo">175</span>        int expectedHash = (int) ((contentHash.getValue() &gt;&gt; 8) &amp; 0xff);<a name="line.175"></a>
+<span class="sourceLineNo">176</span>        contentHash.reset();<a name="line.176"></a>
+<span class="sourceLineNo">177</span>        if (headerHash != expectedHash) {<a name="line.177"></a>
+<span class="sourceLineNo">178</span>            throw new IOException("frame header checksum mismatch.");<a name="line.178"></a>
+<span class="sourceLineNo">179</span>        }<a name="line.179"></a>
+<span class="sourceLineNo">180</span>    }<a name="line.180"></a>
+<span class="sourceLineNo">181</span><a name="line.181"></a>
+<span class="sourceLineNo">182</span>    private void nextBlock() throws IOException {<a name="line.182"></a>
+<span class="sourceLineNo">183</span>        maybeFinishCurrentBlock();<a name="line.183"></a>
+<span class="sourceLineNo">184</span>        long len = ByteUtils.fromLittleEndian(supplier, 4);<a name="line.184"></a>
+<span class="sourceLineNo">185</span>        boolean uncompressed = (len &amp; UNCOMPRESSED_FLAG_MASK) != 0;<a name="line.185"></a>
+<span class="sourceLineNo">186</span>        int realLen = (int) (len &amp; (~UNCOMPRESSED_FLAG_MASK));<a name="line.186"></a>
+<span class="sourceLineNo">187</span>        if (realLen == 0) {<a name="line.187"></a>
+<span class="sourceLineNo">188</span>            endReached = true;<a name="line.188"></a>
+<span class="sourceLineNo">189</span>            verifyContentChecksum();<a name="line.189"></a>
+<span class="sourceLineNo">190</span>            return;<a name="line.190"></a>
+<span class="sourceLineNo">191</span>        }<a name="line.191"></a>
+<span class="sourceLineNo">192</span>        InputStream capped = new BoundedInputStream(in, realLen);<a name="line.192"></a>
+<span class="sourceLineNo">193</span>        if (uncompressed) {<a name="line.193"></a>
+<span class="sourceLineNo">194</span>            inUncompressed = true;<a name="line.194"></a>
+<span class="sourceLineNo">195</span>            currentBlock = capped;<a name="line.195"></a>
+<span class="sourceLineNo">196</span>        } else {<a name="line.196"></a>
+<span class="sourceLineNo">197</span>            inUncompressed = false;<a name="line.197"></a>
+<span class="sourceLineNo">198</span>            currentBlock = new BlockLZ4CompressorInputStream(capped);<a name="line.198"></a>
+<span class="sourceLineNo">199</span>        }<a name="line.199"></a>
+<span class="sourceLineNo">200</span>    }<a name="line.200"></a>
+<span class="sourceLineNo">201</span><a name="line.201"></a>
+<span class="sourceLineNo">202</span>    private void maybeFinishCurrentBlock() throws IOException {<a name="line.202"></a>
+<span class="sourceLineNo">203</span>        if (currentBlock != null) {<a name="line.203"></a>
+<span class="sourceLineNo">204</span>            currentBlock.close();<a name="line.204"></a>
+<span class="sourceLineNo">205</span>            currentBlock = null;<a name="line.205"></a>
+<span class="sourceLineNo">206</span>            if (expectBlockChecksum) {<a name="line.206"></a>
+<span class="sourceLineNo">207</span>                int skipped = (int) IOUtils.skip(in, 4);<a name="line.207"></a>
+<span class="sourceLineNo">208</span>                count(skipped);<a name="line.208"></a>
+<span class="sourceLineNo">209</span>                if (4 != skipped) {<a name="line.209"></a>
+<span class="sourceLineNo">210</span>                    throw new IOException("Premature end of stream while reading block checksum");<a name="line.210"></a>
+<span class="sourceLineNo">211</span>                }<a name="line.211"></a>
+<span class="sourceLineNo">212</span>            }<a name="line.212"></a>
+<span class="sourceLineNo">213</span>        }<a name="line.213"></a>
+<span class="sourceLineNo">214</span>    }<a name="line.214"></a>
+<span class="sourceLineNo">215</span><a name="line.215"></a>
+<span class="sourceLineNo">216</span>    private void verifyContentChecksum() throws IOException {<a name="line.216"></a>
+<span class="sourceLineNo">217</span>        if (expectContentChecksum) {<a name="line.217"></a>
+<span class="sourceLineNo">218</span>            byte[] checksum = new byte[4];<a name="line.218"></a>
+<span class="sourceLineNo">219</span>            int read = IOUtils.readFully(in, checksum);<a name="line.219"></a>
+<span class="sourceLineNo">220</span>            count(read);<a name="line.220"></a>
+<span class="sourceLineNo">221</span>            if (4 != read) {<a name="line.221"></a>
+<span class="sourceLineNo">222</span>                throw new IOException("Premature end of stream while reading content checksum");<a name="line.222"></a>
+<span class="sourceLineNo">223</span>            }<a name="line.223"></a>
+<span class="sourceLineNo">224</span>            long expectedHash = contentHash.getValue();<a name="line.224"></a>
+<span class="sourceLineNo">225</span>            if (expectedHash != ByteUtils.fromLittleEndian(checksum)) {<a name="line.225"></a>
+<span class="sourceLineNo">226</span>                throw new IOException("content checksum mismatch.");<a name="line.226"></a>
+<span class="sourceLineNo">227</span>            }<a name="line.227"></a>
+<span class="sourceLineNo">228</span>            contentHash.reset();<a name="line.228"></a>
+<span class="sourceLineNo">229</span>        }<a name="line.229"></a>
+<span class="sourceLineNo">230</span>    }<a name="line.230"></a>
+<span class="sourceLineNo">231</span><a name="line.231"></a>
+<span class="sourceLineNo">232</span>    private int readOneByte() throws IOException {<a name="line.232"></a>
+<span class="sourceLineNo">233</span>        final int b = in.read();<a name="line.233"></a>
+<span class="sourceLineNo">234</span>        if (b != -1) {<a name="line.234"></a>
+<span class="sourceLineNo">235</span>            count(1);<a name="line.235"></a>
+<span class="sourceLineNo">236</span>            return b &amp; 0xFF;<a name="line.236"></a>
+<span class="sourceLineNo">237</span>        }<a name="line.237"></a>
+<span class="sourceLineNo">238</span>        return -1;<a name="line.238"></a>
+<span class="sourceLineNo">239</span>    }<a name="line.239"></a>
+<span class="sourceLineNo">240</span><a name="line.240"></a>
+<span class="sourceLineNo">241</span>    private int readOnce(byte[] b, int off, int len) throws IOException {<a name="line.241"></a>
+<span class="sourceLineNo">242</span>        if (inUncompressed) {<a name="line.242"></a>
+<span class="sourceLineNo">243</span>            int cnt = currentBlock.read(b, off, len);<a name="line.243"></a>
+<span class="sourceLineNo">244</span>            count(cnt);<a name="line.244"></a>
+<span class="sourceLineNo">245</span>            return cnt;<a name="line.245"></a>
+<span class="sourceLineNo">246</span>        } else {<a name="line.246"></a>
+<span class="sourceLineNo">247</span>            BlockLZ4CompressorInputStream l = (BlockLZ4CompressorInputStream) currentBlock;<a name="line.247"></a>
+<span class="sourceLineNo">248</span>            long before = l.getBytesRead();<a name="line.248"></a>
+<span class="sourceLineNo">249</span>            int cnt = currentBlock.read(b, off, len);<a name="line.249"></a>
+<span class="sourceLineNo">250</span>            count(l.getBytesRead() - before);<a name="line.250"></a>
+<span class="sourceLineNo">251</span>            return cnt;<a name="line.251"></a>
+<span class="sourceLineNo">252</span>        }<a name="line.252"></a>
+<span class="sourceLineNo">253</span>    }<a name="line.253"></a>
+<span class="sourceLineNo">254</span><a name="line.254"></a>
+<span class="sourceLineNo">255</span>    /**<a name="line.255"></a>
+<span class="sourceLineNo">256</span>     * Checks if the signature matches what is expected for a .lz4 file.<a name="line.256"></a>
+<span class="sourceLineNo">257</span>     *<a name="line.257"></a>
+<span class="sourceLineNo">258</span>     * &lt;p&gt;.lz4 files start with a four byte signature.&lt;/p&gt;<a name="line.258"></a>
+<span class="sourceLineNo">259</span>     *<a name="line.259"></a>
+<span class="sourceLineNo">260</span>     * @param signature the bytes to check<a name="line.260"></a>
+<span class="sourceLineNo">261</span>     * @param length    the number of bytes to check<a name="line.261"></a>
+<span class="sourceLineNo">262</span>     * @return          true if this is a .sz stream, false otherwise<a name="line.262"></a>
+<span class="sourceLineNo">263</span>     */<a name="line.263"></a>
+<span class="sourceLineNo">264</span>    public static boolean matches(final byte[] signature, final int length) {<a name="line.264"></a>
+<span class="sourceLineNo">265</span><a name="line.265"></a>
+<span class="sourceLineNo">266</span>        if (length &lt; LZ4_SIGNATURE.length) {<a name="line.266"></a>
+<span class="sourceLineNo">267</span>            return false;<a name="line.267"></a>
+<span class="sourceLineNo">268</span>        }<a name="line.268"></a>
+<span class="sourceLineNo">269</span><a name="line.269"></a>
+<span class="sourceLineNo">270</span>        byte[] shortenedSig = signature;<a name="line.270"></a>
+<span class="sourceLineNo">271</span>        if (signature.length &gt; LZ4_SIGNATURE.length) {<a name="line.271"></a>
+<span class="sourceLineNo">272</span>            shortenedSig = new byte[LZ4_SIGNATURE.length];<a name="line.272"></a>
+<span class="sourceLineNo">273</span>            System.arraycopy(signature, 0, shortenedSig, 0, LZ4_SIGNATURE.length);<a name="line.273"></a>
+<span class="sourceLineNo">274</span>        }<a name="line.274"></a>
+<span class="sourceLineNo">275</span><a name="line.275"></a>
+<span class="sourceLineNo">276</span>        return Arrays.equals(shortenedSig, LZ4_SIGNATURE);<a name="line.276"></a>
+<span class="sourceLineNo">277</span>    }<a name="line.277"></a>
+<span class="sourceLineNo">278</span>}<a name="line.278"></a>
 
 
 



Mime
View raw message