List of usage examples for java.io RandomAccessFile getFilePointer
public native long getFilePointer() throws IOException;
From source file:org.scriptbox.util.common.io.Tailer.java
/** * Read new lines.// www.j ava2s . c o m * * @param reader The file to read * @return The new position after the lines have been read * @throws java.io.IOException if an I/O error occurs. */ private long readLines(RandomAccessFile reader) throws IOException { StringBuilder sb = new StringBuilder(); long pos = reader.getFilePointer(); long rePos = pos; // position to re-read int num; boolean seenCR = false; while (run && ((num = reader.read(inbuf)) != -1)) { for (int i = 0; i < num; i++) { byte ch = inbuf[i]; switch (ch) { case '\n': seenCR = false; // swallow CR before LF listener.handle(sb.toString()); sb.setLength(0); rePos = pos + i + 1; break; case '\r': if (seenCR) { sb.append('\r'); } seenCR = true; break; default: if (seenCR) { seenCR = false; // swallow final CR listener.handle(sb.toString()); sb.setLength(0); rePos = pos + i + 1; } sb.append((char) ch); // add character, not its ascii value } } pos = reader.getFilePointer(); } reader.seek(rePos); // Ensure we can re-read if necessary return rePos; }
From source file:com.haulmont.cuba.core.sys.LogControlImpl.java
protected String readUtf8Line(RandomAccessFile logFile) throws IOException { int c = -1;//from w ww . j av a 2 s . com boolean eol = false; ByteArrayOutputStream input = new ByteArrayOutputStream(); while (!eol) { switch (c = logFile.read()) { case -1: case '\n': eol = true; break; case '\r': eol = true; long cur = logFile.getFilePointer(); if ((logFile.read()) != '\n') { logFile.seek(cur); } break; default: input.write((byte) c); break; } } if ((c == -1) && (input.size() == 0)) { return null; } return new String(input.toByteArray(), StandardCharsets.UTF_8); }
From source file:dk.netarkivet.common.utils.FileUtils.java
/** * Read the last line in a file. Note this method is not UTF-8 safe. * * @param file input file to read last line from. * @return The last line in the file (ending newline is irrelevant), * returns an empty string if file is empty. * @throws ArgumentNotValid on null argument, or file is not a readable * file.//from ww w . ja va 2 s. co m * @throws IOFailure on IO trouble reading file. */ public static String readLastLine(File file) { ArgumentNotValid.checkNotNull(file, "File file"); if (!file.isFile() || !file.canRead()) { final String errMsg = "File '" + file.getAbsolutePath() + "' is not a readable file."; log.warn(errMsg); throw new ArgumentNotValid(errMsg); } if (file.length() == 0) { return ""; } RandomAccessFile rafile = null; try { rafile = new RandomAccessFile(file, "r"); //seek to byte one before end of file (remember we know the file is // not empty) - this ensures that an ending newline is not read rafile.seek(rafile.length() - 2); //now search to the last linebreak, or beginning of file while (rafile.getFilePointer() != 0 && rafile.read() != '\n') { //search back two, because we just searched forward one to find //newline rafile.seek(rafile.getFilePointer() - 2); } return rafile.readLine(); } catch (IOException e) { final String errMsg = "Unable to access file '" + file.getAbsolutePath() + "'"; log.warn(errMsg, e); throw new IOFailure(errMsg, e); } finally { try { if (rafile != null) { rafile.close(); } } catch (IOException e) { log.debug("Unable to close file '" + file.getAbsolutePath() + "' after reading", e); } } }
From source file:com.microsoft.azure.management.datalake.store.uploader.SingleSegmentUploader.java
/** * Uploads the segment contents./*w ww . j a v a 2 s . co m*/ * * @param inputStream The input stream. * @param endPosition The end position. * @throws Exception if there is any failure attempting to upload the contents of a single segment. */ private void uploadSegmentContents(RandomAccessFile inputStream, long endPosition) throws Exception { long bytesCopiedSoFar = 0; // we start off with a fresh stream byte[] buffer = new byte[BUFFER_LENGTH]; int residualBufferLength = 0; //the number of bytes that remained in the buffer from the last upload (bytes which were not uploaded) while (inputStream.getFilePointer() < endPosition) { //read a block of data, and keep track of how many bytes are actually read int bytesRead = readIntoBuffer(inputStream, buffer, residualBufferLength, endPosition); int bufferDataLength = residualBufferLength + bytesRead; //determine the cutoff offset for upload - everything before will be uploaded, everything after is residual; (the position of the last record in this buffer) int uploadCutoff = bufferDataLength; if (!metadata.isBinary()) { uploadCutoff = determineUploadCutoffForTextFile(buffer, bufferDataLength, inputStream); } bytesCopiedSoFar = uploadBuffer(buffer, uploadCutoff, bytesCopiedSoFar); residualBufferLength = bufferDataLength - uploadCutoff; if (residualBufferLength > 0) { //move the remainder of the buffer to the front System.arraycopy(buffer, uploadCutoff, buffer, 0, residualBufferLength); } } //make sure we don't leave anything behind if (residualBufferLength > 0) { uploadBuffer(buffer, residualBufferLength, bytesCopiedSoFar); } buffer = null; }
From source file:edu.msu.cme.rdp.readseq.readers.core.SFFCore.java
@Override public LinkedHashMap<String, Long> scanInternal() throws IOException { if (commonHeader.indexOffset > commonHeader.headerLength) { return readIndex(); }// w w w . java 2 s . c o m RandomAccessFile seqFile = super.getRawFile(); seqFile.seek(commonHeader.headerLength); LinkedHashMap<String, Long> seqIndex = new LinkedHashMap(commonHeader.numReads); for (int index = 0; index < commonHeader.numReads; index++) { long pos = seqFile.getFilePointer(); ReadBlock block = readReadBlock(); seqIndex.put(block.name, pos); } return seqIndex; }
From source file:com.aol.advertising.qiao.injector.file.AbstractFileTailer.java
protected long checksum(RandomAccessFile raFile) throws IOException, InsufficientFileLengthException { long pos = raFile.getFilePointer(); try {//from ww w . j av a 2 s . c om byte[] buffer = new byte[checksumByteLength]; raFile.seek(0); int n = raFile.read(buffer); if (n < checksumByteLength) { String s; logger.warn(s = ("not enough data for checksum: current file size=" + n)); throw new InsufficientFileLengthException(s); } synchronized (_crc) { _crc.reset(); _crc.update(buffer); return _crc.getValue(); } } finally { raFile.seek(pos); } }
From source file:com.qumasoft.qvcslib.CompareFilesWithApacheDiff.java
private CompareLineInfo[] buildLinesFromFile(File inFile) throws IOException { List<CompareLineInfo> lineInfoList = new ArrayList<>(); RandomAccessFile randomAccessFile = new RandomAccessFile(inFile, "r"); long fileLength = randomAccessFile.length(); int startOfLineSeekPosition = 0; int currentSeekPosition = 0; byte character; while (currentSeekPosition < fileLength) { character = randomAccessFile.readByte(); currentSeekPosition = (int) randomAccessFile.getFilePointer(); if (character == '\n') { int endOfLine = (int) randomAccessFile.getFilePointer(); byte[] buffer = new byte[endOfLine - startOfLineSeekPosition]; randomAccessFile.seek(startOfLineSeekPosition); randomAccessFile.readFully(buffer); String line = new String(buffer, UTF8); CompareLineInfo lineInfo = new CompareLineInfo(startOfLineSeekPosition, createCompareLine(line)); lineInfoList.add(lineInfo);//from w w w .j av a 2 s . c o m startOfLineSeekPosition = endOfLine; } } // Add the final line which can happen if it doesn't end in a newline. if ((fileLength - startOfLineSeekPosition) > 0L) { byte[] buffer = new byte[(int) fileLength - startOfLineSeekPosition]; randomAccessFile.seek(startOfLineSeekPosition); randomAccessFile.readFully(buffer); String line = new String(buffer, UTF8); CompareLineInfo lineInfo = new CompareLineInfo(startOfLineSeekPosition, createCompareLine(line)); lineInfoList.add(lineInfo); } CompareLineInfo[] lineInfoArray = new CompareLineInfo[lineInfoList.size()]; int i = 0; for (CompareLineInfo lineInfo : lineInfoList) { lineInfoArray[i++] = lineInfo; } return lineInfoArray; }
From source file:org.multibit.file.FileHandler.java
/** * Delete a file with an overwrite of all of the data. * /*from w w w . j av a 2s . c o m*/ * Set bit patterns are used rather than random numbers to avoid a * futex_wait_queue_me error on Linux systems (related to /dev/random usage) * * @param file * @throws IOException */ public static void secureDelete(File file) throws IOException { log.debug("Start of secureDelete"); RandomAccessFile raf = null; if (file != null && file.exists()) { try { // Prep for file delete as this can be fiddly on windows. // Make sure it is writable and any references to it are garbage // collected and finalized. file.setWritable(true); System.gc(); long length = file.length(); raf = new RandomAccessFile(file, "rws"); raf.seek(0); raf.getFilePointer(); int pos = 0; while (pos < length) { raf.write(SECURE_DELETE_FILL_BYTES); pos += SECURE_DELETE_FILL_BYTES.length; } } finally { if (raf != null) { raf.close(); raf = null; } } boolean deleteSuccess = file.delete(); log.debug("Result of delete of file '" + file.getAbsolutePath() + "' was " + deleteSuccess); } log.debug("End of secureDelete"); }
From source file:com.haulmont.cuba.core.sys.LogControlImpl.java
@Override public String getTail(String fileName) throws LogControlException { // security check, supported only valid file names fileName = FilenameUtils.getName(fileName); StringBuilder sb = new StringBuilder(); RandomAccessFile randomAccessFile = null; try {//ww w . j a va 2 s . c om File logFile = new File(logDir, fileName); if (!logFile.exists()) throw new LogFileNotFoundException(fileName); randomAccessFile = new RandomAccessFile(logFile, "r"); long lengthFile = randomAccessFile.length(); if (lengthFile >= LOG_TAIL_AMOUNT_BYTES) { randomAccessFile.seek(lengthFile - LOG_TAIL_AMOUNT_BYTES); skipFirstLine(randomAccessFile); } while (randomAccessFile.read() != -1) { randomAccessFile.seek(randomAccessFile.getFilePointer() - 1); String line = readUtf8Line(randomAccessFile); if (line != null) { sb.append(line).append("\n"); } } } catch (IOException e) { log.error("Error reading log file", e); throw new LogControlException("Error reading log file: " + fileName); } finally { if (randomAccessFile != null) { try { randomAccessFile.close(); } catch (IOException ignored) { } } } return sb.toString(); }
From source file:org.commoncrawl.service.crawler.CrawlLog.java
/** * seek out next instance of sync bytes in the file input stream * //from w w w .j av a 2s. c o m * @param file * @throws IOException */ private static boolean seekToNextSyncBytesPos(byte[] syncBytesBuffer, RandomAccessFile file, long maxFileSize) throws IOException { while (file.getFilePointer() < maxFileSize) { try { // read in a sync.length buffer amount file.read(syncBytesBuffer); int syncLen = SYNC_BYTES_SIZE; // start scan for next sync position ... for (int i = 0; file.getFilePointer() < maxFileSize; i++) { int j = 0; for (; j < syncLen; j++) { if (_sync[j] != syncBytesBuffer[(i + j) % syncLen]) break; } if (j == syncLen) { // found matching sync bytes - reset file pos to before sync bytes file.seek(file.getFilePointer() - SYNC_BYTES_SIZE); // position // before // sync return true; } syncBytesBuffer[i % syncLen] = file.readByte(); } } catch (IOException e) { LOG.warn("IOException at:" + file.getFilePointer() + " Exception:" + CCStringUtils.stringifyException(e)); LOG.warn("Skipping to:" + file.getFilePointer() + 4096); file.seek(file.getFilePointer() + 4096); } } return false; }