New protocol wrapping transport model for Thrift Java


git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@664846 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/java/src/protocol/TBinaryProtocol.java b/lib/java/src/protocol/TBinaryProtocol.java
index 61bac08..b6431bf 100644
--- a/lib/java/src/protocol/TBinaryProtocol.java
+++ b/lib/java/src/protocol/TBinaryProtocol.java
@@ -8,82 +8,99 @@
  *
  * @author Mark Slee <mcslee@facebook.com>
  */
-public class TBinaryProtocol implements TProtocol {
+public class TBinaryProtocol extends TProtocol {
 
-  public void writeMessageBegin(TTransport out, TMessage message) throws TException {
-    writeString(out, message.name);
-    writeByte(out, message.type);
-    writeI32(out, message.seqid);
+  /**
+   * Factory
+   */
+  public static class Factory implements TProtocolFactory {
+    public TProtocol[] getIOProtocols(TTransport in, TTransport out) {
+      TProtocol[] io = new TProtocol[2];
+      io[0] = io[1] = new TBinaryProtocol(in, out);
+      return io;
+    }
   }
 
-  public void writeMessageEnd(TTransport out) throws TException {}
-
-
-  public void writeStructBegin(TTransport out, TStruct struct) throws TException {}
-
-  public void writeStructEnd(TTransport out) throws TException {}
-
-  public void writeFieldBegin(TTransport out, TField field) throws TException {
-    writeByte(out, field.type);
-    writeI16(out, field.id);
+  /**
+   * Constructor
+   */
+  public TBinaryProtocol(TTransport in, TTransport out) {
+    super(in, out);
   }
 
-  public void writeFieldEnd(TTransport out) throws TException {}
-
-  public void writeFieldStop(TTransport out) throws TException {
-    writeByte(out, TType.STOP);
+  public void writeMessageBegin(TMessage message) throws TException {
+    writeString(message.name);
+    writeByte(message.type);
+    writeI32(message.seqid);
   }
 
-  public void writeMapBegin(TTransport out, TMap map) throws TException {
-    writeByte(out, map.keyType);
-    writeByte(out, map.valueType);
-    writeI32(out, map.size);
+  public void writeMessageEnd() {}
+
+  public void writeStructBegin(TStruct struct) {}
+
+  public void writeStructEnd() {}
+
+  public void writeFieldBegin(TField field) throws TException {
+    writeByte(field.type);
+    writeI16(field.id);
   }
 
-  public void writeMapEnd(TTransport out) throws TException {}
+  public void writeFieldEnd() {}
 
-  public void writeListBegin(TTransport out, TList list) throws TException {
-    writeByte(out, list.elemType);
-    writeI32(out, list.size);
+  public void writeFieldStop() throws TException {
+    writeByte(TType.STOP);
   }
 
-  public void writeListEnd(TTransport out) throws TException {}
-
-  public void writeSetBegin(TTransport out, TSet set) throws TException {
-    writeByte(out, set.elemType);
-    writeI32(out, set.size);
+  public void writeMapBegin(TMap map) throws TException {
+    writeByte(map.keyType);
+    writeByte(map.valueType);
+    writeI32(map.size);
   }
 
-  public void writeSetEnd(TTransport out) throws TException {}
+  public void writeMapEnd() {}
 
-  public void writeBool(TTransport out, boolean b) throws TException {
-    writeByte(out, b ? (byte)1 : (byte)0);
+  public void writeListBegin(TList list) throws TException {
+    writeByte(list.elemType);
+    writeI32(list.size);
   }
 
-  public void writeByte(TTransport out, byte b) throws TException {
-    byte[] bout = new byte[1];
+  public void writeListEnd() {}
+
+  public void writeSetBegin(TSet set) throws TException {
+    writeByte(set.elemType);
+    writeI32(set.size);
+  }
+
+  public void writeSetEnd() {}
+
+  public void writeBool(boolean b) throws TException {
+    writeByte(b ? (byte)1 : (byte)0);
+  }
+
+  private byte [] bout = new byte[1];
+  public void writeByte(byte b) throws TException {
     bout[0] = b;
-    out.write(bout, 0, 1);
+    outputTransport_.write(bout, 0, 1);
   }
 
-  public void writeI16(TTransport out, short i16) throws TException {
-    byte[] i16out = new byte[2];
+  private byte[] i16out = new byte[2];
+  public void writeI16(short i16) throws TException {
     i16out[0] = (byte)(0xff & (i16 >> 8));
     i16out[1] = (byte)(0xff & (i16));
-    out.write(i16out, 0, 2);
+    outputTransport_.write(i16out, 0, 2);
   }
 
-  public void writeI32(TTransport out, int i32) throws TException {
-    byte[] i32out = new byte[4];
+  private byte[] i32out = new byte[4];
+  public void writeI32(int i32) throws TException {
     i32out[0] = (byte)(0xff & (i32 >> 24));
     i32out[1] = (byte)(0xff & (i32 >> 16));
     i32out[2] = (byte)(0xff & (i32 >> 8));
     i32out[3] = (byte)(0xff & (i32));
-    out.write(i32out, 0, 4);
+    outputTransport_.write(i32out, 0, 4);
   }
 
-  public void writeI64(TTransport out, long i64) throws TException {
-    byte[] i64out = new byte[8];
+  private byte[] i64out = new byte[8];
+  public void writeI64(long i64) throws TException {
     i64out[0] = (byte)(0xff & (i64 >> 56));
     i64out[1] = (byte)(0xff & (i64 >> 48));
     i64out[2] = (byte)(0xff & (i64 >> 40));
@@ -92,100 +109,100 @@
     i64out[5] = (byte)(0xff & (i64 >> 16));
     i64out[6] = (byte)(0xff & (i64 >> 8));
     i64out[7] = (byte)(0xff & (i64));
-    out.write(i64out, 0, 8);
+    outputTransport_.write(i64out, 0, 8);
   }
 
-  public void writeDouble(TTransport out, double dub) throws TException {
-    writeI64(out, Double.doubleToLongBits(dub));
+  public void writeDouble(double dub) throws TException {
+    writeI64(Double.doubleToLongBits(dub));
   }
 
-  public void writeString(TTransport out, String str) throws TException {
+  public void writeString(String str) throws TException {
     byte[] dat = str.getBytes();
-    writeI32(out, dat.length);
-    out.write(dat, 0, dat.length);
+    writeI32(dat.length);
+    outputTransport_.write(dat, 0, dat.length);
   }
 
   /**
    * Reading methods.
    */
 
-  public TMessage readMessageBegin(TTransport in) throws TException {
+  public TMessage readMessageBegin() throws TException {
     TMessage message = new TMessage();
-    message.name = readString(in);
-    message.type = readByte(in);
-    message.seqid = readI32(in);
+    message.name = readString();
+    message.type = readByte();
+    message.seqid = readI32();
     return message;
   }
 
-  public void readMessageEnd(TTransport in) throws TException {}
+  public void readMessageEnd() {}
 
-  public TStruct readStructBegin(TTransport in) throws TException {
+  public TStruct readStructBegin() {
     return new TStruct();
   }
 
-  public void readStructEnd(TTransport in) throws TException {}
+  public void readStructEnd() {}
 
-  public TField readFieldBegin(TTransport in) throws TException {
+  public TField readFieldBegin() throws TException {
     TField field = new TField();
-    field.type = readByte(in);
+    field.type = readByte();
     if (field.type != TType.STOP) {
-      field.id = readI16(in);
+      field.id = readI16();
     }
     return field;
   }
   
-  public void readFieldEnd(TTransport in) throws TException {}
+  public void readFieldEnd() {}
  
-  public TMap readMapBegin(TTransport in) throws TException {
+  public TMap readMapBegin() throws TException {
     TMap map = new TMap();
-    map.keyType = readByte(in);
-    map.valueType = readByte(in);
-    map.size = readI32(in);
+    map.keyType = readByte();
+    map.valueType = readByte();
+    map.size = readI32();
     return map;
   }
 
-  public void readMapEnd(TTransport in) throws TException {}
+  public void readMapEnd() {}
 
-  public TList readListBegin(TTransport in) throws TException {
+  public TList readListBegin() throws TException {
     TList list = new TList();
-    list.elemType = readByte(in);
-    list.size = readI32(in);
+    list.elemType = readByte();
+    list.size = readI32();
     return list;
   }
 
-  public void readListEnd(TTransport in) throws TException {}
+  public void readListEnd() {}
 
-  public TSet readSetBegin(TTransport in) throws TException {
+  public TSet readSetBegin() throws TException {
     TSet set = new TSet();
-    set.elemType = readByte(in);
-    set.size = readI32(in);
+    set.elemType = readByte();
+    set.size = readI32();
     return set;
   }
 
-  public void readSetEnd(TTransport in) throws TException {}
+  public void readSetEnd() {}
 
-  public boolean readBool(TTransport in) throws TException {
-    return (readByte(in) == 1);
+  public boolean readBool() throws TException {
+    return (readByte() == 1);
   }
 
-  public byte readByte(TTransport in) throws TException {
-    byte[] bin = new byte[1];
-    in.readAll(bin, 0, 1);
+  private byte[] bin = new byte[1];
+  public byte readByte() throws TException {
+    inputTransport_.readAll(bin, 0, 1);
     return bin[0];
   }
 
-  public short readI16(TTransport in) throws TException {
-    byte[] i16rd = new byte[2];
-    in.readAll(i16rd, 0, 2);
+  private byte[] i16rd = new byte[2];
+  public short readI16() throws TException {
+    inputTransport_.readAll(i16rd, 0, 2);
     return
       (short)
       (((i16rd[0] & 0xff) << 8) |
        ((i16rd[1] & 0xff)));
   }
 
-  public int readI32(TTransport in) throws TException {
-    byte[] i32rd = new byte[4];
-    in.readAll(i32rd, 0, 4);
+  private byte[] i32rd = new byte[4];
+  public int readI32() throws TException {
+    inputTransport_.readAll(i32rd, 0, 4);
     return
       ((i32rd[0] & 0xff) << 24) |
       ((i32rd[1] & 0xff) << 16) |
@@ -193,9 +210,9 @@
       ((i32rd[3] & 0xff));
   }
  
-  public long readI64(TTransport in) throws TException {
-    byte[] i64rd = new byte[8];
-    in.readAll(i64rd, 0, 8);
+  private byte[] i64rd = new byte[8];
+  public long readI64() throws TException {
+    inputTransport_.readAll(i64rd, 0, 8);
     return
       ((long)(i64rd[0] & 0xff) << 56) |
       ((long)(i64rd[1] & 0xff) << 48) |
@@ -207,14 +224,14 @@
       ((long)(i64rd[7] & 0xff));
   }
 
-  public double readDouble(TTransport in) throws TException {
-    return Double.longBitsToDouble(readI64(in));
+  public double readDouble() throws TException {
+    return Double.longBitsToDouble(readI64());
   }
 
-  public String readString(TTransport in)  throws TException {
-    int size = readI32(in);
+  public String readString() throws TException {
+    int size = readI32();
     byte[] buf = new byte[size];
-    in.readAll(buf, 0, size);
+    inputTransport_.readAll(buf, 0, size);
     return new String(buf);
   }
 }