[120] | 1 | /* |
---|
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
| 3 | * or more contributor license agreements. See the NOTICE file |
---|
| 4 | * distributed with this work for additional information |
---|
| 5 | * regarding copyright ownership. The ASF licenses this file |
---|
| 6 | * to you under the Apache License, Version 2.0 (the |
---|
| 7 | * "License"); you may not use this file except in compliance |
---|
| 8 | * with the License. You may obtain a copy of the License at |
---|
| 9 | * |
---|
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 11 | * |
---|
| 12 | * Unless required by applicable law or agreed to in writing, software |
---|
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 15 | * See the License for the specific language governing permissions and |
---|
| 16 | * limitations under the License. |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | package org.apache.hadoop.io.compress; |
---|
| 20 | |
---|
| 21 | import java.io.IOException; |
---|
| 22 | import java.io.OutputStream; |
---|
| 23 | |
---|
| 24 | import org.apache.hadoop.io.compress.CompressionOutputStream; |
---|
| 25 | import org.apache.hadoop.io.compress.Compressor; |
---|
| 26 | |
---|
| 27 | public class CompressorStream extends CompressionOutputStream { |
---|
| 28 | protected Compressor compressor; |
---|
| 29 | protected byte[] buffer; |
---|
| 30 | protected boolean closed = false; |
---|
| 31 | |
---|
| 32 | public CompressorStream(OutputStream out, Compressor compressor, int bufferSize) { |
---|
| 33 | super(out); |
---|
| 34 | |
---|
| 35 | if (out == null || compressor == null) { |
---|
| 36 | throw new NullPointerException(); |
---|
| 37 | } else if (bufferSize <= 0) { |
---|
| 38 | throw new IllegalArgumentException("Illegal bufferSize"); |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | this.compressor = compressor; |
---|
| 42 | buffer = new byte[bufferSize]; |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | public CompressorStream(OutputStream out, Compressor compressor) { |
---|
| 46 | this(out, compressor, 512); |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | /** |
---|
| 50 | * Allow derived classes to directly set the underlying stream. |
---|
| 51 | * |
---|
| 52 | * @param out Underlying output stream. |
---|
| 53 | */ |
---|
| 54 | protected CompressorStream(OutputStream out) { |
---|
| 55 | super(out); |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | public void write(byte[] b, int off, int len) throws IOException { |
---|
| 59 | // Sanity checks |
---|
| 60 | if (compressor.finished()) { |
---|
| 61 | throw new IOException("write beyond end of stream"); |
---|
| 62 | } |
---|
| 63 | if ((off | len | (off + len) | (b.length - (off + len))) < 0) { |
---|
| 64 | throw new IndexOutOfBoundsException(); |
---|
| 65 | } else if (len == 0) { |
---|
| 66 | return; |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | compressor.setInput(b, off, len); |
---|
| 70 | while (!compressor.needsInput()) { |
---|
| 71 | compress(); |
---|
| 72 | } |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | protected void compress() throws IOException { |
---|
| 76 | int len = compressor.compress(buffer, 0, buffer.length); |
---|
| 77 | if (len > 0) { |
---|
| 78 | out.write(buffer, 0, len); |
---|
| 79 | } |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | public void finish() throws IOException { |
---|
| 83 | if (!compressor.finished()) { |
---|
| 84 | compressor.finish(); |
---|
| 85 | while (!compressor.finished()) { |
---|
| 86 | compress(); |
---|
| 87 | } |
---|
| 88 | } |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | public void resetState() throws IOException { |
---|
| 92 | compressor.reset(); |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | public void close() throws IOException { |
---|
| 96 | if (!closed) { |
---|
| 97 | finish(); |
---|
| 98 | out.close(); |
---|
| 99 | closed = true; |
---|
| 100 | } |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | private byte[] oneByte = new byte[1]; |
---|
| 104 | public void write(int b) throws IOException { |
---|
| 105 | oneByte[0] = (byte)(b & 0xff); |
---|
| 106 | write(oneByte, 0, oneByte.length); |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | } |
---|