2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
20 package org
.apache
.thrift
.transport
;
22 import java
.io
.ByteArrayInputStream
;
24 import org
.apache
.thrift
.TByteArrayOutputStream
;
27 * Socket implementation of the TTransport interface. To be commented soon!
30 public class TFramedTransport
extends TTransport
{
33 * Underlying transport
35 private TTransport transport_
= null;
40 private final TByteArrayOutputStream writeBuffer_
=
41 new TByteArrayOutputStream(1024);
46 private ByteArrayInputStream readBuffer_
= null;
48 public static class Factory
extends TTransportFactory
{
52 public TTransport
getTransport(TTransport base
) {
53 return new TFramedTransport(base
);
58 * Constructor wraps around another tranpsort
60 public TFramedTransport(TTransport transport
) {
61 transport_
= transport
;
64 public void open() throws TTransportException
{
68 public boolean isOpen() {
69 return transport_
.isOpen();
76 public int read(byte[] buf
, int off
, int len
) throws TTransportException
{
77 if (readBuffer_
!= null) {
78 int got
= readBuffer_
.read(buf
, off
, len
);
84 // Read another frame of data
87 return readBuffer_
.read(buf
, off
, len
);
90 private void readFrame() throws TTransportException
{
91 byte[] i32rd
= new byte[4];
92 transport_
.readAll(i32rd
, 0, 4);
94 ((i32rd
[0] & 0xff) << 24) |
95 ((i32rd
[1] & 0xff) << 16) |
96 ((i32rd
[2] & 0xff) << 8) |
99 byte[] buff
= new byte[size
];
100 transport_
.readAll(buff
, 0, size
);
101 readBuffer_
= new ByteArrayInputStream(buff
);
104 public void write(byte[] buf
, int off
, int len
) throws TTransportException
{
105 writeBuffer_
.write(buf
, off
, len
);
108 public void flush() throws TTransportException
{
109 byte[] buf
= writeBuffer_
.get();
110 int len
= writeBuffer_
.len();
111 writeBuffer_
.reset();
113 byte[] i32out
= new byte[4];
114 i32out
[0] = (byte)(0xff & (len
>> 24));
115 i32out
[1] = (byte)(0xff & (len
>> 16));
116 i32out
[2] = (byte)(0xff & (len
>> 8));
117 i32out
[3] = (byte)(0xff & (len
));
118 transport_
.write(i32out
, 0, 4);
119 transport_
.write(buf
, 0, len
);