private static MappedByteBuffer mapInternal(File file, MapMode mode, long size) throws IOException { checkNotNull(file); checkNotNull(mode); Closer closer = Closer.create(); try { RandomAccessFile raf = closer.register(new RandomAccessFile(file, mode == MapMode.READ_ONLY ? "r" : "rw")); FileChannel channel = closer.register(raf.getChannel()); return channel.map(mode, 0, size == -1 ? channel.size() : size); } catch (Throwable e) { throw closer.rethrow(e); } finally { closer.close(); } }
private byte[] readFile(File file) throws IOException { RandomAccessFile raf = new RandomAccessFile(file, "r"); byte[] buffer = new byte[(int) raf.length()]; raf.readFully(buffer); raf.close(); return buffer; }
public void open(File outputFile) throws IOException { outputFile.mkdirs(); if (outputFile.exists()) { outputFile.delete(); } outputFile.createNewFile(); outputRAF = new RandomAccessFile(outputFile, "rw"); outputRAF.setLength(mappedFileSizeBytes); outputRAF.seek(mappedFileSizeBytes - 1); outputRAF.writeByte(0); outputRAF.seek(0); outputChannel = outputRAF.getChannel(); fileBuffer = outputChannel.map(FileChannel.MapMode.READ_WRITE, 0, mappedFileSizeBytes); }
private int nextId() throws IOException { if (raf.length() - raf.getFilePointer() >= 4) { int id = raf.readInt(); return id < 0 ? id : table[id]; } return -2; }
public void skip(long start) throws IOException { file.seek(file.getFilePointer()+start); }
void addPatchFile(String zipFileName) throws IOException { File file = new File(zipFileName); RandomAccessFile f = new RandomAccessFile(file, "r"); long fileLength = f.length(); f.close(); throw new java.io.IOException(); f.seek(0); f.seek(searchStart); ByteBuffer bbuf = ByteBuffer.allocate((int) readAmount); byte[] buffer = bbuf.array(); f.readFully(buffer); bbuf.order(ByteOrder.LITTLE_ENDIAN); MappedByteBuffer directoryMap = f.getChannel().map( FileChannel.MapMode.READ_ONLY, dirOffset, dirSize); directoryMap.order(ByteOrder.LITTLE_ENDIAN); if (directoryMap.getInt(currentOffset) != kCDESignature) { Log.w(LOG_TAG, "Missed a central dir sig (at " + currentOffset + ")"); .getShort(currentOffset + kCDENameLen) & 0xffff; int extraLen = directoryMap.getShort(currentOffset + kCDEExtraLen) & 0xffff; int commentLen = directoryMap.getShort(currentOffset
public static void main(String[] args) throws IOException { // trying FileLock mechanics in different processes File file = new File("tmp.lock"); file.createNewFile(); FileChannel channel = new RandomAccessFile(file, "r").getChannel(); boolean shared = true; FileLock lock1 = channel.tryLock(0, Long.MAX_VALUE, shared); System.out.println("locked " + lock1); System.in.read(); System.out.println("release " + lock1); lock1.release(); }
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(); }
public synchronized void writeToConfigXmlFile(String content) { FileChannel channel = null; FileOutputStream outputStream = null; FileLock lock = null; try { RandomAccessFile randomAccessFile = new RandomAccessFile(fileLocation(), "rw"); channel = randomAccessFile.getChannel(); lock = channel.lock(); randomAccessFile.seek(0); randomAccessFile.setLength(0); outputStream = new FileOutputStream(randomAccessFile.getFD()); IOUtils.write(content, outputStream, UTF_8); } catch (Exception e) { throw new RuntimeException(e); } finally { if (channel != null && lock != null) { try { lock.release(); channel.close(); IOUtils.closeQuietly(outputStream); } catch (IOException e) { LOGGER.error("Error occured when releasing file lock and closing file.", e); } } } }
raf = new RandomAccessFile(file, "r"); channel = raf.getChannel(); return channel.map(FileChannel.MapMode.READ_ONLY, 0, fileLength).load(); } finally { if (channel != null) { try { channel.close(); } catch (IOException e) { raf.close(); } catch (IOException e) {
public ZipFile(File fd) throws IOException { RandomAccessFile randomAccessFile = new RandomAccessFile(fd, "r"); file = randomAccessFile; raf = randomAccessFile.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, fd.length()); readCentralDir(); }
try { // Get a file channel for the file File file = new File("filename"); FileChannel channel = new RandomAccessFile(file, "rw").getChannel(); // Use the file channel to create a lock on the file. // This method blocks until it can retrieve the lock. FileLock lock = channel.lock(); /* use channel.lock OR channel.tryLock(); */ // Try acquiring the lock without blocking. This method returns // null or throws an exception if the file is already locked. try { lock = channel.tryLock(); } catch (OverlappingFileLockException e) { // File is already locked in this thread or virtual machine } // Release the lock - if it is not null! if( lock != null ) { lock.release(); } // Close the file channel.close(); } catch (Exception e) { }
try { int shmSize = 1024; RandomAccessFile file = new RandomAccessFile("shm.raw","rw"); // inialize file size if(file.length() < shmSize) { byte[] tmp = new byte[shmSize]; file.write(tmp); file.seek(0); // seek back to start of file. } // memory-map file. FileChannel ch = file.getChannel(); MappedByteBuffer shm = ch.map(FileChannel.MapMode.READ_WRITE, 0, shmSize); ch.close(); // channel not needed anymore. shm.load(); // force file into physical memory. // now use the ByteBuffer's get/put/position methods to read/write the shared memory } catch(Exception e) { e.printStackTrace(); }
public static MappedByteBuffer expandBuffer(String filename, MappedByteBuffer buffer, long newSize) { buffer.force(); int oldPosition = buffer.position(); try (RandomAccessFile raf = new RandomAccessFile(filename, "rw")) { raf.setLength(newSize); buffer = raf.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, raf.length()); buffer.position(oldPosition); return buffer; } catch (IOException e) { throw new RuntimeException(e); } }
@Override public void run() { try { RandomAccessFile raf = new RandomAccessFile(file, "rw"); System.out.println("Getting lock (parallel thread)..."); FileLock lock = raf.getChannel().lock(); System.out.println("Obtained lock (parallel tread): " + lock); lock.release(); } catch (Throwable e) { e.printStackTrace(); } } });
public void writeToFileNIOWay2(File file) throws IOException { final int numberOfIterations = 1000000; final String messageToWrite = "This is a test üüüüüüööööö"; final byte[] messageBytes = messageToWrite. getBytes(Charset.forName("ISO-8859-1")); final long appendSize = numberOfIterations * messageBytes.length; final RandomAccessFile raf = new RandomAccessFile(file, "rw"); raf.seek(raf.length()); final FileChannel fc = raf.getChannel(); final MappedByteBuffer mbf = fc.map(FileChannel.MapMode.READ_WRITE, fc. position(), appendSize); fc.close(); for (int i = 1; i < numberOfIterations; i++) { mbf.put(messageBytes); } }
public long length () { try { if (!exists()) { return 0; } RandomAccessFile raf = new RandomAccessFile(this, "r"); long len = raf.length(); raf.close(); return len; } catch (IOException e) { return 0; } }
@Override public DmaIdList build(String filename) { try { File file = new File(filename); if (!file.exists()) { //noinspection ResultOfMethodCallIgnored file.getParentFile().mkdirs(); //noinspection ResultOfMethodCallIgnored file.createNewFile(); try (RandomAccessFile raf = new RandomAccessFile(new File(filename), "rw")) { raf.setLength(DmaIdList.META_DATA_SIZE + defaultCapacity * DmaIdList.SIZE_OF_DATA); } } try (RandomAccessFile raf = new RandomAccessFile(new File(filename), "rw")) { FileChannel channel = raf.getChannel(); MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, raf.length()); int numRecords = buffer.getInt(); int capacity = (int) (raf.length() - DmaIdList.META_DATA_SIZE) / DmaIdList.SIZE_OF_DATA; buffer.position(DmaIdList.META_DATA_SIZE + numRecords * DmaIdList.SIZE_OF_DATA); return new DmaIdList(filename, buffer, numRecords, capacity); } } catch (IOException e) { throw new RuntimeException(e); } } }
RandomAccessFile raf = new RandomAccessFile(FILE_NAME, "rw"); mmappedByteBuffer = raf.getChannel().map(MapMode.READ_WRITE, 0L, raf.length()); mmappedByteBuffer.load();