mirror of
https://github.com/valitydev/thrift.git
synced 2024-11-07 02:45:22 +00:00
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
This commit is contained in:
parent
80f6398b63
commit
a7f879b81f
73
lib/java/src/transport/TMemoryBuffer.java
Normal file
73
lib/java/src/transport/TMemoryBuffer.java
Normal file
@ -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 <chad@powerset.com>
|
||||
*/
|
||||
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_;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user