]>
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 | /** | |
23 | * Generic class that encapsulates the I/O layer. This is basically a thin | |
24 | * wrapper around the combined functionality of Java input/output streams. | |
25 | * | |
26 | */ | |
27 | public abstract class TTransport { | |
28 | ||
29 | /** | |
30 | * Queries whether the transport is open. | |
31 | * | |
32 | * @return True if the transport is open. | |
33 | */ | |
34 | public abstract boolean isOpen(); | |
35 | ||
36 | /** | |
37 | * Is there more data to be read? | |
38 | * | |
39 | * @return True if the remote side is still alive and feeding us | |
40 | */ | |
41 | public boolean peek() { | |
42 | return isOpen(); | |
43 | } | |
44 | ||
45 | /** | |
46 | * Opens the transport for reading/writing. | |
47 | * | |
48 | * @throws TTransportException if the transport could not be opened | |
49 | */ | |
50 | public abstract void open() | |
51 | throws TTransportException; | |
52 | ||
53 | /** | |
54 | * Closes the transport. | |
55 | */ | |
56 | public abstract void close(); | |
57 | ||
58 | /** | |
59 | * Reads up to len bytes into buffer buf, starting att offset off. | |
60 | * | |
61 | * @param buf Array to read into | |
62 | * @param off Index to start reading at | |
63 | * @param len Maximum number of bytes to read | |
64 | * @return The number of bytes actually read | |
65 | * @throws TTransportException if there was an error reading data | |
66 | */ | |
67 | public abstract int read(byte[] buf, int off, int len) | |
68 | throws TTransportException; | |
69 | ||
70 | /** | |
71 | * Guarantees that all of len bytes are actually read off the transport. | |
72 | * | |
73 | * @param buf Array to read into | |
74 | * @param off Index to start reading at | |
75 | * @param len Maximum number of bytes to read | |
76 | * @return The number of bytes actually read, which must be equal to len | |
77 | * @throws TTransportException if there was an error reading data | |
78 | */ | |
79 | public int readAll(byte[] buf, int off, int len) | |
80 | throws TTransportException { | |
81 | int got = 0; | |
82 | int ret = 0; | |
83 | while (got < len) { | |
84 | ret = read(buf, off+got, len-got); | |
85 | if (ret <= 0) { | |
86 | throw new TTransportException("Cannot read. Remote side has closed. Tried to read " + len + " bytes, but only got " + got + " bytes."); | |
87 | } | |
88 | got += ret; | |
89 | } | |
90 | return got; | |
91 | } | |
92 | ||
93 | /** | |
94 | * Writes the buffer to the output | |
95 | * | |
96 | * @param buf The output data buffer | |
97 | * @throws TTransportException if an error occurs writing data | |
98 | */ | |
99 | public void write(byte[] buf) throws TTransportException { | |
100 | write(buf, 0, buf.length); | |
101 | } | |
102 | ||
103 | /** | |
104 | * Writes up to len bytes from the buffer. | |
105 | * | |
106 | * @param buf The output data buffer | |
107 | * @param off The offset to start writing from | |
108 | * @param len The number of bytes to write | |
109 | * @throws TTransportException if there was an error writing data | |
110 | */ | |
111 | public abstract void write(byte[] buf, int off, int len) | |
112 | throws TTransportException; | |
113 | ||
114 | /** | |
115 | * Flush any pending data out of a transport buffer. | |
116 | * | |
117 | * @throws TTransportException if there was an error writing out data. | |
118 | */ | |
119 | public void flush() | |
120 | throws TTransportException {} | |
121 | } |