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 * Implementation of a Marshaller for Strings 025 * 026 */ 027 public class StringCodec implements Codec<String> { 028 029 public static final StringCodec INSTANCE = new StringCodec(); 030 031 /** 032 * Write the payload of this entry to the RawContainer 033 * 034 * @param object 035 * @param dataOut 036 * @throws IOException 037 */ 038 public void encode(String object, DataOutput dataOut) throws IOException { 039 dataOut.writeUTF(object); 040 } 041 042 /** 043 * Read the entry from the RawContainer 044 * 045 * @param dataIn 046 * @return unmarshalled object 047 * @throws IOException 048 */ 049 public String decode(DataInput dataIn) throws IOException { 050 return dataIn.readUTF(); 051 } 052 053 054 public int getFixedSize() { 055 return -1; 056 } 057 058 public String deepCopy(String source) { 059 return source; 060 } 061 062 public boolean isDeepCopySupported() { 063 return true; 064 } 065 066 public boolean isEstimatedSizeSupported() { 067 return true; 068 } 069 070 public int estimatedSize(String object) { 071 return object.length()+2; 072 } 073 }