]>
git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/lib/java/src/org/apache/thrift/transport/TSimpleFileTransport.java
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
19 package org
.apache
.thrift
.transport
;
21 import java
.io
.IOException
;
22 import java
.io
.RandomAccessFile
;
26 * Basic file support for the TTransport interface
28 public final class TSimpleFileTransport
extends TTransport
{
30 private RandomAccessFile file
= null;
31 private boolean readable
;
32 private boolean writable
;
37 * Create a transport backed by a simple file
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
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");
51 if (!read
&& !write
) {
52 throw new TTransportException("Neither READ nor WRITE specified");
63 * Create a transport backed by a simple file
64 * Implicitly opens file to conform to C++ behavior.
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
71 public TSimpleFileTransport(String path
, boolean read
, boolean write
)
72 throws TTransportException
{
73 this(path
, read
, write
, true);
77 * Create a transport backed by a simple read only disk file (implicitly opens
80 * @param path the path to the file to open/create
81 * @throws TTransportException if file open fails
83 public TSimpleFileTransport(String path
) throws TTransportException
{
84 this(path
, true, false, true);
90 * @return true if open, otherwise false
93 public boolean isOpen() {
94 return (file
!= null);
98 * Open file if not previously opened.
100 * @throws TTransportException if open fails
103 public void open() throws TTransportException
{
106 String access
= "r"; //RandomAccessFile objects must be readable
110 file
= new RandomAccessFile(path_
, access
);
111 } catch (IOException ioe
) {
113 throw new TTransportException(ioe
.getMessage());
119 * Close file, subsequent read/write activity will throw exceptions
122 public void close() {
126 } catch (Exception e
) {
134 * Read up to len many bytes into buf at offset
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
143 public int read(byte[] buf
, int off
, int len
) throws TTransportException
{
145 throw new TTransportException("Read operation on write only file");
149 iBytesRead
= file
.read(buf
, off
, len
);
150 } catch (IOException ioe
) {
152 throw new TTransportException(ioe
.getMessage());
158 * Write len many bytes from buff starting at offset
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
166 public void write(byte[] buf
, int off
, int len
) throws TTransportException
{
168 file
.write(buf
, off
, len
);
169 } catch (IOException ioe
) {
171 throw new TTransportException(ioe
.getMessage());
176 * Move file pointer to specified offset, new read/write calls will act here
178 * @param offset bytes from beginning of file to move pointer to
179 * @throws TTransportException is seek fails
181 public void seek(long offset
) throws TTransportException
{
184 } catch (IOException ex
) {
185 throw new TTransportException(ex
.getMessage());
190 * Return the length of the file in bytes
192 * @return length of the file in bytes
193 * @throws TTransportException if file access fails
195 public long length() throws TTransportException
{
197 return file
.length();
198 } catch (IOException ex
) {
199 throw new TTransportException(ex
.getMessage());
204 * Return current file pointer position in bytes from beginning of file
206 * @return file pointer position
207 * @throws TTransportException if file access fails
209 public long getFilePointer() throws TTransportException
{
211 return file
.getFilePointer();
212 } catch (IOException ex
) {
213 throw new TTransportException(ex
.getMessage());