THRIFT-820 Remove readLength attribute from BinaryProtocol

Patch: Carl Yeksigian
diff --git a/lib/as3/src/org/apache/thrift/protocol/TBinaryProtocol.as b/lib/as3/src/org/apache/thrift/protocol/TBinaryProtocol.as
index 4412479..b2ff9d8 100644
--- a/lib/as3/src/org/apache/thrift/protocol/TBinaryProtocol.as
+++ b/lib/as3/src/org/apache/thrift/protocol/TBinaryProtocol.as
@@ -38,9 +38,6 @@
     protected var strictRead_:Boolean = false;
     protected var strictWrite_:Boolean = true;
     
-    protected var readLength_:int;
-    protected var checkReadLength_:Boolean = false;
-
   /**
    * Factory
    */
@@ -298,7 +295,6 @@
   
     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 @@
     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;
diff --git a/lib/csharp/src/Protocol/TBinaryProtocol.cs b/lib/csharp/src/Protocol/TBinaryProtocol.cs
index e16b837..4bbd9ad 100644
--- a/lib/csharp/src/Protocol/TBinaryProtocol.cs
+++ b/lib/csharp/src/Protocol/TBinaryProtocol.cs
@@ -35,10 +35,6 @@
 		protected bool strictRead_ = false;
 		protected bool strictWrite_ = true;
 
-		protected int readLength_;
-		protected bool checkReadLength_ = false;
-
-
 		#region BinaryProtocol Factory
 		 /**
 		  * Factory
@@ -375,35 +371,15 @@
 #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 @@
 
 		private int ReadAll(byte[] buf, int off, int len)
 		{
-			CheckReadLength(len);
 			return trans.ReadAll(buf, off, len);
 		}
 
diff --git a/lib/delphi/src/Thrift.Protocol.pas b/lib/delphi/src/Thrift.Protocol.pas
index b08458a..1f27203 100644
--- a/lib/delphi/src/Thrift.Protocol.pas
+++ b/lib/delphi/src/Thrift.Protocol.pas
@@ -370,13 +370,11 @@
   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 @@
     function ReadDouble:Double; override;

     function ReadBinary: TBytes; override;

 

-    procedure SetReadLength( readLength: Integer );

   end;

 

 

@@ -859,18 +856,6 @@
   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 @@
 function TBinaryProtocolImpl.ReadAll( var buf: TBytes; off,

   len: Integer): Integer;

 begin

-  CheckReadLength( len );

   Result := FTrans.ReadAll( buf, off, len );

 end;

 

@@ -892,7 +876,6 @@
   buf : TBytes;

 begin

   size := ReadI32;

-  CheckReadLength( size );

   SetLength( buf, size );

   FTrans.ReadAll( buf, 0, size);

   Result := buf;

@@ -1063,7 +1046,6 @@
 var

   buf : TBytes;

 begin

-  CheckReadLength( size );

   SetLength( buf, size );

   FTrans.ReadAll( buf, 0, size );

   Result := TEncoding.UTF8.GetString( buf);

@@ -1080,12 +1062,6 @@
 

 end;

 

-procedure TBinaryProtocolImpl.SetReadLength(readLength: Integer);

-begin

-  FReadLength := readLength;

-  FCheckReadLength := True;

-end;

-

 procedure TBinaryProtocolImpl.WriteBinary( const b: TBytes);

 var iLen : Integer;

 begin

diff --git a/lib/go/thrift/binary_protocol.go b/lib/go/thrift/binary_protocol.go
index 6fb8624..b57b528 100644
--- a/lib/go/thrift/binary_protocol.go
+++ b/lib/go/thrift/binary_protocol.go
@@ -30,8 +30,6 @@
 	trans           TTransport
 	strictRead      bool
 	strictWrite     bool
-	readLength      int
-	checkReadLength bool
 	buffer          [8]byte
 }
 
@@ -45,8 +43,7 @@
 }
 
 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 @@
 		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) 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)
diff --git a/lib/java/src/org/apache/thrift/protocol/TBinaryProtocol.java b/lib/java/src/org/apache/thrift/protocol/TBinaryProtocol.java
index 0c20fa9..32a761f 100644
--- a/lib/java/src/org/apache/thrift/protocol/TBinaryProtocol.java
+++ b/lib/java/src/org/apache/thrift/protocol/TBinaryProtocol.java
@@ -38,37 +38,24 @@
   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 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 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 @@
   }
 
   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);
-      }
-    }
-  }
-
 }
diff --git a/lib/javame/src/org/apache/thrift/protocol/TBinaryProtocol.java b/lib/javame/src/org/apache/thrift/protocol/TBinaryProtocol.java
index 1d16889..f6fe765 100644
--- a/lib/javame/src/org/apache/thrift/protocol/TBinaryProtocol.java
+++ b/lib/javame/src/org/apache/thrift/protocol/TBinaryProtocol.java
@@ -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 @@
   protected boolean strictRead_ = false;
   protected boolean strictWrite_ = true;
 
-  protected int readLength_;
-  protected boolean checkReadLength_ = false;
-
   /**
    * Factory
    */
@@ -311,7 +308,6 @@
 
   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 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);
-      }
-    }
-  }
-
 }