001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.fusesource.hawtbuf.codec; 018 019 import java.io.DataInput; 020 import java.io.DataOutput; 021 import java.io.IOException; 022 023 /** 024 */ 025 public interface Codec<T> { 026 027 /** 028 * Write the payload of the object to the DataOutput stream. 029 * 030 * @param object 031 * @param dataOut 032 * @throws IOException 033 */ 034 void encode(T object, DataOutput dataOut) throws IOException; 035 036 037 /** 038 * Read the payload of the object from the DataInput stream. 039 * 040 * @param dataIn 041 * @return unmarshalled object 042 * @throws IOException 043 */ 044 T decode(DataInput dataIn) throws IOException; 045 046 /** 047 * @return -1 if the object do not always marshall to a fixed size, otherwise return that fixed size. 048 */ 049 int getFixedSize(); 050 051 /** 052 * 053 * @return true if the {@link #estimatedSize(Object)} operation is supported. 054 */ 055 boolean isEstimatedSizeSupported(); 056 057 /** 058 * @param object 059 * @return the estimated marshaled size of the object. 060 */ 061 int estimatedSize(T object); 062 063 /** 064 * 065 * @return true if the {@link #deepCopy(Object)} operations is supported. 066 */ 067 boolean isDeepCopySupported(); 068 069 /** 070 * @return a deep copy of the source object. If the source is immutable 071 * the same source should be returned. 072 */ 073 T deepCopy(T source); 074 075 }