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 * // Default constructor to allow (de)serialization 047 * MyWritable() { } 048 * 049 * public void write(DataOutput out) throws IOException { 050 * out.writeInt(counter); 051 * out.writeLong(timestamp); 052 * } 053 * 054 * public void readFields(DataInput in) throws IOException { 055 * counter = in.readInt(); 056 * timestamp = in.readLong(); 057 * } 058 * 059 * public static MyWritable read(DataInput in) throws IOException { 060 * MyWritable w = new MyWritable(); 061 * w.readFields(in); 062 * return w; 063 * } 064 * } 065 * </pre></blockquote></p> 066 */ 067@InterfaceAudience.Public 068@InterfaceStability.Stable 069public interface Writable { 070 /** 071 * Serialize the fields of this object to <code>out</code>. 072 * 073 * @param out <code>DataOuput</code> to serialize this object into. 074 * @throws IOException 075 */ 076 void write(DataOutput out) throws IOException; 077 078 /** 079 * Deserialize the fields of this object from <code>in</code>. 080 * 081 * <p>For efficiency, implementations should attempt to re-use storage in the 082 * existing object where possible.</p> 083 * 084 * @param in <code>DataInput</code> to deseriablize this object from. 085 * @throws IOException 086 */ 087 void readFields(DataInput in) throws IOException; 088}