本文整理汇总了Java中java.io.RandomAccessFile.length方法的典型用法代码示例。如果您正苦于以下问题:Java RandomAccessFile.length方法的具体用法?Java RandomAccessFile.length怎么用?Java RandomAccessFile.length使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.io.RandomAccessFile
的用法示例。
在下文中一共展示了RandomAccessFile.length方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: fillPhotoSizeWithBytes
import java.io.RandomAccessFile; //导入方法依赖的package包/类
public static void fillPhotoSizeWithBytes(TLRPC.PhotoSize photoSize) {
if (photoSize == null || photoSize.bytes != null) {
return;
}
File file = FileLoader.getPathToAttach(photoSize, true);
try {
RandomAccessFile f = new RandomAccessFile(file, "r");
int len = (int) f.length();
if (len < 20000) {
photoSize.bytes = new byte[(int) f.length()];
f.readFully(photoSize.bytes, 0, photoSize.bytes.length);
}
} catch (Throwable e) {
FileLog.e("tmessages", e);
}
}
示例2: RandomAccessMmapObject
import java.io.RandomAccessFile; //导入方法依赖的package包/类
public RandomAccessMmapObject(final RandomAccessFile randomAccessFile, String mode)
throws IOException, IllegalArgumentException {
if (randomAccessFile.length() > Integer.MAX_VALUE) {
throw new IllegalArgumentException("Only files up to 2GiB in size are supported.");
}
FileChannel.MapMode mapMode;
if (mode.equals("r")) {
mapMode = FileChannel.MapMode.READ_ONLY;
} else {
mapMode = FileChannel.MapMode.READ_WRITE;
}
mFileChannel = randomAccessFile.getChannel();
mByteBuffer = mFileChannel.map(mapMode, 0, randomAccessFile.length());
mByteBuffer.position(0);
mShouldDeleteFileOnRelease = false;
mFile = null;
}
示例3: open
import java.io.RandomAccessFile; //导入方法依赖的package包/类
@Override
public long open(DataSpec dataSpec) throws FileDataSourceException {
try {
uri = dataSpec.uri;
file = new RandomAccessFile(dataSpec.uri.getPath(), "r");
file.seek(dataSpec.position);
bytesRemaining = dataSpec.length == C.LENGTH_UNSET ? file.length() - dataSpec.position
: dataSpec.length;
if (bytesRemaining < 0) {
throw new EOFException();
}
} catch (IOException e) {
throw new FileDataSourceException(e);
}
opened = true;
if (listener != null) {
listener.onTransferStart(this, dataSpec);
}
return bytesRemaining;
}
示例4: setDumpBuffer
import java.io.RandomAccessFile; //导入方法依赖的package包/类
private void setDumpBuffer(RandomAccessFile file) throws IOException {
long length = file.length();
try {
if (length > Integer.MAX_VALUE) {
dumpBuffer = new LongMemoryMappedData(file, length);
} else {
dumpBuffer = new MemoryMappedData(file, length);
}
} catch (IOException ex) {
if (ex.getCause() instanceof OutOfMemoryError) {
dumpBuffer = new FileData(file, length);
} else {
throw ex;
}
}
}
示例5: readChunk
import java.io.RandomAccessFile; //导入方法依赖的package包/类
public static RealChunk readChunk(RandomAccessFile raf)
throws CannotReadException, IOException {
final String id = Utils.readString(raf, 4);
final int size = Utils.readUint32AsInt(raf);
if (size < 8) {
throw new CannotReadException(
"Corrupt file: RealAudio chunk length at position "
+ (raf.getFilePointer() - 4)
+ " cannot be less than 8");
}
if (size > (raf.length() - raf.getFilePointer() + 8)) {
throw new CannotReadException(
"Corrupt file: RealAudio chunk length of " + size
+ " at position " + (raf.getFilePointer() - 4)
+ " extends beyond the end of the file");
}
final byte[] bytes = new byte[size - 8];
raf.readFully(bytes);
return new RealChunk(id, size, bytes);
}
示例6: getLength
import java.io.RandomAccessFile; //导入方法依赖的package包/类
@Override
public long
getLength(
RandomAccessFile raf )
throws FMFileManagerException
{
try{
AEThread2.setDebug( owner );
return( raf.length());
}catch( Throwable e ){
throw( new FMFileManagerException( "getLength fails", e ));
}
}
示例7: findLastDate
import java.io.RandomAccessFile; //导入方法依赖的package包/类
private Date findLastDate(RandomAccessFile raf) throws IOException {
Date date = null;
long pos = raf.length() - 2;
while (date == null && pos > 0) {
do {
raf.seek(pos);
--pos;
} while (raf.read() != 10 && pos > 0); // '\n'
date = parseDate(raf);
}
return date;
}
示例8: Descriptor
import java.io.RandomAccessFile; //导入方法依赖的package包/类
public Descriptor(File file, String mode) throws IOException {
this.file = file;
this.mode = mode;
isOpen=true;
RandomAccessFile raf = new RandomAccessFile(file, mode);
length=raf.length();
fileMap.put(Thread.currentThread().getName(), raf);
}
示例9: ensureLockState
import java.io.RandomAccessFile; //导入方法依赖的package包/类
public LockState ensureLockState(RandomAccessFile lockFileAccess) throws IOException {
if (lockFileAccess.length() == 0) {
// File did not exist before locking, use some initial state
LockState state = protocol.createInitialState();
writeState(lockFileAccess, state);
return state;
} else {
return readState(lockFileAccess);
}
}
示例10: removeLastLine
import java.io.RandomAccessFile; //导入方法依赖的package包/类
public static void removeLastLine(String filename, int len) {
try{
RandomAccessFile raf = new RandomAccessFile(filename, "rw");
long length = raf.length();
raf.setLength(length - len);
raf.close();
}
catch(Exception ex){
ex.printStackTrace();
}
}
示例11: read
import java.io.RandomAccessFile; //导入方法依赖的package包/类
public AiffAudioHeader read(RandomAccessFile raf) throws CannotReadException, IOException {
if (raf.length() < 4) {
//Empty File
throw new CannotReadException("Not an AIFF file; too short");
}
return null; // TODO stub
}
示例12: shift
import java.io.RandomAccessFile; //导入方法依赖的package包/类
/** Copy file.content[from...f.length-1] into file.content[to...], then truncate the file after that point.
* <p> If (from > to), this means we simply delete the portion of the file beginning at "to" and up to but excluding "from".
* <p> If (from < to), this means we insert (to-from) number of ARBITRARY bytes into the "from" location
* and shift the original file content accordingly.
* <p> Note: after this operation, the file's current position will be moved to the start of the file.
* @throws IOException if (from < 0) || (to < 0) || (from >= file.length())
*/
public static void shift (RandomAccessFile file, long from, long to) throws IOException {
long total = file.length();
if (from<0 || from>=total || to<0) throw new IOException(); else if (from==to) {file.seek(0); return;}
final byte buf[] = new byte[4096];
int res;
if (from>to) {
while(true) {
file.seek(from);
if ((res=file.read(buf)) <= 0) { file.setLength(to); file.seek(0); return; }
file.seek(to); file.write(buf, 0, res); from=from+res; to=to+res;
}
} else {
file.seek(total);
for(long todo=to-from; todo>0;) {
if (todo >= buf.length) {file.write(buf); todo = todo - buf.length;} else {file.write(buf, 0, (int)todo); break;}
}
for(long todo=total-from; todo>0; total=total-res, todo=todo-res) {
if (todo > buf.length) res=buf.length; else res=(int)todo;
file.seek(total - res);
for(int done=0; done<res;) { int r=file.read(buf, done, res-done); if (r<=0) throw new IOException(); else done += r; }
file.seek(total - res + (to - from));
file.write(buf, 0, res);
}
}
file.seek(0);
}
示例13: readWordIndex
import java.io.RandomAccessFile; //导入方法依赖的package包/类
private int readWordIndex(RandomAccessFile raf) throws IOException {
String word = readWord(raf);
if(raf.length()==raf.getFilePointer()) {
return -1;
}
return searchVocab(word);
}
示例14: deserialize
import java.io.RandomAccessFile; //导入方法依赖的package包/类
/**
* Read the inflights file and return a
* {@link com.google.common.collect.SetMultimap}
* of transactionIDs to events that were inflight.
*
* @return - map of inflight events per txnID.
*/
public SetMultimap<Long, Long> deserialize()
throws IOException, BadCheckpointException {
SetMultimap<Long, Long> inflights = HashMultimap.create();
if (!fileChannel.isOpen()) {
file = new RandomAccessFile(inflightEventsFile, "rw");
fileChannel = file.getChannel();
}
if (file.length() == 0) {
return inflights;
}
file.seek(0);
byte[] checksum = new byte[16];
file.read(checksum);
ByteBuffer buffer = ByteBuffer.allocate(
(int) (file.length() - file.getFilePointer()));
fileChannel.read(buffer);
byte[] fileChecksum = digest.digest(buffer.array());
if (!Arrays.equals(checksum, fileChecksum)) {
throw new BadCheckpointException("Checksum of inflights file differs"
+ " from the checksum expected.");
}
buffer.position(0);
LongBuffer longBuffer = buffer.asLongBuffer();
try {
while (true) {
long txnID = longBuffer.get();
int numEvents = (int) (longBuffer.get());
for (int i = 0; i < numEvents; i++) {
long val = longBuffer.get();
inflights.put(txnID, val);
}
}
} catch (BufferUnderflowException ex) {
LOG.debug("Reached end of inflights buffer. Long buffer position ="
+ String.valueOf(longBuffer.position()));
}
return inflights;
}
示例15: readBytesFromFile
import java.io.RandomAccessFile; //导入方法依赖的package包/类
/**
* @param filePath The path relative to
* {@link com.google.testing.util.TestUtil#getDefaultSrcDir}.
*/
public static ByteString readBytesFromFile(String filename) {
File fullPath = new File(getTestDataDir(), filename);
try {
RandomAccessFile file = new RandomAccessFile(fullPath, "r");
byte[] content = new byte[(int) file.length()];
file.readFully(content);
return ByteString.copyFrom(content);
} catch (IOException e) {
// Throw a RuntimeException here so that we can call this function from
// static initializers.
throw new IllegalArgumentException(
"Couldn't read file: " + fullPath.getPath(), e);
}
}