001 /**
002 * Copyright (C) 20010, Progress Software Corporation and/or its
003 * subsidiaries or affiliates. All rights reserved.
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * 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.proto;
018
019 import org.fusesource.hawtbuf.codec.VariableCodec;
020
021 import java.io.*;
022
023 /**
024 * <p>
025 * Implements the Codec interface for PBMessages type which
026 * encode/decodes unframed messages.
027 * </p>
028 *
029 * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
030 */
031 public class PBMessageUnframedCodec<T extends MessageBuffer> extends VariableCodec<T> {
032
033 final private PBMessageFactory<?, ? extends T> factory;
034
035 public PBMessageUnframedCodec(PBMessageFactory<?, ? extends T> factory) {
036 this.factory = factory;
037 }
038
039 public T decode(DataInput dataIn) throws IOException {
040 return (T)factory.parseUnframed((InputStream) dataIn).freeze();
041 }
042
043 public void encode(T value, DataOutput dataOut) throws IOException {
044 value.writeUnframed((OutputStream) dataOut);
045 }
046
047 @Override
048 public boolean isEstimatedSizeSupported() {
049 return true;
050 }
051
052 public int estimatedSize(T value) {
053 return value.serializedSizeUnframed();
054 }
055
056 @Override
057 public boolean isDeepCopySupported() {
058 return true;
059 }
060
061 @Override
062 public T deepCopy(T value) {
063 return (T) value;
064 }
065 }