]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/lib/javame/src/org/apache/thrift/transport/TFramedTransport.java
buildsys: switch source download to quincy
[ceph.git] / ceph / src / jaegertracing / thrift / lib / javame / src / org / apache / thrift / transport / TFramedTransport.java
1 /*
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
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
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
17 * under the License.
18 */
19
20 package org.apache.thrift.transport;
21
22 import java.io.ByteArrayInputStream;
23
24 import org.apache.thrift.TByteArrayOutputStream;
25
26 /**
27 * Socket implementation of the TTransport interface. To be commented soon!
28 *
29 */
30 public class TFramedTransport extends TTransport {
31
32 /**
33 * Underlying transport
34 */
35 private TTransport transport_ = null;
36
37 /**
38 * Buffer for output
39 */
40 private final TByteArrayOutputStream writeBuffer_ =
41 new TByteArrayOutputStream(1024);
42
43 /**
44 * Buffer for input
45 */
46 private ByteArrayInputStream readBuffer_ = null;
47
48 public static class Factory extends TTransportFactory {
49 public Factory() {
50 }
51
52 public TTransport getTransport(TTransport base) {
53 return new TFramedTransport(base);
54 }
55 }
56
57 /**
58 * Constructor wraps around another tranpsort
59 */
60 public TFramedTransport(TTransport transport) {
61 transport_ = transport;
62 }
63
64 public void open() throws TTransportException {
65 transport_.open();
66 }
67
68 public boolean isOpen() {
69 return transport_.isOpen();
70 }
71
72 public void close() {
73 transport_.close();
74 }
75
76 public int read(byte[] buf, int off, int len) throws TTransportException {
77 if (readBuffer_ != null) {
78 int got = readBuffer_.read(buf, off, len);
79 if (got > 0) {
80 return got;
81 }
82 }
83
84 // Read another frame of data
85 readFrame();
86
87 return readBuffer_.read(buf, off, len);
88 }
89
90 private void readFrame() throws TTransportException {
91 byte[] i32rd = new byte[4];
92 transport_.readAll(i32rd, 0, 4);
93 int size =
94 ((i32rd[0] & 0xff) << 24) |
95 ((i32rd[1] & 0xff) << 16) |
96 ((i32rd[2] & 0xff) << 8) |
97 ((i32rd[3] & 0xff));
98
99 byte[] buff = new byte[size];
100 transport_.readAll(buff, 0, size);
101 readBuffer_ = new ByteArrayInputStream(buff);
102 }
103
104 public void write(byte[] buf, int off, int len) throws TTransportException {
105 writeBuffer_.write(buf, off, len);
106 }
107
108 public void flush() throws TTransportException {
109 byte[] buf = writeBuffer_.get();
110 int len = writeBuffer_.len();
111 writeBuffer_.reset();
112
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);
120 transport_.flush();
121 }
122 }