001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 019package org.apache.hadoop.io; 020 021import java.io.DataOutput; 022import java.io.DataInput; 023import java.io.IOException; 024 025import org.apache.hadoop.classification.InterfaceAudience; 026import org.apache.hadoop.classification.InterfaceStability; 027 028/** 029 * A serializable object which implements a simple, efficient, serialization 030 * protocol, based on {@link DataInput} and {@link DataOutput}. 031 * 032 * <p>Any <code>key</code> or <code>value</code> type in the Hadoop Map-Reduce 033 * framework implements this interface.</p> 034 * 035 * <p>Implementations typically implement a static <code>read(DataInput)</code> 036 * method which constructs a new instance, calls {@link #readFields(DataInput)} 037 * and returns the instance.</p> 038 * 039 * <p>Example:</p> 040 * <p><blockquote><pre> 041 * public class MyWritable implements Writable { 042 * // Some data 043 * private int counter; 044 * private long timestamp; 045 * 046 * public void write(DataOutput out) throws IOException { 047 * out.writeInt(counter); 048 * out.writeLong(timestamp); 049 * } 050 * 051 * public void readFields(DataInput in) throws IOException { 052 * counter = in.readInt(); 053 * timestamp = in.readLong(); 054 * } 055 * 056 * public static MyWritable read(DataInput in) throws IOException { 057 * MyWritable w = new MyWritable(); 058 * w.readFields(in); 059 * return w; 060 * } 061 * } 062 * </pre></blockquote></p> 063 */ 064@InterfaceAudience.Public 065@InterfaceStability.Stable 066public interface Writable { 067 /** 068 * Serialize the fields of this object to <code>out</code>. 069 * 070 * @param out <code>DataOuput</code> to serialize this object into. 071 * @throws IOException 072 */ 073 void write(DataOutput out) throws IOException; 074 075 /** 076 * Deserialize the fields of this object from <code>in</code>. 077 * 078 * <p>For efficiency, implementations should attempt to re-use storage in the 079 * existing object where possible.</p> 080 * 081 * @param in <code>DataInput</code> to deseriablize this object from. 082 * @throws IOException 083 */ 084 void readFields(DataInput in) throws IOException; 085}