1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
|
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.thrift.transport;
import java.io.IOException;
import java.io.RandomAccessFile;
import org.apache.thrift.TConfiguration;
/** Basic file support for the TTransport interface */
public final class TSimpleFileTransport extends TEndpointTransport {
private RandomAccessFile file = null;
private final boolean readable;
private final boolean writable;
private final String path_;
/**
* Create a transport backed by a simple file
*
* @param path the path to the file to open/create
* @param read true to support read operations
* @param write true to support write operations
* @param openFile true to open the file on construction
* @throws TTransportException if file open fails
*/
public TSimpleFileTransport(String path, boolean read, boolean write, boolean openFile)
throws TTransportException {
this(new TConfiguration(), path, read, write, openFile);
}
/**
* Create a transport backed by a simple file
*
* @param config
* @param path the path to the file to open/create
* @param read true to support read operations
* @param write true to support write operations
* @param openFile true to open the file on construction
* @throws TTransportException if file open fails
*/
public TSimpleFileTransport(
TConfiguration config, String path, boolean read, boolean write, boolean openFile)
throws TTransportException {
super(config);
if (path.length() <= 0) {
throw new TTransportException("No path specified");
}
if (!read && !write) {
throw new TTransportException("Neither READ nor WRITE specified");
}
readable = read;
writable = write;
path_ = path;
if (openFile) {
open();
}
}
/**
* Create a transport backed by a simple file Implicitly opens file to conform to C++ behavior.
*
* @param path the path to the file to open/create
* @param read true to support read operations
* @param write true to support write operations
* @throws TTransportException if file open fails
*/
public TSimpleFileTransport(String path, boolean read, boolean write) throws TTransportException {
this(path, read, write, true);
}
/**
* Create a transport backed by a simple read only disk file (implicitly opens file)
*
* @param path the path to the file to open/create
* @throws TTransportException if file open fails
*/
public TSimpleFileTransport(String path) throws TTransportException {
this(path, true, false, true);
}
/**
* Test file status
*
* @return true if open, otherwise false
*/
@Override
public boolean isOpen() {
return (file != null);
}
/**
* Open file if not previously opened.
*
* @throws TTransportException if open fails
*/
@Override
public void open() throws TTransportException {
if (file == null) {
try {
String access = "r"; // RandomAccessFile objects must be readable
if (writable) {
access += "w";
}
file = new RandomAccessFile(path_, access);
} catch (IOException ioe) {
file = null;
throw new TTransportException(ioe.getMessage());
}
}
}
/** Close file, subsequent read/write activity will throw exceptions */
@Override
public void close() {
if (file != null) {
try {
file.close();
} catch (Exception e) {
// Nothing to do
}
file = null;
}
}
/**
* Read up to len many bytes into buf at offset
*
* @param buf houses bytes read
* @param off offset into buff to begin writing to
* @param len maximum number of bytes to read
* @return number of bytes actually read
* @throws TTransportException on read failure
*/
@Override
public int read(byte[] buf, int off, int len) throws TTransportException {
if (!readable) {
throw new TTransportException("Read operation on write only file");
}
checkReadBytesAvailable(len);
int iBytesRead = 0;
try {
iBytesRead = file.read(buf, off, len);
} catch (IOException ioe) {
file = null;
throw new TTransportException(ioe.getMessage());
}
return iBytesRead;
}
/**
* Write len many bytes from buff starting at offset
*
* @param buf buffer containing bytes to write
* @param off offset into buffer to begin writing from
* @param len number of bytes to write
* @throws TTransportException on write failure
*/
@Override
public void write(byte[] buf, int off, int len) throws TTransportException {
try {
file.write(buf, off, len);
} catch (IOException ioe) {
file = null;
throw new TTransportException(ioe.getMessage());
}
}
/**
* Move file pointer to specified offset, new read/write calls will act here
*
* @param offset bytes from beginning of file to move pointer to
* @throws TTransportException is seek fails
*/
public void seek(long offset) throws TTransportException {
try {
file.seek(offset);
} catch (IOException ex) {
throw new TTransportException(ex.getMessage());
}
}
/**
* Return the length of the file in bytes
*
* @return length of the file in bytes
* @throws TTransportException if file access fails
*/
public long length() throws TTransportException {
try {
return file.length();
} catch (IOException ex) {
throw new TTransportException(ex.getMessage());
}
}
/**
* Return current file pointer position in bytes from beginning of file
*
* @return file pointer position
* @throws TTransportException if file access fails
*/
public long getFilePointer() throws TTransportException {
try {
return file.getFilePointer();
} catch (IOException ex) {
throw new TTransportException(ex.getMessage());
}
}
}
|