source: proiecte/HadoopJUnit/hadoop-0.20.1/src/core/org/apache/hadoop/io/CompressedWritable.java @ 120

Last change on this file since 120 was 120, checked in by (none), 14 years ago

Added the mail files for the Hadoop JUNit Project

  • Property svn:executable set to *
File size: 3.1 KB
Line 
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
19package org.apache.hadoop.io;
20
21import java.io.IOException;
22import java.io.DataInput;
23import java.io.DataOutput;
24import java.io.DataOutputStream;
25import java.io.DataInputStream;
26import java.io.ByteArrayOutputStream;
27import java.io.ByteArrayInputStream;
28import java.util.zip.Deflater;
29import java.util.zip.DeflaterOutputStream;
30import 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. */
37public 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}
Note: See TracBrowser for help on using the repository browser.