@@ -0,0 +1,3157 @@
+/*%0A * Licensed to Elasticsearch under one or more contributor%0A * license agreements. See the NOTICE file distributed with%0A * this work for additional information regarding copyright%0A * ownership. Elasticsearch licenses this file to you under%0A * the Apache License, Version 2.0 (the %22License%22); you may%0A * not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A *    http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing,%0A * software distributed under the License is distributed on an%0A * %22AS IS%22 BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY%0A * KIND, either express or implied.  See the License for the%0A * specific language governing permissions and limitations%0A * under the License.%0A */%0A%0Apackage org.elasticsearch.common.compress;%0A%0Aimport org.apache.lucene.util.TestUtil;%0Aimport org.elasticsearch.common.io.stream.ByteBufferStreamInput;%0Aimport org.elasticsearch.common.io.stream.OutputStreamStreamOutput;%0Aimport org.elasticsearch.common.io.stream.StreamInput;%0Aimport org.elasticsearch.common.io.stream.StreamOutput;%0Aimport org.elasticsearch.common.settings.ImmutableSettings;%0Aimport org.elasticsearch.test.ElasticsearchTestCase;%0A%0Aimport java.io.ByteArrayOutputStream;%0Aimport java.io.IOException;%0Aimport java.nio.ByteBuffer;%0Aimport java.util.Random;%0A%0A/**%0A * Test streaming compression (e.g. used for recovery)%0A */%0Apublic class CompressedStreamTests extends ElasticsearchTestCase %7B%0A%0A    public void testRandom() throws IOException %7B%0A        String compressor = %22lzf%22;%0A        CompressorFactory.configure(ImmutableSettings.settingsBuilder().put(%22compress.default.type%22, compressor).build());%0A        Random r = getRandom();%0A        for (int i = 0; i %3C 2000; i++) %7B%0A            byte bytes%5B%5D = new byte%5BTestUtil.nextInt(r, 1, 10000)%5D;%0A            r.nextBytes(bytes);%0A            %0A            ByteBuffer bb = ByteBuffer.wrap(bytes);%0A            StreamInput rawIn = new ByteBufferStreamInput(bb);%0A            Compressor c = CompressorFactory.defaultCompressor();%0A           %0A            ByteArrayOutputStream bos = new ByteArrayOutputStream();%0A            OutputStreamStreamOutput rawOs = new OutputStreamStreamOutput(bos);%0A            StreamOutput os = c.streamOutput(rawOs);%0A            %0A            byte buffer%5B%5D = new byte%5B1024%5D; //arbitrary%0A            int len;%0A            while ((len = rawIn.read(buffer)) != -1) %7B%0A              os.write(buffer, 0, len);%0A            %7D%0A            os.close();%0A            %0A            // now we have compressed byte array%0A            %0A            byte compressed%5B%5D = bos.toByteArray();%0A            ByteBuffer bb2 = ByteBuffer.wrap(compressed);%0A            StreamInput compressedIn = new ByteBufferStreamInput(bb2);%0A            StreamInput in = c.streamInput(compressedIn);%0A           %0A            ByteArrayOutputStream uncompressedOut = new ByteArrayOutputStream();%0A            while ((len = in.read(buffer)) != -1) %7B%0A              uncompressedOut.write(buffer, 0, len);%0A            %7D%0A            uncompressedOut.close();%0A            %0A            assertArrayEquals(bytes, uncompressedOut.toByteArray());%0A        %7D%0A    %7D%0A%7D%0A

test some random bytes with compressed streams