[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; |
---|
| 20 | |
---|
| 21 | import java.io.IOException; |
---|
| 22 | import java.io.DataInput; |
---|
| 23 | import java.io.DataOutput; |
---|
| 24 | import java.io.DataOutputStream; |
---|
| 25 | import java.io.DataInputStream; |
---|
| 26 | import java.io.ByteArrayOutputStream; |
---|
| 27 | import java.io.ByteArrayInputStream; |
---|
| 28 | import java.util.zip.Deflater; |
---|
| 29 | import java.util.zip.DeflaterOutputStream; |
---|
| 30 | import java.util.zip.InflaterInputStream; |
---|
| 31 | |
---|
| 32 | /** A base-class for Writables which store themselves compressed and lazily |
---|
| 33 | * inflate on field access. This is useful for large objects whose fields are |
---|
| 34 | * not be altered during a map or reduce operation: leaving the field data |
---|
| 35 | * compressed makes copying the instance from one file to another much |
---|
| 36 | * faster. */ |
---|
| 37 | public abstract class CompressedWritable implements Writable { |
---|
| 38 | // if non-null, the compressed field data of this instance. |
---|
| 39 | private byte[] compressed; |
---|
| 40 | |
---|
| 41 | public CompressedWritable() {} |
---|
| 42 | |
---|
| 43 | public final void readFields(DataInput in) throws IOException { |
---|
| 44 | compressed = new byte[in.readInt()]; |
---|
| 45 | in.readFully(compressed, 0, compressed.length); |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | /** Must be called by all methods which access fields to ensure that the data |
---|
| 49 | * has been uncompressed. */ |
---|
| 50 | protected void ensureInflated() { |
---|
| 51 | if (compressed != null) { |
---|
| 52 | try { |
---|
| 53 | ByteArrayInputStream deflated = new ByteArrayInputStream(compressed); |
---|
| 54 | DataInput inflater = |
---|
| 55 | new DataInputStream(new InflaterInputStream(deflated)); |
---|
| 56 | readFieldsCompressed(inflater); |
---|
| 57 | compressed = null; |
---|
| 58 | } catch (IOException e) { |
---|
| 59 | throw new RuntimeException(e); |
---|
| 60 | } |
---|
| 61 | } |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | /** Subclasses implement this instead of {@link #readFields(DataInput)}. */ |
---|
| 65 | protected abstract void readFieldsCompressed(DataInput in) |
---|
| 66 | throws IOException; |
---|
| 67 | |
---|
| 68 | public final void write(DataOutput out) throws IOException { |
---|
| 69 | if (compressed == null) { |
---|
| 70 | ByteArrayOutputStream deflated = new ByteArrayOutputStream(); |
---|
| 71 | Deflater deflater = new Deflater(Deflater.BEST_SPEED); |
---|
| 72 | DataOutputStream dout = |
---|
| 73 | new DataOutputStream(new DeflaterOutputStream(deflated, deflater)); |
---|
| 74 | writeCompressed(dout); |
---|
| 75 | dout.close(); |
---|
| 76 | deflater.end(); |
---|
| 77 | compressed = deflated.toByteArray(); |
---|
| 78 | } |
---|
| 79 | out.writeInt(compressed.length); |
---|
| 80 | out.write(compressed); |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | /** Subclasses implement this instead of {@link #write(DataOutput)}. */ |
---|
| 84 | protected abstract void writeCompressed(DataOutput out) throws IOException; |
---|
| 85 | |
---|
| 86 | } |
---|