]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
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.Closeable; | |
23 | ||
24 | /** | |
25 | * Generic class that encapsulates the I/O layer. This is basically a thin | |
26 | * wrapper around the combined functionality of Java input/output streams. | |
27 | * | |
28 | */ | |
29 | public abstract class TTransport implements Closeable { | |
30 | ||
31 | /** | |
32 | * Queries whether the transport is open. | |
33 | * | |
34 | * @return True if the transport is open. | |
35 | */ | |
36 | public abstract boolean isOpen(); | |
37 | ||
38 | /** | |
39 | * Is there more data to be read? | |
40 | * | |
41 | * @return True if the remote side is still alive and feeding us | |
42 | */ | |
43 | public boolean peek() { | |
44 | return isOpen(); | |
45 | } | |
46 | ||
47 | /** | |
48 | * Opens the transport for reading/writing. | |
49 | * | |
50 | * @throws TTransportException if the transport could not be opened | |
51 | */ | |
52 | public abstract void open() | |
53 | throws TTransportException; | |
54 | ||
55 | /** | |
56 | * Closes the transport. | |
57 | */ | |
58 | public abstract void close(); | |
59 | ||
60 | /** | |
61 | * Reads up to len bytes into buffer buf, starting at offset off. | |
62 | * | |
63 | * @param buf Array to read into | |
64 | * @param off Index to start reading at | |
65 | * @param len Maximum number of bytes to read | |
66 | * @return The number of bytes actually read | |
67 | * @throws TTransportException if there was an error reading data | |
68 | */ | |
69 | public abstract int read(byte[] buf, int off, int len) | |
70 | throws TTransportException; | |
71 | ||
72 | /** | |
73 | * Guarantees that all of len bytes are actually read off the transport. | |
74 | * | |
75 | * @param buf Array to read into | |
76 | * @param off Index to start reading at | |
77 | * @param len Maximum number of bytes to read | |
78 | * @return The number of bytes actually read, which must be equal to len | |
79 | * @throws TTransportException if there was an error reading data | |
80 | */ | |
81 | public int readAll(byte[] buf, int off, int len) | |
82 | throws TTransportException { | |
83 | int got = 0; | |
84 | int ret = 0; | |
85 | while (got < len) { | |
86 | ret = read(buf, off+got, len-got); | |
87 | if (ret <= 0) { | |
88 | throw new TTransportException( | |
89 | "Cannot read. Remote side has closed. Tried to read " | |
90 | + len | |
91 | + " bytes, but only got " | |
92 | + got | |
93 | + " bytes. (This is often indicative of an internal error on the server side. Please check your server logs.)"); | |
94 | } | |
95 | got += ret; | |
96 | } | |
97 | return got; | |
98 | } | |
99 | ||
100 | /** | |
101 | * Writes the buffer to the output | |
102 | * | |
103 | * @param buf The output data buffer | |
104 | * @throws TTransportException if an error occurs writing data | |
105 | */ | |
106 | public void write(byte[] buf) throws TTransportException { | |
107 | write(buf, 0, buf.length); | |
108 | } | |
109 | ||
110 | /** | |
111 | * Writes up to len bytes from the buffer. | |
112 | * | |
113 | * @param buf The output data buffer | |
114 | * @param off The offset to start writing from | |
115 | * @param len The number of bytes to write | |
116 | * @throws TTransportException if there was an error writing data | |
117 | */ | |
118 | public abstract void write(byte[] buf, int off, int len) | |
119 | throws TTransportException; | |
120 | ||
121 | /** | |
122 | * Flush any pending data out of a transport buffer. | |
123 | * | |
124 | * @throws TTransportException if there was an error writing out data. | |
125 | */ | |
126 | public void flush() | |
127 | throws TTransportException {} | |
128 | ||
129 | /** | |
130 | * Access the protocol's underlying buffer directly. If this is not a | |
131 | * buffered transport, return null. | |
132 | * @return protocol's Underlying buffer | |
133 | */ | |
134 | public byte[] getBuffer() { | |
135 | return null; | |
136 | } | |
137 | ||
138 | /** | |
139 | * Return the index within the underlying buffer that specifies the next spot | |
140 | * that should be read from. | |
141 | * @return index within the underlying buffer that specifies the next spot | |
142 | * that should be read from | |
143 | */ | |
144 | public int getBufferPosition() { | |
145 | return 0; | |
146 | } | |
147 | ||
148 | /** | |
149 | * Get the number of bytes remaining in the underlying buffer. Returns -1 if | |
150 | * this is a non-buffered transport. | |
151 | * @return the number of bytes remaining in the underlying buffer. <br> Returns -1 if | |
152 | * this is a non-buffered transport. | |
153 | */ | |
154 | public int getBytesRemainingInBuffer() { | |
155 | return -1; | |
156 | } | |
157 | ||
158 | /** | |
159 | * Consume len bytes from the underlying buffer. | |
160 | * @param len | |
161 | */ | |
162 | public void consumeBuffer(int len) {} | |
163 | } |