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    import org.fusesource.hawtbuf.Buffer;
024    
025    /**
026     * Implementation of a Marshaller for Buffer objects
027     * 
028     * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
029     */
030    public class FixedBufferCodec implements Codec<Buffer> {
031        
032        private final int size;
033    
034        public FixedBufferCodec(int size) {
035            this.size = size;
036        }
037    
038        public void encode(Buffer value, DataOutput dataOut) throws IOException {
039            dataOut.write(value.data, value.offset, size);
040        }
041    
042        public Buffer decode(DataInput dataIn) throws IOException {
043            byte[] data = new byte[size];
044            dataIn.readFully(data);
045            return new Buffer(data);
046        }
047    
048        public int getFixedSize() {
049            return size;
050        }
051    
052        public Buffer deepCopy(Buffer source) {
053            return source.deepCopy();
054        }
055    
056        public boolean isDeepCopySupported() {
057            return true;
058        }
059    
060        public boolean isEstimatedSizeSupported() {
061            return true;
062        }
063        public int estimatedSize(Buffer object) {
064            return size;
065        }
066        
067    }