@Override public int read(byte[] buffer, int byteOffset, int byteCount) throws IOException { synchronized (sharedRaf) { final long length = endOffset - offset; if (byteCount > length) { byteCount = (int) length; } sharedRaf.seek(offset); int count = sharedRaf.read(buffer, byteOffset, byteCount); if (count > 0) { offset += count; return count; } else { return -1; } } } @Override public long skip(long byteCount) throws IOException {
GEMFInputStream(final String filePath, final long offset, final int length) throws IOException { this.raf = new RandomAccessFile(filePath, "r"); raf.seek(offset); this.remainingBytes = length; }
static void modify(File file) throws IOException { long size = file.length(); if (size == 0) { recreateZeroSizeFile(file); return; } RandomAccessFile accessFile = new RandomAccessFile(file, "rwd"); accessFile.seek(size - 1); byte lastByte = accessFile.readByte(); accessFile.seek(size - 1); accessFile.write(lastByte); accessFile.close(); }
try{ String fileName = "c:/myraffile.txt"; File file = new File(fileName); RandomAccessFile raf = new RandomAccessFile(file, "rw"); raf.readChar(); raf.seek(0); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
private void initFile(String fullFileName) throws IOException { this.currFullFileName = fullFileName; this.hisFullFileName = fullFileName + HIS_DATA_FILENAME_POSTFIX; try { currDataFile = new File(currFullFileName); if (!currDataFile.exists()) { currDataFile.createNewFile(); trxStartTimeMills = System.currentTimeMillis(); } else { trxStartTimeMills = currDataFile.lastModified(); } currRaf = new RandomAccessFile(currDataFile, "rw"); currRaf.seek(currDataFile.length()); currFileChannel = currRaf.getChannel(); } catch (IOException exx) { LOGGER.error("init file error," + exx.getMessage()); throw exx; } }
/** * @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()); } }
public static void main(String[] args) throws Exception { if(args.length != 1) Utils.croak("USAGE: java " + CatReadOnlyStore.class.getName() + " directory"); File dir = new File(args[0]); Serializer<Object> serializer = new JsonTypeSerializer(JsonTypeDefinition.fromJson("'string'"), true); for(int chunk = 0;; chunk++) { File indexFile = new File(dir, chunk + ".index"); File dataFile = new File(dir, chunk + ".data"); if(!indexFile.exists() || !dataFile.exists()) break; RandomAccessFile index = new RandomAccessFile(indexFile, "r"); RandomAccessFile data = new RandomAccessFile(dataFile, "r"); position = index.readInt(); data.seek(position); int size = data.readInt(); byte[] value = new byte[size];
public Void invoke(File f, VirtualChannel channel) throws IOException { try (OutputStream os = p.getOut(); OutputStream out = new java.util.zip.GZIPOutputStream(os, 8192); RandomAccessFile raf = new RandomAccessFile(reading(f), "r")) { raf.seek(offset); byte[] buf = new byte[8192]; int len; while ((len = raf.read(buf)) >= 0) { out.write(buf, 0, len); } return null; } } });
RandomAccessFile raf = new RandomAccessFile(file, "rw"); if (skip < 0 || skip > raf.length()) { System.out.println("skip error"); return; raf.setLength(raf.length() + b.length); for (long i = raf.length() - 1; i > b.length + skip - 1; i--) { raf.seek(i - b.length); byte temp = raf.readByte(); raf.seek(i); raf.writeByte(temp); raf.seek(skip); raf.write(b); raf.close(); } catch (Exception e) { e.printStackTrace();
class WidgetList extends AbstractList<Widget> { private static final int SIZE_OF_WIDGET = 100; private final RandomAccessFile file; public WidgetList(RandomAccessFile file) { this.file = file; } @Override public int size() { return (int)(file.length() / SIZE_OF_WIDGET); } @Override public Widget get(int index) { file.seek((long)index * SIZE_OF_WIDGET); byte[] data = new byte[SIZE_OF_WIDGET]; file.read(data); return new Widget(data); } }
/** * Open a stream to a file. * * @param offset If zero, stream the entire log. If positive, read from this byte position onwards. If negative, * read this many bytes from the end of the file. * * @return input supplier for this log, if available from this provider */ public static InputStream streamFile(final File file, final long offset) throws IOException { final RandomAccessFile raf = new RandomAccessFile(file, "r"); final long rafLength = raf.length(); if (offset > 0) { raf.seek(offset); } else if (offset < 0 && offset < rafLength) { raf.seek(Math.max(0, rafLength + offset)); } return Channels.newInputStream(raf.getChannel()); } }
/** */ private void corruptPartition(File partitionsDir) throws IOException { ThreadLocalRandom rand = ThreadLocalRandom.current(); for(File partFile : partitionsDir.listFiles((d, n) -> n.startsWith("part"))) { try (RandomAccessFile raf = new RandomAccessFile(partFile, "rw")) { byte[] buf = new byte[1024]; rand.nextBytes(buf); raf.seek(4096 * 2 + 1); raf.write(buf); } } }
public synchronized void reopen(long lastPosition) throws IOException { if (isClosed) { throw new IOException("Random Access File is closed"); } try { this.raf.close(); } catch (IOException e) { // ignore } this.raf = new RandomAccessFile(file, mode); this.raf.seek(lastPosition); }
RandomAccessFile reader = new RandomAccessFile("/proc/stat", "r"); String load = reader.readLine(); } catch (Exception e) {} reader.seek(0); load = reader.readLine(); reader.close();
try (RandomAccessFile raf = new RandomAccessFile(file, "r")) { long len = raf.length(); raf.seek(pos);
StringBuffer sb = new StringBuffer(); try { endPos = raf.length(); lastPos = endPos; raf.seek(endPos - 1); raf.read(oneByte); if ((char) oneByte[0] != '\n') { numOfLines++; pos = lastPos - BUFFERSIZE; raf.seek(pos); raf.seek(pos); if ((endPos - pos) < BUFFERSIZE) { int remainer = (int) (endPos - pos); info = buildDisplayingHeader(sb.length(), raf.length()); } catch (FileNotFoundException e) { sb = null; try { if (raf != null) { raf.close();
@Test public void testNegativeSizeInHeaderThrows() throws IOException { RandomAccessFile emptyFile = new RandomAccessFile(file, "rwd"); emptyFile.seek(0); emptyFile.writeInt(-2147483648); emptyFile.setLength(INITIAL_LENGTH); emptyFile.getChannel().force(true); emptyFile.close(); try { newQueueFile(); fail("Should have thrown about bad header length"); } catch (IOException ex) { assertThat(ex.getMessage()).isIn( Arrays.asList("File is corrupt; length stored in header (-2147483648) is invalid.", "Unable to read version 0 format. Supported versions are 1 and legacy.")); } }
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.write(centralDirBytes); raf.seek(fc.size() - commentLength - 6); raf.write(temp.array());
LongBuffer revertBuffer() throws IOException { LongBuffer reverted = new LongBuffer(buffer.length); if (bufferSize < buffer.length) { for (int i=0;i<bufferSize;i++) { reverted.writeLong(buffer[bufferSize - 1 - i]); } } else { writeStream.flush(); RandomAccessFile raf = new RandomAccessFile(backingFile,"r"); long offset = raf.length(); while(offset > 0) { offset-=8; raf.seek(offset); reverted.writeLong(raf.readLong()); } } reverted.startReading(); return reverted; }
private int copy(InputStream in, RandomAccessFile out) throws IOException, UpdateError { byte[] buffer = new byte[BUFFER_SIZE]; BufferedInputStream bis = new BufferedInputStream(in, BUFFER_SIZE); try { out.seek(out.length()); int bytes = 0; long previousBlockTime = -1; while (!isCancelled()) { int n = bis.read(buffer, 0, BUFFER_SIZE); if (n == -1) { break; } out.write(buffer, 0, n); bytes += n; checkNetwork(); if (mSpeed != 0) { previousBlockTime = -1; } else if (previousBlockTime == -1) { previousBlockTime = System.currentTimeMillis(); } else if ((System.currentTimeMillis() - previousBlockTime) > TIME_OUT) { throw new UpdateError(UpdateError.DOWNLOAD_NETWORK_TIMEOUT); } } return bytes; } finally { out.close(); bis.close(); in.close(); } }