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 org
.slf4j
.Logger
;
23 import org
.slf4j
.LoggerFactory
;
25 import java
.io
.IOException
;
26 import java
.io
.InputStream
;
27 import java
.io
.OutputStream
;
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.
36 public class TIOStreamTransport
extends TTransport
{
38 private static final Logger LOGGER
= LoggerFactory
.getLogger(TIOStreamTransport
.class.getName());
40 /** Underlying inputStream */
41 protected InputStream inputStream_
= null;
43 /** Underlying outputStream */
44 protected OutputStream outputStream_
= null;
47 * Subclasses can invoke the default constructor and then assign the input
48 * streams in the open method.
50 protected TIOStreamTransport() {}
53 * Input stream constructor.
55 * @param is Input stream to read from
57 public TIOStreamTransport(InputStream is
) {
62 * Output stream constructor.
64 * @param os Output stream to read from
66 public TIOStreamTransport(OutputStream os
) {
71 * Two-way stream constructor.
73 * @param is Input stream to read from
74 * @param os Output stream to read from
76 public TIOStreamTransport(InputStream is
, OutputStream os
) {
83 * @return false after close is called.
85 public boolean isOpen() {
86 return inputStream_
!= null && outputStream_
!= null;
90 * The streams must already be open. This method does nothing.
92 public void open() throws TTransportException
{}
95 * Closes both the input and output streams.
98 if (inputStream_
!= null) {
100 inputStream_
.close();
101 } catch (IOException iox
) {
102 LOGGER
.warn("Error closing input stream.", iox
);
106 if (outputStream_
!= null) {
108 outputStream_
.close();
109 } catch (IOException iox
) {
110 LOGGER
.warn("Error closing output stream.", iox
);
112 outputStream_
= null;
117 * Reads from the underlying input stream if not null.
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");
125 bytesRead
= inputStream_
.read(buf
, off
, len
);
126 } catch (IOException iox
) {
127 throw new TTransportException(TTransportException
.UNKNOWN
, iox
);
130 throw new TTransportException(TTransportException
.END_OF_FILE
, "Socket is closed by peer.");
136 * Writes to the underlying output stream if not null.
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");
143 outputStream_
.write(buf
, off
, len
);
144 } catch (IOException iox
) {
145 throw new TTransportException(TTransportException
.UNKNOWN
, iox
);
150 * Flushes the underlying output stream if not null.
152 public void flush() throws TTransportException
{
153 if (outputStream_
== null) {
154 throw new TTransportException(TTransportException
.NOT_OPEN
, "Cannot flush null outputStream");
157 outputStream_
.flush();
158 } catch (IOException iox
) {
159 throw new TTransportException(TTransportException
.UNKNOWN
, iox
);