THRIFT-820 Remove readLength attribute from BinaryProtocol
authorCarl Yeksigian <carl@apache.org>
Wed, 14 Aug 2013 23:37:54 +0000 (19:37 -0400)
committerCarl Yeksigian <carl@apache.org>
Wed, 14 Aug 2013 23:38:42 +0000 (19:38 -0400)
Patch: Carl Yeksigian

lib/as3/src/org/apache/thrift/protocol/TBinaryProtocol.as
lib/csharp/src/Protocol/TBinaryProtocol.cs
lib/delphi/src/Thrift.Protocol.pas
lib/go/thrift/binary_protocol.go
lib/java/src/org/apache/thrift/protocol/TBinaryProtocol.java
lib/javame/src/org/apache/thrift/protocol/TBinaryProtocol.java

index 4412479..b2ff9d8 100644 (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;
index e16b837..4bbd9ad 100644 (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);
                }
 
index b08458a..1f27203 100644 (file)
@@ -370,13 +370,11 @@ type
   protected\r
     FStrictRead : Boolean;\r
     FStrictWrite : Boolean;\r
-    FReadLength : Integer;\r
-    FCheckReadLength : Boolean;\r
 \r
   private\r
     function ReadAll( var buf: TBytes; off: Integer; len: Integer ): Integer;\r
     function ReadStringBody( size: Integer): string;\r
-    procedure CheckReadLength( len: Integer );\r
+\r
   public\r
 \r
     type\r
@@ -434,7 +432,6 @@ type
     function ReadDouble:Double; override;\r
     function ReadBinary: TBytes; override;\r
 \r
-    procedure SetReadLength( readLength: Integer );\r
   end;\r
 \r
 \r
@@ -859,18 +856,6 @@ begin
   Create( trans, False, True);\r
 end;\r
 \r
-procedure TBinaryProtocolImpl.CheckReadLength(len: Integer);\r
-begin\r
-  if FCheckReadLength then\r
-  begin\r
-    Dec( FReadLength, len);\r
-    if FReadLength < 0 then\r
-    begin\r
-      raise Exception.Create( 'Message length exceeded: ' + IntToStr( len ) );\r
-    end;\r
-  end;\r
-end;\r
-\r
 constructor TBinaryProtocolImpl.Create( const trans: ITransport; strictRead,\r
   strictWrite: Boolean);\r
 begin\r
@@ -882,7 +867,6 @@ end;
 function TBinaryProtocolImpl.ReadAll( var buf: TBytes; off,\r
   len: Integer): Integer;\r
 begin\r
-  CheckReadLength( len );\r
   Result := FTrans.ReadAll( buf, off, len );\r
 end;\r
 \r
@@ -892,7 +876,6 @@ var
   buf : TBytes;\r
 begin\r
   size := ReadI32;\r
-  CheckReadLength( size );\r
   SetLength( buf, size );\r
   FTrans.ReadAll( buf, 0, size);\r
   Result := buf;\r
@@ -1063,7 +1046,6 @@ function TBinaryProtocolImpl.ReadStringBody( size: Integer): string;
 var\r
   buf : TBytes;\r
 begin\r
-  CheckReadLength( size );\r
   SetLength( buf, size );\r
   FTrans.ReadAll( buf, 0, size );\r
   Result := TEncoding.UTF8.GetString( buf);\r
@@ -1080,12 +1062,6 @@ begin
 \r
 end;\r
 \r
-procedure TBinaryProtocolImpl.SetReadLength(readLength: Integer);\r
-begin\r
-  FReadLength := readLength;\r
-  FCheckReadLength := True;\r
-end;\r
-\r
 procedure TBinaryProtocolImpl.WriteBinary( const b: TBytes);\r
 var iLen : Integer;\r
 begin\r
index 6fb8624..b57b528 100644 (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)
index 0c20fa9..32a761f 100644 (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);
-      }
-    }
-  }
-
 }
index 1d16889..f6fe765 100644 (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);
-      }
-    }
-  }
-
 }