THRIFT-820 Remove readLength attribute from BinaryProtocol

Patch: Carl Yeksigian
This commit is contained in:
Carl Yeksigian 2013-08-14 19:37:54 -04:00
parent d051ca0b23
commit 2ca9c20285
6 changed files with 4 additions and 154 deletions

View File

@ -38,9 +38,6 @@ package org.apache.thrift.protocol {
protected var strictRead_:Boolean = false;
protected var strictWrite_:Boolean = true;
protected var readLength_:int;
protected var checkReadLength_:Boolean = false;
/**
* Factory
*/
@ -298,7 +295,6 @@ package org.apache.thrift.protocol {
public function readBinary():ByteArray {
var size:int = readI32();
checkReadLength(size);
var buf:ByteArray = new ByteArray();
trans_.readAll(buf, 0, size);
return buf;
@ -307,25 +303,10 @@ package org.apache.thrift.protocol {
private function readAll(len:int):void {
reset(bytes);
checkReadLength(len);
trans_.readAll(bytes, 0, len);
bytes.position = 0;
}
public function setReadLength(readLength:int):void {
readLength_ = readLength;
checkReadLength_ = true;
}
protected function checkReadLength(length:int):void {
if (checkReadLength_) {
readLength_ -= length;
if (readLength_ < 0) {
throw new TError("Message length exceeded: " + length);
}
}
}
private static function reset(arr:ByteArray):void {
arr.length = 0;

View File

@ -35,10 +35,6 @@ namespace Thrift.Protocol
protected bool strictRead_ = false;
protected bool strictWrite_ = true;
protected int readLength_;
protected bool checkReadLength_ = false;
#region BinaryProtocol Factory
/**
* Factory
@ -375,35 +371,15 @@ namespace Thrift.Protocol
#endif
}
public void SetReadLength(int readLength)
{
readLength_ = readLength;
checkReadLength_ = true;
}
protected void CheckReadLength(int length)
{
if (checkReadLength_)
{
readLength_ -= length;
if (readLength_ < 0)
{
throw new Exception("Message length exceeded: " + length);
}
}
}
public override byte[] ReadBinary()
{
int size = ReadI32();
CheckReadLength(size);
byte[] buf = new byte[size];
trans.ReadAll(buf, 0, size);
return buf;
}
private string ReadStringBody(int size)
{
CheckReadLength(size);
byte[] buf = new byte[size];
trans.ReadAll(buf, 0, size);
return Encoding.UTF8.GetString(buf, 0, buf.Length);
@ -411,7 +387,6 @@ namespace Thrift.Protocol
private int ReadAll(byte[] buf, int off, int len)
{
CheckReadLength(len);
return trans.ReadAll(buf, off, len);
}

View File

@ -370,13 +370,11 @@ type
protected
FStrictRead : Boolean;
FStrictWrite : Boolean;
FReadLength : Integer;
FCheckReadLength : Boolean;
private
function ReadAll( var buf: TBytes; off: Integer; len: Integer ): Integer;
function ReadStringBody( size: Integer): string;
procedure CheckReadLength( len: Integer );
public
type
@ -434,7 +432,6 @@ type
function ReadDouble:Double; override;
function ReadBinary: TBytes; override;
procedure SetReadLength( readLength: Integer );
end;
@ -859,18 +856,6 @@ begin
Create( trans, False, True);
end;
procedure TBinaryProtocolImpl.CheckReadLength(len: Integer);
begin
if FCheckReadLength then
begin
Dec( FReadLength, len);
if FReadLength < 0 then
begin
raise Exception.Create( 'Message length exceeded: ' + IntToStr( len ) );
end;
end;
end;
constructor TBinaryProtocolImpl.Create( const trans: ITransport; strictRead,
strictWrite: Boolean);
begin
@ -882,7 +867,6 @@ end;
function TBinaryProtocolImpl.ReadAll( var buf: TBytes; off,
len: Integer): Integer;
begin
CheckReadLength( len );
Result := FTrans.ReadAll( buf, off, len );
end;
@ -892,7 +876,6 @@ var
buf : TBytes;
begin
size := ReadI32;
CheckReadLength( size );
SetLength( buf, size );
FTrans.ReadAll( buf, 0, size);
Result := buf;
@ -1063,7 +1046,6 @@ function TBinaryProtocolImpl.ReadStringBody( size: Integer): string;
var
buf : TBytes;
begin
CheckReadLength( size );
SetLength( buf, size );
FTrans.ReadAll( buf, 0, size );
Result := TEncoding.UTF8.GetString( buf);
@ -1080,12 +1062,6 @@ begin
end;
procedure TBinaryProtocolImpl.SetReadLength(readLength: Integer);
begin
FReadLength := readLength;
FCheckReadLength := True;
end;
procedure TBinaryProtocolImpl.WriteBinary( const b: TBytes);
var iLen : Integer;
begin

View File

@ -30,8 +30,6 @@ type TBinaryProtocol struct {
trans TTransport
strictRead bool
strictWrite bool
readLength int
checkReadLength bool
buffer [8]byte
}
@ -45,8 +43,7 @@ func NewTBinaryProtocolTransport(t TTransport) *TBinaryProtocol {
}
func NewTBinaryProtocol(t TTransport, strictRead, strictWrite bool) *TBinaryProtocol {
//return &TBinaryProtocol{TProtocolBase:TProtocolBase{trans:t}, strictRead:strictRead, strictWrite:strictWrite, readLength:0, checkReadLength:false};
return &TBinaryProtocol{trans: t, strictRead: strictRead, strictWrite: strictWrite, readLength: 0, checkReadLength: false}
return &TBinaryProtocol{trans: t, strictRead: strictRead, strictWrite: strictWrite}
}
func NewTBinaryProtocolFactoryDefault() *TBinaryProtocolFactory {
@ -412,9 +409,6 @@ func (p *TBinaryProtocol) ReadBinary() ([]byte, error) {
return nil, e
}
isize := int(size)
if e = p.readLengthOk(isize); e != nil {
return nil, e
}
buf := make([]byte, isize)
_, err := io.ReadFull(p.trans, buf)
return buf, NewTProtocolException(err)
@ -433,35 +427,14 @@ func (p *TBinaryProtocol) Transport() TTransport {
}
func (p *TBinaryProtocol) readAll(buf []byte) error {
if e := p.readLengthOk(len(buf)); e != nil {
return e
}
_, err := io.ReadFull(p.trans, buf)
return NewTProtocolException(err)
}
func (p *TBinaryProtocol) setReadLength(readLength int) {
p.readLength = readLength
p.checkReadLength = true
}
func (p *TBinaryProtocol) readLengthOk(length int) error {
if p.checkReadLength {
p.readLength = p.readLength - length
if p.readLength < 0 {
return NewTProtocolExceptionWithType(UNKNOWN_PROTOCOL_EXCEPTION, fmt.Errorf("Message length exceeded: %d", length))
}
}
return nil
}
func (p *TBinaryProtocol) readStringBody(size int) (value string, err error) {
if size < 0 {
return "", nil
}
if err := p.readLengthOk(size); err != nil {
return "", err
}
isize := int(size)
buf := make([]byte, isize)
_, e := io.ReadFull(p.trans, buf)

View File

@ -38,37 +38,24 @@ public class TBinaryProtocol extends TProtocol {
protected boolean strictRead_ = false;
protected boolean strictWrite_ = true;
protected int readLength_;
protected boolean checkReadLength_ = false;
/**
* Factory
*/
public static class Factory implements TProtocolFactory {
protected boolean strictRead_ = false;
protected boolean strictWrite_ = true;
protected int readLength_;
public Factory() {
this(false, true);
}
public Factory(boolean strictRead, boolean strictWrite) {
this(strictRead, strictWrite, 0);
}
public Factory(boolean strictRead, boolean strictWrite, int readLength) {
strictRead_ = strictRead;
strictWrite_ = strictWrite;
readLength_ = readLength;
}
public TProtocol getProtocol(TTransport trans) {
TBinaryProtocol proto = new TBinaryProtocol(trans, strictRead_, strictWrite_);
if (readLength_ != 0) {
proto.setReadLength(readLength_);
}
return proto;
return new TBinaryProtocol(trans, strictRead_, strictWrite_);
}
}
@ -349,7 +336,6 @@ public class TBinaryProtocol extends TProtocol {
public String readStringBody(int size) throws TException {
try {
checkReadLength(size);
byte[] buf = new byte[size];
trans_.readAll(buf, 0, size);
return new String(buf, "UTF-8");
@ -360,7 +346,6 @@ public class TBinaryProtocol extends TProtocol {
public ByteBuffer readBinary() throws TException {
int size = readI32();
checkReadLength(size);
if (trans_.getBytesRemainingInBuffer() >= size) {
ByteBuffer bb = ByteBuffer.wrap(trans_.getBuffer(), trans_.getBufferPosition(), size);
@ -374,25 +359,6 @@ public class TBinaryProtocol extends TProtocol {
}
private int readAll(byte[] buf, int off, int len) throws TException {
checkReadLength(len);
return trans_.readAll(buf, off, len);
}
public void setReadLength(int readLength) {
readLength_ = readLength;
checkReadLength_ = true;
}
protected void checkReadLength(int length) throws TException {
if (length < 0) {
throw new TProtocolException("Negative length: " + length);
}
if (checkReadLength_) {
readLength_ -= length;
if (readLength_ < 0) {
throw new TProtocolException("Message length exceeded: " + length);
}
}
}
}

View File

@ -1,4 +1,4 @@
/*
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
@ -36,9 +36,6 @@ public class TBinaryProtocol extends TProtocol {
protected boolean strictRead_ = false;
protected boolean strictWrite_ = true;
protected int readLength_;
protected boolean checkReadLength_ = false;
/**
* Factory
*/
@ -311,7 +308,6 @@ public class TBinaryProtocol extends TProtocol {
public String readStringBody(int size) throws TException {
try {
checkReadLength(size);
byte[] buf = new byte[size];
trans_.readAll(buf, 0, size);
return new String(buf, "UTF-8");
@ -322,29 +318,12 @@ public class TBinaryProtocol extends TProtocol {
public byte[] readBinary() throws TException {
int size = readI32();
checkReadLength(size);
byte[] buf = new byte[size];
trans_.readAll(buf, 0, size);
return buf;
}
private int readAll(byte[] buf, int off, int len) throws TException {
checkReadLength(len);
return trans_.readAll(buf, off, len);
}
public void setReadLength(int readLength) {
readLength_ = readLength;
checkReadLength_ = true;
}
protected void checkReadLength(int length) throws TException {
if (checkReadLength_) {
readLength_ -= length;
if (readLength_ < 0) {
throw new TException("Message length exceeded: " + length);
}
}
}
}