From a7f879b81f9058c5843d5b1bffeb8224db1c3a3b Mon Sep 17 00:00:00 2001 From: David Reiss Date: Fri, 7 Mar 2008 20:12:17 +0000 Subject: [PATCH] Add Java memory buffer transport implementation. Wraps a TByteArrayOutputStream. git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665561 13f79535-47bb-0310-9956-ffa450edef68 --- lib/java/src/transport/TMemoryBuffer.java | 73 +++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 lib/java/src/transport/TMemoryBuffer.java diff --git a/lib/java/src/transport/TMemoryBuffer.java b/lib/java/src/transport/TMemoryBuffer.java new file mode 100644 index 00000000..d558f6be --- /dev/null +++ b/lib/java/src/transport/TMemoryBuffer.java @@ -0,0 +1,73 @@ +// Copyright (c) 2006- Facebook +// Distributed under the Thrift Software License +// +// See accompanying file LICENSE or visit the Thrift site at: +// http://developers.facebook.com/thrift/ + +package com.facebook.thrift.transport; + +import com.facebook.thrift.TByteArrayOutputStream; +import java.io.UnsupportedEncodingException; + +/** + * Memory buffer-based implementation of the TTransport interface. + * + * @author Chad Walters + */ +public class TMemoryBuffer extends TTransport { + + /** + * + */ + public TMemoryBuffer(int size) { + arr_ = new TByteArrayOutputStream(size); + } + + @Override + public boolean isOpen() { + return true; + } + + @Override + public void open() { + /* Do nothing */ + } + + @Override + public void close() { + /* Do nothing */ + } + + @Override + public int read(byte[] buf, int off, int len) { + byte[] src = arr_.get(); + int amtToRead = (len > arr_.len() - pos_ ? arr_.len() - pos_ : len); + if (amtToRead > 0) { + System.arraycopy(src, pos_, buf, off, amtToRead); + pos_ += amtToRead; + } + return amtToRead; + } + + @Override + public void write(byte[] buf, int off, int len) { + arr_.write(buf, off, len); + } + + /** + * Output the contents of the memory buffer as a String, using the supplied + * encoding + * @param enc the encoding to use + * @return the contents of the memory buffer as a String + */ + public String toString(String enc) throws UnsupportedEncodingException { + return arr_.toString(enc); + } + + // The contents of the buffer + private TByteArrayOutputStream arr_; + + // Position to read next byte from + private int pos_; +} + -- 2.17.1