]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/lib/java/src/org/apache/thrift/transport/TSimpleFileTransport.java
buildsys: switch source download to quincy
[ceph.git] / ceph / src / jaegertracing / thrift / lib / java / src / org / apache / thrift / transport / TSimpleFileTransport.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 package org.apache.thrift.transport;
20
21 import java.io.IOException;
22 import java.io.RandomAccessFile;
23
24
25 /**
26 * Basic file support for the TTransport interface
27 */
28 public final class TSimpleFileTransport extends TTransport {
29
30 private RandomAccessFile file = null;
31 private boolean readable;
32 private boolean writable;
33 private String path_;
34
35
36 /**
37 * Create a transport backed by a simple file
38 *
39 * @param path the path to the file to open/create
40 * @param read true to support read operations
41 * @param write true to support write operations
42 * @param openFile true to open the file on construction
43 * @throws TTransportException if file open fails
44 */
45 public TSimpleFileTransport(String path, boolean read,
46 boolean write, boolean openFile)
47 throws TTransportException {
48 if (path.length() <= 0) {
49 throw new TTransportException("No path specified");
50 }
51 if (!read && !write) {
52 throw new TTransportException("Neither READ nor WRITE specified");
53 }
54 readable = read;
55 writable = write;
56 path_ = path;
57 if (openFile) {
58 open();
59 }
60 }
61
62 /**
63 * Create a transport backed by a simple file
64 * Implicitly opens file to conform to C++ behavior.
65 *
66 * @param path the path to the file to open/create
67 * @param read true to support read operations
68 * @param write true to support write operations
69 * @throws TTransportException if file open fails
70 */
71 public TSimpleFileTransport(String path, boolean read, boolean write)
72 throws TTransportException {
73 this(path, read, write, true);
74 }
75
76 /**
77 * Create a transport backed by a simple read only disk file (implicitly opens
78 * file)
79 *
80 * @param path the path to the file to open/create
81 * @throws TTransportException if file open fails
82 */
83 public TSimpleFileTransport(String path) throws TTransportException {
84 this(path, true, false, true);
85 }
86
87 /**
88 * Test file status
89 *
90 * @return true if open, otherwise false
91 */
92 @Override
93 public boolean isOpen() {
94 return (file != null);
95 }
96
97 /**
98 * Open file if not previously opened.
99 *
100 * @throws TTransportException if open fails
101 */
102 @Override
103 public void open() throws TTransportException {
104 if (file == null){
105 try {
106 String access = "r"; //RandomAccessFile objects must be readable
107 if (writable) {
108 access += "w";
109 }
110 file = new RandomAccessFile(path_, access);
111 } catch (IOException ioe) {
112 file = null;
113 throw new TTransportException(ioe.getMessage());
114 }
115 }
116 }
117
118 /**
119 * Close file, subsequent read/write activity will throw exceptions
120 */
121 @Override
122 public void close() {
123 if (file != null) {
124 try {
125 file.close();
126 } catch (Exception e) {
127 //Nothing to do
128 }
129 file = null;
130 }
131 }
132
133 /**
134 * Read up to len many bytes into buf at offset
135 *
136 * @param buf houses bytes read
137 * @param off offset into buff to begin writing to
138 * @param len maximum number of bytes to read
139 * @return number of bytes actually read
140 * @throws TTransportException on read failure
141 */
142 @Override
143 public int read(byte[] buf, int off, int len) throws TTransportException {
144 if (!readable) {
145 throw new TTransportException("Read operation on write only file");
146 }
147 int iBytesRead = 0;
148 try {
149 iBytesRead = file.read(buf, off, len);
150 } catch (IOException ioe) {
151 file = null;
152 throw new TTransportException(ioe.getMessage());
153 }
154 return iBytesRead;
155 }
156
157 /**
158 * Write len many bytes from buff starting at offset
159 *
160 * @param buf buffer containing bytes to write
161 * @param off offset into buffer to begin writing from
162 * @param len number of bytes to write
163 * @throws TTransportException on write failure
164 */
165 @Override
166 public void write(byte[] buf, int off, int len) throws TTransportException {
167 try {
168 file.write(buf, off, len);
169 } catch (IOException ioe) {
170 file = null;
171 throw new TTransportException(ioe.getMessage());
172 }
173 }
174
175 /**
176 * Move file pointer to specified offset, new read/write calls will act here
177 *
178 * @param offset bytes from beginning of file to move pointer to
179 * @throws TTransportException is seek fails
180 */
181 public void seek(long offset) throws TTransportException {
182 try {
183 file.seek(offset);
184 } catch (IOException ex) {
185 throw new TTransportException(ex.getMessage());
186 }
187 }
188
189 /**
190 * Return the length of the file in bytes
191 *
192 * @return length of the file in bytes
193 * @throws TTransportException if file access fails
194 */
195 public long length() throws TTransportException {
196 try {
197 return file.length();
198 } catch (IOException ex) {
199 throw new TTransportException(ex.getMessage());
200 }
201 }
202
203 /**
204 * Return current file pointer position in bytes from beginning of file
205 *
206 * @return file pointer position
207 * @throws TTransportException if file access fails
208 */
209 public long getFilePointer() throws TTransportException {
210 try {
211 return file.getFilePointer();
212 } catch (IOException ex) {
213 throw new TTransportException(ex.getMessage());
214 }
215 }
216 }