]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/lib/java/src/org/apache/thrift/transport/TIOStreamTransport.java
buildsys: switch source download to quincy
[ceph.git] / ceph / src / jaegertracing / thrift / lib / java / src / org / apache / thrift / transport / TIOStreamTransport.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 org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.OutputStream;
28
29 /**
30 * This is the most commonly used base transport. It takes an InputStream
31 * and an OutputStream and uses those to perform all transport operations.
32 * This allows for compatibility with all the nice constructs Java already
33 * has to provide a variety of types of streams.
34 *
35 */
36 public class TIOStreamTransport extends TTransport {
37
38 private static final Logger LOGGER = LoggerFactory.getLogger(TIOStreamTransport.class.getName());
39
40 /** Underlying inputStream */
41 protected InputStream inputStream_ = null;
42
43 /** Underlying outputStream */
44 protected OutputStream outputStream_ = null;
45
46 /**
47 * Subclasses can invoke the default constructor and then assign the input
48 * streams in the open method.
49 */
50 protected TIOStreamTransport() {}
51
52 /**
53 * Input stream constructor.
54 *
55 * @param is Input stream to read from
56 */
57 public TIOStreamTransport(InputStream is) {
58 inputStream_ = is;
59 }
60
61 /**
62 * Output stream constructor.
63 *
64 * @param os Output stream to read from
65 */
66 public TIOStreamTransport(OutputStream os) {
67 outputStream_ = os;
68 }
69
70 /**
71 * Two-way stream constructor.
72 *
73 * @param is Input stream to read from
74 * @param os Output stream to read from
75 */
76 public TIOStreamTransport(InputStream is, OutputStream os) {
77 inputStream_ = is;
78 outputStream_ = os;
79 }
80
81 /**
82 *
83 * @return false after close is called.
84 */
85 public boolean isOpen() {
86 return inputStream_ != null && outputStream_ != null;
87 }
88
89 /**
90 * The streams must already be open. This method does nothing.
91 */
92 public void open() throws TTransportException {}
93
94 /**
95 * Closes both the input and output streams.
96 */
97 public void close() {
98 if (inputStream_ != null) {
99 try {
100 inputStream_.close();
101 } catch (IOException iox) {
102 LOGGER.warn("Error closing input stream.", iox);
103 }
104 inputStream_ = null;
105 }
106 if (outputStream_ != null) {
107 try {
108 outputStream_.close();
109 } catch (IOException iox) {
110 LOGGER.warn("Error closing output stream.", iox);
111 }
112 outputStream_ = null;
113 }
114 }
115
116 /**
117 * Reads from the underlying input stream if not null.
118 */
119 public int read(byte[] buf, int off, int len) throws TTransportException {
120 if (inputStream_ == null) {
121 throw new TTransportException(TTransportException.NOT_OPEN, "Cannot read from null inputStream");
122 }
123 int bytesRead;
124 try {
125 bytesRead = inputStream_.read(buf, off, len);
126 } catch (IOException iox) {
127 throw new TTransportException(TTransportException.UNKNOWN, iox);
128 }
129 if (bytesRead < 0) {
130 throw new TTransportException(TTransportException.END_OF_FILE, "Socket is closed by peer.");
131 }
132 return bytesRead;
133 }
134
135 /**
136 * Writes to the underlying output stream if not null.
137 */
138 public void write(byte[] buf, int off, int len) throws TTransportException {
139 if (outputStream_ == null) {
140 throw new TTransportException(TTransportException.NOT_OPEN, "Cannot write to null outputStream");
141 }
142 try {
143 outputStream_.write(buf, off, len);
144 } catch (IOException iox) {
145 throw new TTransportException(TTransportException.UNKNOWN, iox);
146 }
147 }
148
149 /**
150 * Flushes the underlying output stream if not null.
151 */
152 public void flush() throws TTransportException {
153 if (outputStream_ == null) {
154 throw new TTransportException(TTransportException.NOT_OPEN, "Cannot flush null outputStream");
155 }
156 try {
157 outputStream_.flush();
158 } catch (IOException iox) {
159 throw new TTransportException(TTransportException.UNKNOWN, iox);
160 }
161 }
162 }