Refine search
private byte[] getBlock(long offset) throws IOException { byte[] block; if (offset < mappedSize) { block = new byte[blockSize]; buf.position((int)offset); buf.get(block); return block; } else { Long offsetObj = new Long(offset); block = (byte[]) blockCache.get(offsetObj); if (block == null) { block = new byte[blockSize]; data.seek(offset); data.readFully(block); blockCache.put(offsetObj,block); } return block; } }
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); } }
/** * Writes data to the block from an input {@link ByteBuffer}. * * @param offset starting offset of the block file to write * @param inputBuf {@link ByteBuffer} that input data is stored in * @return the size of data that was written */ private long write(long offset, ByteBuffer inputBuf) throws IOException { int inputBufLength = inputBuf.limit() - inputBuf.position(); MappedByteBuffer outputBuf = mLocalFileChannel.map(FileChannel.MapMode.READ_WRITE, offset, inputBufLength); outputBuf.put(inputBuf); int bytesWritten = outputBuf.limit(); BufferUtils.cleanDirectBuffer(outputBuf); return bytesWritten; } }
@Override public RawShadingResult transformToByteBuffer(HgtCache.HgtFileInfo source, int padding) { int axisLength = getAxisLenght(source); int rowLen = axisLength + 1; FileInputStream stream = null; FileChannel channel = null; try { File file = source.getFile(); stream = new FileInputStream(file); channel = stream.getChannel(); MappedByteBuffer map = channel.map(FileChannel.MapMode.READ_ONLY, 0, file.length()); map.order(ByteOrder.BIG_ENDIAN); byte[] bytes = convert(map, axisLength, rowLen, padding); return new RawShadingResult(bytes, axisLength, axisLength, padding); } catch (IOException e) { LOGGER.log(Level.SEVERE, e.getMessage(), e); return null; } finally { if (channel != null) try { channel.close(); } catch (IOException e) { e.printStackTrace(); } if (stream != null) try { stream.close(); } catch (IOException e) { e.printStackTrace(); } } }
/** * Reads a whole file into a StringBuffer based on java.nio * * @param file the file to open. * @throws IOException */ public static byte[] readFileToByteArray(File file) throws IOException { int length = (int) file.length(); MappedByteBuffer in = new FileInputStream(file).getChannel().map( FileChannel.MapMode.READ_ONLY, 0, length); int i = 0; byte[] result = new byte[length]; while (i < length) result[i] = in.get(i++); return result; }
FileInputStream fis = new FileInputStream(next); FileChannel channel = fis.getChannel(); MappedByteBuffer map = channel.map(FileChannel.MapMode.READ_ONLY, 0, fileSize); map.load(); map.get(buffer); queue.put(new WorkItem(path, buffer)); channel.close(); fis.close(); } catch (Exception e) { System.err.println("Could not open " + path + ". " + e.getMessage());
private void loadFileIntoMemory(File xmlFile) throws IOException { FileInputStream fis = new FileInputStream(xmlFile); FileChannel fc = fis.getChannel(); MappedByteBuffer mmb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); xmlFileBuffer = new byte[(int)fc.size()]; mmb.get(xmlFileBuffer); fis.close(); }
public void setData(File f, boolean loadNow) throws IOException { Preconditions.checkArgument(f.length() == rows * columns * 8L, "File " + f + " is wrong length"); for (int i = 0; i < (rows + rowsPerBlock - 1) / rowsPerBlock; i++) { long start = i * rowsPerBlock * columns * 8L; long size = rowsPerBlock * columns * 8L; MappedByteBuffer buf = new FileInputStream(f).getChannel().map(FileChannel.MapMode.READ_ONLY, start, Math.min(f.length() - start, size)); if (loadNow) { buf.load(); } addData(buf.asDoubleBuffer()); } }
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 + kCDECommentLen) & 0xffff; directoryMap.position(currentOffset + kCDELen); directoryMap.get(tempBuf, 0, fileNameLen); directoryMap.position(0); ze.mMethod = directoryMap.getShort(currentOffset + kCDEMethod) & 0xffff; ze.mWhenModified = directoryMap.getInt(currentOffset + kCDEModWhen) & 0xffffffffL; ze.mCRC32 = directoryMap.getLong(currentOffset + kCDECRC) & 0xffffffffL; ze.mCompressedLength = directoryMap.getLong(currentOffset + kCDECompLen) & 0xffffffffL; ze.mUncompressedLength = directoryMap.getLong(currentOffset + kCDEUncompLen) & 0xffffffffL; ze.mLocalHdrOffset = directoryMap.getInt(currentOffset + kCDELocalOffset) & 0xffffffffL;
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) {
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(); }
protected static void start(boolean server) throws Exception { RandomAccessFile file=new RandomAccessFile("/tmp/shm", "rwd"); FileChannel channel=file.getChannel(); MappedByteBuffer shared_buffer=channel.map(FileChannel.MapMode.READ_WRITE,0,SIZE + 10); file.close(); int count=0; // messages written (server) or read (client) int busy_spin=0; byte input=shared_buffer.get(0); shared_buffer.put(buf, 0, buf.length); shared_buffer.rewind(); shared_buffer.put((byte)1).rewind(); count++; if(count % PRINT == 0) while((input=shared_buffer.get(0)) != 0) { if(busy_spin++ < MAX_BUSY_SPIN) byte input=shared_buffer.get(0); shared_buffer.get(buf,0,buf.length); shared_buffer.rewind(); shared_buffer.put((byte)0).rewind(); count++; if(count % PRINT == 0) while((input=shared_buffer.get(0)) != 1) { if(busy_spin++ < MAX_BUSY_SPIN)
@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); } } }
byte[] ret; try { fc = new RandomAccessFile(logFile, "r").getChannel(); fileSize = fc.size(); long position = fileSize - pageSize; try { fout = fc.map(FileChannel.MapMode.READ_ONLY, position, size); fout.get(ret); String str = new String(ret, ConfigExtension.getLogViewEncoding(conf)); return new Pair<>(fileSize, str.getBytes());
RandomAccessFile raf = new RandomAccessFile(FILE_NAME, "rw"); mmappedByteBuffer = raf.getChannel().map(MapMode.READ_WRITE, 0L, raf.length()); mmappedByteBuffer.load();
public StoreCheckpoint(final String scpPath) throws IOException { File file = new File(scpPath); MappedFile.ensureDirOK(file.getParent()); boolean fileExists = file.exists(); this.randomAccessFile = new RandomAccessFile(file, "rw"); this.fileChannel = this.randomAccessFile.getChannel(); this.mappedByteBuffer = fileChannel.map(MapMode.READ_WRITE, 0, MappedFile.OS_PAGE_SIZE); if (fileExists) { log.info("store checkpoint file exists, " + scpPath); this.physicMsgTimestamp = this.mappedByteBuffer.getLong(0); this.logicsMsgTimestamp = this.mappedByteBuffer.getLong(8); this.indexMsgTimestamp = this.mappedByteBuffer.getLong(16); log.info("store checkpoint file physicMsgTimestamp " + this.physicMsgTimestamp + ", " + UtilAll.timeMillisToHumanString(this.physicMsgTimestamp)); log.info("store checkpoint file logicsMsgTimestamp " + this.logicsMsgTimestamp + ", " + UtilAll.timeMillisToHumanString(this.logicsMsgTimestamp)); log.info("store checkpoint file indexMsgTimestamp " + this.indexMsgTimestamp + ", " + UtilAll.timeMillisToHumanString(this.indexMsgTimestamp)); } else { log.info("store checkpoint file not exists, " + scpPath); } }
static PinotByteBuffer mapFile(File file, boolean readOnly, long offset, int size, ByteOrder byteOrder) throws IOException { if (readOnly) { try (FileChannel fileChannel = new RandomAccessFile(file, "r").getChannel()) { ByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, offset, size).order(byteOrder); return new PinotByteBuffer(buffer, true, false); } } else { try (FileChannel fileChannel = new RandomAccessFile(file, "rw").getChannel()) { ByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, offset, size).order(byteOrder); return new PinotByteBuffer(buffer, true, true); } } }