public void skip(long start) throws IOException { file.seek(file.getFilePointer()+start); }
private int nextId() throws IOException { if (raf.length() - raf.getFilePointer() >= 4) { int id = raf.readInt(); return id < 0 ? id : table[id]; } return -2; }
/** * Skips over {@code count} bytes in this file. Less than {@code count} * bytes are skipped if the end of the file is reached or an exception is * thrown during the operation. Nothing is done if {@code count} is * negative. * * @param count * the number of bytes to skip. * @return the number of bytes actually skipped. * @throws IOException * if this file is closed or another I/O error occurs. */ public int skipBytes(int count) throws IOException { if (count > 0) { long currentPos = getFilePointer(), eof = length(); int newCount = (int) ((currentPos + count > eof) ? eof - currentPos : count); seek(currentPos + newCount); return newCount; } return 0; }
public void run() throws Exception { Channel ch = bootstrap.bind(0).sync().channel(); long pointer = 0; for (;;) { long len = file.length(); if (len < pointer) { // file was reset pointer = len; } else if (len > pointer) { // Content was added RandomAccessFile raf = new RandomAccessFile(file, "r"); raf.seek(pointer); String line; while ((line = raf.readLine()) != null) { ch.writeAndFlush(new LogEvent(null, -1, file.getAbsolutePath(), line)); } pointer = raf.getFilePointer(); raf.close(); } try { Thread.sleep(1000); } catch (InterruptedException e) { Thread.interrupted(); break; } } }
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());
private void readFileContents(File file) throws IOException { try (RandomAccessFile data = new RandomAccessFile(file, "r")) { while (data.getFilePointer() < data.length()) { // Read header (and put the file pointer to the data start) KwdHeader header = readKwdHeader(data); readFileContents(header, data); } if (data.getFilePointer() != data.length()) { throw new RuntimeException("Failed to parse file"); } } }
long lastOffset = 0; try (final RandomAccessFile raf = new RandomAccessFile(dest, "rw")) { final int fd = getfd(raf.getFD()); offset = raf.getFilePointer(); numBytes = 0;
FileChannel fileChannel = null; try { fIn = new RandomAccessFile(apkFile, "rw"); fileChannel = fIn.getChannel(); final long commentLength = ApkUtil.getCommentLength(fileChannel); fIn.seek(centralDirStartOffset); fIn.setLength(fIn.getFilePointer()); fIn.seek(fileChannel.size() - commentLength - 6);
/** * @throws Exception If failed. */ @Test public void testReadLineFromBinaryFile() throws Exception { File file = new File(FILE_PATH); file.deleteOnExit(); RandomAccessFile raf = new RandomAccessFile(file, "rw"); byte[] b = new byte[100]; Arrays.fill(b, (byte)10); raf.write(b); raf.writeBytes("swap-spaces/space1/b53b3a3d6ab90ce0268229151c9bde11|" + "b53b3a3d6ab90ce0268229151c9bde11|1315392441288" + U.nl()); raf.writeBytes("swap-spaces/space1/b53b3a3d6ab90ce0268229151c9bde11|" + "b53b3a3d6ab90ce0268229151c9bde11|1315392441288" + U.nl()); raf.write(b); raf.writeBytes("test" + U.nl()); raf.getFD().sync(); raf.seek(0); while (raf.getFilePointer() < raf.length()) { String s = raf.readLine(); X.println("String: " + s + ";"); X.println("String length: " + s.length()); X.println("File pointer: " + raf.getFilePointer()); } }
/** * Seeks to the given offset. */ private void seekToOffset(int offset) throws IOException { if (randomAccessFile.getFilePointer() != offset) { randomAccessFile.seek(offset); } }
@Override public int available() { try { return (int) (source.length() - source.getFilePointer()); } catch (IOException ex) { return 0; } }
public byte[] getNextInput() { try { if (file.length() - file.getFilePointer() < buffer.length) { int s = (int) (file.length() - file.getFilePointer()); ByteBuffer bb = ByteBuffer.wrap(buffer, 0, s); this.file.getChannel().read(bb); bb = ByteBuffer.wrap(buffer, buffer.length - s, s); this.file.seek(0); this.file.getChannel().read(bb); } else { ByteBuffer bb = ByteBuffer.wrap(buffer); this.file.getChannel().read(bb); } } catch (IOException e) { e.printStackTrace(); } return buffer; }
private void printLogSettingsFile() { try { RandomAccessFile raf = new RandomAccessFile(LogSettingsPath, "r"); while (raf.getFilePointer() < raf.length()) { settingsArea.append(raf.readLine() + LB); } raf.close(); } catch (Exception e) { settingsArea.setEnabled(false); } }
RandomAccessFile file = new RandomAccessFile("filename.txt", "r"); List<Long> indexList = new ArrayList(); long pos = 0; while (file.readLine() != null) { Long linePos = new Long(pos); indexList.add(linePos); pos = file.getFilePointer(); } int indexSize = indexList.size(); Long[] indexArray = new Long[indexSize]; indexList.toArray(indexArray);
FileChannel fc = null; try { raf = new RandomAccessFile(apkFile, "rw"); fc = raf.getChannel(); final long commentLength = ApkUtil.findZipCommentLength(fc); raf.seek(centralDirStartOffset); final byte[] centralDirBytes = new byte[(int) (fc.size() - centralDirStartOffset)]; raf.read(centralDirBytes); raf.setLength(raf.getFilePointer()); raf.seek(fc.size() - commentLength - 6);
public void insert(String filename, long offset, byte[] content) { RandomAccessFile r = new RandomAccessFile(new File(filename), "rw"); RandomAccessFile rtemp = new RandomAccessFile(new File(filename + "~"), "rw"); long fileSize = r.length(); FileChannel sourceChannel = r.getChannel(); FileChannel targetChannel = rtemp.getChannel(); sourceChannel.transferTo(offset, (fileSize - offset), targetChannel); sourceChannel.truncate(offset); r.seek(offset); r.write(content); long newOffset = r.getFilePointer(); targetChannel.position(0L); sourceChannel.transferFrom(targetChannel, newOffset, (fileSize - offset)); sourceChannel.close(); targetChannel.close(); }
private void flushBuf() throws IOException { if (this.bufDirty) { if (super.getFilePointer() != this.bufStartPos) { super.seek(this.bufStartPos); } super.write(this.buf, 0, this.bufUsedSize); this.bufDirty = false; } }
@Override public int available() throws IOException { return (int) (raf.length() - raf.getFilePointer()); }
public void corruptRecoveryLocation(Location recoveryPosition) throws IOException { DataFile dataFile = getDataFile(recoveryPosition); // with corruption on recovery we have no faith in the content - slip to the next batch record or eof DataFileAccessor reader = accessorPool.openDataFileAccessor(dataFile); try { RandomAccessFile randomAccessFile = reader.getRaf().getRaf(); randomAccessFile.seek(recoveryPosition.getOffset() + 1); byte[] data = new byte[getWriteBatchSize()]; ByteSequence bs = new ByteSequence(data, 0, randomAccessFile.read(data)); int nextOffset = 0; if (findNextBatchRecord(bs, randomAccessFile) >= 0) { nextOffset = Math.toIntExact(randomAccessFile.getFilePointer() - bs.remaining()); } else { nextOffset = Math.toIntExact(randomAccessFile.length()); } Sequence sequence = new Sequence(recoveryPosition.getOffset(), nextOffset - 1); LOG.warn("Corrupt journal records found in '{}' between offsets: {}", dataFile.getFile(), sequence); // skip corruption on getNextLocation recoveryPosition.setOffset(nextOffset); recoveryPosition.setSize(-1); dataFile.corruptedBlocks.add(sequence); } catch (IOException e) { } finally { accessorPool.closeDataFileAccessor(reader); } }
RandomAccessFile first = new RandomAccessFile("D:\\Temp.txt","r"); RandomAccessFile second = new RandomAccessFile("D:\\Temp.txt","r"); String line; String nextLine = second.readLine(); while (nextLine != null) { line = first.readLine(); nextLine = second.readLine(); if ((line.contains("[DesiredText]") && nextLine.isEmpty())) { pozycje.add(first.getFilePointer()); } }