本文整理汇总了Java中java.io.RandomAccessFile.getFilePointer方法的典型用法代码示例。如果您正苦于以下问题:Java RandomAccessFile.getFilePointer方法的具体用法?Java RandomAccessFile.getFilePointer怎么用?Java RandomAccessFile.getFilePointer使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.io.RandomAccessFile
的用法示例。
在下文中一共展示了RandomAccessFile.getFilePointer方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: extractDate
import java.io.RandomAccessFile; //导入方法依赖的package包/类
private Date extractDate(RandomAccessFile raf) throws IOException {
Date date = null;
long pos = raf.getFilePointer();
String line = readSubLine(raf);
while (line != null && pos < Long.MAX_VALUE) {
date = parseDate(line);
if (date == null) {
int c = goToNextLine(raf);
pos = raf.getFilePointer();
line = c != -1 ? readSubLine(raf) : null;
} else {
line = null;
}
}
raf.seek(pos);
return date;
}
示例2: insertContent
import java.io.RandomAccessFile; //导入方法依赖的package包/类
public static void insertContent(String path, long offset, byte[] content) throws IOException {
RandomAccessFile sourceRaf = new RandomAccessFile(path, "rw");
File tempFile = FileUtil.createFile(path + ".tmp");
RandomAccessFile tempRaf = new RandomAccessFile(tempFile, "rw");
long fileSize = sourceRaf.length();
FileChannel sourceChannel = sourceRaf.getChannel();
FileChannel targetChannel = tempRaf.getChannel();
long remaining = fileSize - offset;
long position = offset;
while (remaining > 0) {
long transferred = sourceChannel.transferTo(position, remaining, targetChannel);
remaining -= transferred;
position += transferred;
}
sourceChannel.truncate(offset);
sourceRaf.seek(offset);
sourceRaf.write(content);
long newOffset = sourceRaf.getFilePointer();
targetChannel.position(0);
sourceChannel.transferFrom(targetChannel, newOffset, (fileSize - offset));
sourceChannel.close();
targetChannel.close();
FileUtil.deleteIfExists(tempFile);
}
示例3: restoreFile
import java.io.RandomAccessFile; //导入方法依赖的package包/类
/** todo - take account of incomplete addition of block due to lack of disk */
// buggy database files had size == position == 0 at the end
public static void restoreFile(String sourceName,
String destName) throws IOException {
RandomAccessFile source = new RandomAccessFile(sourceName, "r");
RandomAccessFile dest = new RandomAccessFile(destName, "rw");
while (source.getFilePointer() != source.length()) {
int size = source.readInt();
long position = source.readLong();
byte[] buffer = new byte[size];
source.read(buffer);
dest.seek(position);
dest.write(buffer);
}
source.close();
dest.close();
}
示例4: 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 = (int)Utils.readUint32(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);
}
示例5: nextPacket
import java.io.RandomAccessFile; //导入方法依赖的package包/类
/**
* get the next Packet from the RandomAccessFile
*
* @param raf
* @return - the Packet
* @throws Exception
*/
public PacketSeek nextPacket(RandomAccessFile raf) throws Exception {
String line = null;
int count = 0;
long pos = raf.getFilePointer();
while ((line = raf.readLine()) != null) {
Packet p = LogReader.lineAsPacket(line);
if (p != null) {
PacketSeek packetSeek = new PacketSeek();
packetSeek.packet = p;
packetSeek.pos = pos;
return packetSeek;
}
pos = raf.getFilePointer();
if (checkCountLimit("line", count++, false))
return null;
} // while
return null;
}
示例6: recreate
import java.io.RandomAccessFile; //导入方法依赖的package包/类
public static MSPartialFile recreate(File file) throws IOException {
if (!file.exists())
throw new IOException("File does not exist");
RandomAccessFile maskFile = new RandomAccessFile(file, "rw");
RobustMML mml;
try {
mml = new RobustMML(maskFile.readUTF());
} catch (MMLException ex) {
throw new IOException("MML Meta data was badly formed. This file is corrupt.");
}
return new MSPartialFile(file, maskFile, new PartialFileHeader(mml, (int) maskFile
.getFilePointer()));
}
示例7: 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);
}
示例8: readLineWithEOL
import java.io.RandomAccessFile; //导入方法依赖的package包/类
public static String readLineWithEOL(
RandomAccessFile raf ) throws IOException {
StringBuilder line = new StringBuilder();
int c = -1;
boolean eol = false;
while (!eol) {
switch (c = raf.read()) {
case -1:
eol = true;
break;
case '\n':
eol = true;
line.append('\n');
break;
case '\r':
eol = true;
line.append('\r');
long cur = raf.getFilePointer();
if ( (raf.read()) != '\n') {
raf.seek(cur);
} else {
line.append('\n');
}
break;
default:
line.append((char) c);
break;
}
}
if (c == -1 && line.length() == 0) {
return null;
}
return line.toString();
}
示例9: summarizeOggPageHeaders
import java.io.RandomAccessFile; //导入方法依赖的package包/类
/**
* Summarize all the ogg headers in a file
* <p/>
* A useful utility function
*
* @param oggFile
* @throws CannotReadException
* @throws IOException
*/
public void summarizeOggPageHeaders(File oggFile) throws CannotReadException, IOException {
RandomAccessFile raf = new RandomAccessFile(oggFile, "r");
while (raf.getFilePointer() < raf.length()) {
System.out.println("pageHeader starts at absolute file position:" + raf.getFilePointer());
OggPageHeader pageHeader = OggPageHeader.read(raf);
System.out.println("pageHeader finishes at absolute file position:" + raf.getFilePointer());
System.out.println(pageHeader + "\n");
raf.seek(raf.getFilePointer() + pageHeader.getPageLength());
}
System.out.println("Raf File Pointer at:" + raf.getFilePointer() + "File Size is:" + raf.length());
raf.close();
}
示例10: loadAllReferences
import java.io.RandomAccessFile; //导入方法依赖的package包/类
public void loadAllReferences() {
try {
RandomAccessFile raf = new RandomAccessFile(daaFile, "r");
try {
raf.seek(getLocationOfBlockInFile(refNamesBlockIndex));
referenceNames = new byte[(int) dbSeqsUsed][];
referenceLocations = new long[1 + ((int) dbSeqsUsed >>> referenceLocationChunkBits)];
for (int i = 0; i < (int) dbSeqsUsed; i++) {
if ((i & (referenceLocationChunkSize - 1)) == 0) {
referenceLocations[i >>> referenceLocationChunkBits] = raf.getFilePointer();
}
int c = raf.read();
while (c != 0)
c = raf.read();
}
refLengths = new int[(int) dbSeqsUsed];
for (int i = 0; i < dbSeqsUsed; i++) {
ByteBuffer buffer = ByteBuffer.allocate(4);
buffer.order(ByteOrder.LITTLE_ENDIAN);
raf.read(buffer.array());
refLengths[i] = buffer.getInt();
}
} finally {
raf.close();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
示例11: fastSignInfo
import java.io.RandomAccessFile; //导入方法依赖的package包/类
public static int fastSignInfo(File file) {
int v1 = SIGNED_NOT;
int v2 = SIGNED_NOT;
try {
ZipFile zip = new ZipFile(file);
if (zip.getEntry("META-INF/CERT.RSA") != null) {
v1 = SIGNED_V1;
}
Field archiveField = ZipFile.class.getDeclaredField("archive");
archiveField.setAccessible(true);
final RandomAccessFile archive = (RandomAccessFile) archiveField.get(zip);
Method positionAtCentralDirectory = ZipFile.class.getDeclaredMethod("positionAtCentralDirectory");
positionAtCentralDirectory.setAccessible(true);
positionAtCentralDirectory.invoke(zip);
long centralDirectoryOffset = archive.getFilePointer();
archive.seek(centralDirectoryOffset - 24);
byte[] buffer = new byte[24];
archive.readFully(buffer);
zip.close();
ByteBuffer footer = ByteBuffer.wrap(buffer);
footer.order(ByteOrder.LITTLE_ENDIAN);
if ((footer.getLong(8) == APK_SIG_BLOCK_MAGIC_LO)
&& (footer.getLong(16) == APK_SIG_BLOCK_MAGIC_HI)) {
v2 = SIGNED_V2;
}
} catch (IOException | NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException | NoSuchMethodException | InvocationTargetException ex) {
}
return v1 | v2;
}
示例12: read
import java.io.RandomAccessFile; //导入方法依赖的package包/类
/**
* Read next PageHeader from file
*
* @param raf
* @return
* @throws IOException
* @throws CannotReadException
*/
public static OggPageHeader read(RandomAccessFile raf) throws IOException, CannotReadException {
long start = raf.getFilePointer();
logger.fine("Trying to read OggPage at:" + start);
byte[] b = new byte[OggPageHeader.CAPTURE_PATTERN.length];
raf.read(b);
if (!(Arrays.equals(b, OggPageHeader.CAPTURE_PATTERN))) {
raf.seek(start);
if (AbstractID3v2Tag.isId3Tag(raf)) {
logger.warning(ErrorMessage.OGG_CONTAINS_ID3TAG.getMsg(raf.getFilePointer() - start));
raf.read(b);
if ((Arrays.equals(b, OggPageHeader.CAPTURE_PATTERN))) {
//Go to the end of the ID3 header
start = raf.getFilePointer() - OggPageHeader.CAPTURE_PATTERN.length;
}
} else {
throw new CannotReadException(ErrorMessage.OGG_HEADER_CANNOT_BE_FOUND.getMsg(new String(b)));
}
}
raf.seek(start + OggPageHeader.FIELD_PAGE_SEGMENTS_POS);
int pageSegments = raf.readByte() & 0xFF; //unsigned
raf.seek(start);
b = new byte[OggPageHeader.OGG_PAGE_HEADER_FIXED_LENGTH + pageSegments];
raf.read(b);
OggPageHeader pageHeader = new OggPageHeader(b);
pageHeader.setStartByte(start);
//Now just after PageHeader, ready for Packet Data
return pageHeader;
}
示例13: ensureOnEqualBoundary
import java.io.RandomAccessFile; //导入方法依赖的package包/类
/**
* If Size is not even then we skip a byte, because chunks have to be aligned
*
* @param raf
* @param chunkHeader
* @throws java.io.IOException
*/
public static void ensureOnEqualBoundary(final RandomAccessFile raf,ChunkHeader chunkHeader) throws IOException
{
if (Utils.isOddLength(chunkHeader.getSize()))
{
// Must come out to an even byte boundary unless at end of file
if(raf.getFilePointer()<raf.length())
{
logger.config("Skipping Byte because on odd boundary");
raf.skipBytes(1);
}
}
}
示例14: handleNextRecord
import java.io.RandomAccessFile; //导入方法依赖的package包/类
private boolean handleNextRecord(RecordUpdater recordUpdater, RandomAccessFile raf) throws IOException {
// populate bytes
if (raf.read(messageOffset) == -1) {
// eof
return false;
}
final long offset = getLong(messageOffset);
raf.read(messageLength);
final long crcOffset = raf.getFilePointer();
raf.read(messageCrc);
messageMagicValue = (byte) raf.read();
// read past the 'attributes' byte which we don't care about
byte attributes = (byte) raf.read();
if (messageMagicValue > 0) {
raf.read(messageTimestamp);
}
raf.read(messageKeyLength);
final long messageKeyOffset = raf.getFilePointer();
final int keyLength = getInteger(messageKeyLength);
final byte[] messageKey;
if (keyLength == -1) {
messageKey = new byte[0];
} else {
messageKey = new byte[keyLength];
raf.readFully(messageKey);
}
raf.readFully(messageValueLength);
final long messageValueOffset = raf.getFilePointer();
final int valueLength = getInteger(messageValueLength);
final byte[] messageValue = new byte[valueLength];
raf.readFully(messageValue);
final long existingCrc = getLong(messageCrc);
if (verifyCrc) {
// could be turned on for verification
final long calculatedCrc = calculateCrc(attributes, messageKey, messageValue);
if (existingCrc != calculatedCrc) {
throw new IllegalStateException("Invalid CRC value detected. Expected " + calculatedCrc
+ " (calculated) but found " + existingCrc + " (in file)");
}
}
if (recordUpdater != null && recordUpdater.update(offset, messageKey, messageValue)) {
// Update CRC
final long newCrc = calculateCrc(attributes, messageKey, messageValue);
if (existingCrc != newCrc) {
raf.seek(crcOffset);
raf.write(getBytes(newCrc, messageCrc.length));
}
raf.seek(messageKeyOffset);
raf.write(messageKey);
raf.seek(messageValueOffset);
raf.write(messageValue);
logger.debug("Updated record with offset={} in segment file: {}", offset, file);
recordsUpdated++;
}
return true;
}
示例15: getBlob
import java.io.RandomAccessFile; //导入方法依赖的package包/类
private boolean getBlob(RandomAccessFile file, int offset,
LookupRequest req) throws IOException {
byte[] header = mBlobHeader;
long oldPosition = file.getFilePointer();
try {
file.seek(offset);
if (file.read(header) != BLOB_HEADER_SIZE) {
Log.w(TAG, "cannot read blob header");
return false;
}
long blobKey = readLong(header, BH_KEY);
if (blobKey == 0) {
return false; // This entry has been cleared.
}
if (blobKey != req.key) {
Log.w(TAG, "blob key does not match: " + blobKey);
return false;
}
int sum = readInt(header, BH_CHECKSUM);
int blobOffset = readInt(header, BH_OFFSET);
if (blobOffset != offset) {
Log.w(TAG, "blob offset does not match: " + blobOffset);
return false;
}
int length = readInt(header, BH_LENGTH);
if (length < 0 || length > mMaxBytes - offset - BLOB_HEADER_SIZE) {
Log.w(TAG, "invalid blob length: " + length);
return false;
}
if (req.buffer == null || req.buffer.length < length) {
req.buffer = new byte[length];
}
byte[] blob = req.buffer;
req.length = length;
if (file.read(blob, 0, length) != length) {
Log.w(TAG, "cannot read blob data");
return false;
}
if (checkSum(blob, 0, length) != sum) {
Log.w(TAG, "blob checksum does not match: " + sum);
return false;
}
return true;
} catch (Throwable t) {
Log.e(TAG, "getBlob failed.", t);
return false;
} finally {
file.seek(oldPosition);
}
}