Java Code Examples for java.nio.MappedByteBuffer#get()
The following examples show how to use
java.nio.MappedByteBuffer#get() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LongMappedBufferMany.java From jelectrum with MIT License | 6 votes |
public synchronized void getBytes(long position, byte[] buff) { long t1 = System.nanoTime(); //Assert.assertTrue(position >= 0); //Assert.assertTrue(position + buff.length <= total_size); int to_read=buff.length; int start_file = (int) (position / MAP_SIZE); int start_offset = (int) (position % MAP_SIZE); MappedByteBuffer map = map_list.get(start_file); map.position(start_offset); int len = Math.min(to_read, (int) (MAP_SIZE - start_offset)); map.get(buff, 0, len); if (len < to_read) { map = map_list.get(start_file + 1); map.position(0); map.get(buff, len, to_read - len); } TimeRecord.record(t1, "long_map_get_bytes"); }
Example 2
Source File: MapTest.java From openjdk-jdk8u with GNU General Public License v2.0 | 6 votes |
/** * Maps blah file with a random offset and checks to see if data * written out to the file can be read back in */ private static void testWrite() throws Exception { StringBuilder sb = new StringBuilder(); sb.setLength(4); for (int x=0; x<1000; x++) { try (RandomAccessFile raf = new RandomAccessFile(blah, "rw")) { FileChannel fc = raf.getChannel(); long offset = generator.nextInt(1000); MappedByteBuffer b = fc.map(MapMode.READ_WRITE, offset, 100); for (int i=0; i<4; i++) { b.put(i, (byte)('0' + i)); } for (int i=0; i<4; i++) { byte aByte = b.get(i); sb.setCharAt(i, (char)aByte); } if (!sb.toString().equals("0123")) throw new Exception("Write test failed"); } } }
Example 3
Source File: Slopbucket.java From jelectrum with MIT License | 6 votes |
public RecordEntry(long data_loc) { int file = (int) (data_loc / SEGMENT_FILE_SIZE); MappedByteBuffer mbb = getBufferMap(file); int offset = (int) (data_loc % SEGMENT_FILE_SIZE); synchronized(mbb) { mbb.position(offset); max_data_size = mbb.getInt(); int sz = mbb.getShort(); byte[] b = new byte[sz]; mbb.get(b); key = ByteString.copyFrom(b); sz = mbb.getInt(); b = new byte[sz]; mbb.get(b); value = ByteString.copyFrom(b); } this.data_loc = data_loc; }
Example 4
Source File: MapTest.java From jdk8u60 with GNU General Public License v2.0 | 6 votes |
/** * Maps blah file with a random offset and checks to see if data * written out to the file can be read back in */ private static void testWrite() throws Exception { StringBuilder sb = new StringBuilder(); sb.setLength(4); for (int x=0; x<1000; x++) { try (RandomAccessFile raf = new RandomAccessFile(blah, "rw")) { FileChannel fc = raf.getChannel(); long offset = generator.nextInt(1000); MappedByteBuffer b = fc.map(MapMode.READ_WRITE, offset, 100); for (int i=0; i<4; i++) { b.put(i, (byte)('0' + i)); } for (int i=0; i<4; i++) { byte aByte = b.get(i); sb.setCharAt(i, (char)aByte); } if (!sb.toString().equals("0123")) throw new Exception("Write test failed"); } } }
Example 5
Source File: MapTest.java From openjdk-8 with GNU General Public License v2.0 | 6 votes |
/** * Maps blah file with a random offset and checks to see if data * written out to the file can be read back in */ private static void testWrite() throws Exception { StringBuilder sb = new StringBuilder(); sb.setLength(4); for (int x=0; x<1000; x++) { try (RandomAccessFile raf = new RandomAccessFile(blah, "rw")) { FileChannel fc = raf.getChannel(); long offset = generator.nextInt(1000); MappedByteBuffer b = fc.map(MapMode.READ_WRITE, offset, 100); for (int i=0; i<4; i++) { b.put(i, (byte)('0' + i)); } for (int i=0; i<4; i++) { byte aByte = b.get(i); sb.setCharAt(i, (char)aByte); } if (!sb.toString().equals("0123")) throw new Exception("Write test failed"); } } }
Example 6
Source File: Bug1169.java From spotbugs with GNU Lesser General Public License v2.1 | 6 votes |
@NoWarning("RCN,NP") @Nonnull protected <R> R executeEngine(@Nonnull final Engine<R> engine, @Nonnull final Path path) throws IOException { try (final FileChannel c = open(path, StandardOpenOption.READ)) { engine.reset(); final MappedByteBuffer mb = c.map(MapMode.READ_ONLY, 0L, c.size()); final int bufferLength = 8*1024; final byte[] buffer = new byte[bufferLength]; while (mb.hasRemaining()) { final int get = Math.min(mb.remaining(), bufferLength); mb.get(buffer, 0, get); engine.update(buffer, 0, get); } return engine.digest(); } }
Example 7
Source File: FileIOUtils.java From DevUtils with Apache License 2.0 | 6 votes |
/** * 通过 MappedByteBuffer, 读取文件内容, 返回 byte[] * @param file 文件 * @return 文件内容 byte[] */ public static byte[] readFileToBytesByMap(final File file) { if (!FileUtils.isFileExists(file)) return null; FileChannel fc = null; try { fc = new RandomAccessFile(file, "r").getChannel(); int size = (int) fc.size(); MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_ONLY, 0, size).load(); byte[] result = new byte[size]; mbb.get(result, 0, size); return result; } catch (IOException e) { JCLogUtils.eTag(TAG, e, "readFileToBytesByMap"); return null; } finally { CloseUtils.closeIOQuietly(fc); } }
Example 8
Source File: SpillMap.java From phoenix with Apache License 2.0 | 6 votes |
private void pageIn() throws IndexOutOfBoundsException { if (!pagedIn) { // Map the memory region MappedByteBuffer buffer = spillFile.getPage(pageIndex); int numElements = buffer.getInt(); for (int i = 0; i < numElements; i++) { int kvSize = buffer.getInt(); byte[] data = new byte[kvSize]; buffer.get(data, 0, kvSize); try { pageMap.put(SpillManager.getKey(data), data); totalResultSize += (data.length + Bytes.SIZEOF_INT); } catch (IOException ioe) { // Error during key access on spilled resource // TODO rework error handling throw new RuntimeException(ioe); } } pagedIn = true; dirtyPage = false; } }
Example 9
Source File: XdrEncodingFileWriterTest.java From monsoon with BSD 3-Clause "New" or "Revised" License | 6 votes |
@Test public void writeBytes() throws Exception { byte output[] = new byte[bytes.length]; try (FileChannel fd = FileChannel.open(file.toPath(), StandardOpenOption.READ, StandardOpenOption.WRITE)) { try (XdrEncodingFileWriter writer = new XdrEncodingFileWriter(new FileChannelWriter(fd, 0))) { writer.beginEncoding(); writer.xdrEncodeOpaque(bytes, bytes.length); writer.endEncoding(); } MappedByteBuffer map = fd.map(FileChannel.MapMode.READ_ONLY, 0, fd.size()); map.order(ByteOrder.BIG_ENDIAN); map.get(output); assertFalse(map.hasRemaining()); } assertArrayEquals(bytes, output); }
Example 10
Source File: NIODemo2.java From cs-summary-reflection with Apache License 2.0 | 5 votes |
@Test public void test2() throws IOException { long start = System.currentTimeMillis(); // myeclipse2013.exe 1G -> out of memory 因为是直接使用内存 我的机器是4G可用不足 // 使用小的图片测试 FileChannel inChannel = FileChannel.open(Paths.get("g:/复杂度.png"), StandardOpenOption.READ); FileChannel outChannel = FileChannel.open( Paths.get("g:/JAVA_PROJECT/复杂度.png"), StandardOpenOption.WRITE, StandardOpenOption.READ, StandardOpenOption.CREATE); // 内存映射文件 MappedByteBuffer inMappedBuf = inChannel.map(MapMode.READ_ONLY, 0, inChannel.size()); MappedByteBuffer outMappedBuf = outChannel.map(MapMode.READ_WRITE, 0, inChannel.size()); // 直接对缓冲区进行数据的读写操作 byte[] dst = new byte[inMappedBuf.limit()]; inMappedBuf.get(dst); outMappedBuf.put(dst); inChannel.close(); outChannel.close(); long end = System.currentTimeMillis(); System.out.println("耗费时间为:" + (end - start)); }
Example 11
Source File: MultiThreadReader.java From JavaInterview with Apache License 2.0 | 5 votes |
@Override public void run() { try { MappedByteBuffer mapBuffer = rAccessFile.getChannel().map(MapMode.READ_ONLY,start, this.sliceSize); ByteArrayOutputStream bos = new ByteArrayOutputStream(); for(int offset=0;offset<sliceSize;offset+=bufferSize){ int readLength; if(offset+bufferSize<=sliceSize){ readLength = bufferSize; }else{ readLength = (int) (sliceSize-offset); } mapBuffer.get(readBuff, 0, readLength); for(int i=0;i<readLength;i++){ byte tmp = readBuff[i]; if(tmp=='\n' || tmp=='\r'){ handle(bos.toByteArray()); bos.reset(); }else{ bos.write(tmp); } } } if(bos.size()>0){ handle(bos.toByteArray()); } cyclicBarrier.await();//测试性能用 }catch (Exception e) { e.printStackTrace(); } }
Example 12
Source File: BigFileReader.java From basic-tools with MIT License | 5 votes |
@Override public void run() { try { MappedByteBuffer mapBuffer = rAccessFile.getChannel().map(FileChannel.MapMode.READ_ONLY, start, this.sliceSize); ByteArrayOutputStream bos = new ByteArrayOutputStream(); for (int offset = 0; offset < sliceSize; offset += bufferSize) { int readLength; if (offset + bufferSize <= sliceSize) { readLength = bufferSize; } else { readLength = (int) (sliceSize - offset); } mapBuffer.get(readBuff, 0, readLength); for (int i = 0; i < readLength; i++) { byte tmp = readBuff[i]; //碰到换行符 if (tmp == '\n' || tmp == '\r') { handle(bos.toByteArray()); bos.reset(); } else { bos.write(tmp); } } } if (bos.size() > 0) { handle(bos.toByteArray()); } cyclicBarrier.await();//测试性能用 } catch (Exception e) { e.printStackTrace(); } }
Example 13
Source File: FileUtil.java From cc-dbp with Apache License 2.0 | 5 votes |
/** * get an input stream for the file that is fast, trading memory for speed * * @param file * @return InputStream */ public static InputStream fastStream(File file) { FileInputStream fis = null; FileChannel ch = null; byte[] byteArray = null; try { fis = new FileInputStream(file); if (fis != null) { ch = fis.getChannel(); if (ch.size() > 1000000000) { return new BufferedInputStream(fis, BUFFER_SIZE); } MappedByteBuffer mb = ch.map(FileChannel.MapMode.READ_ONLY, 0L, ch.size()); byteArray = new byte[mb.capacity()]; int got; while (mb.hasRemaining()) { got = Math.min(mb.remaining(), byteArray.length); mb.get(byteArray, 0, got); } } } catch (FileNotFoundException fnfe) { throw new IOError(fnfe); } catch (IOException ioe) { throw new IOError(ioe); } finally { if (ch != null) { try { ch.close(); fis.close(); } catch (IOException ioe2) { } } } return new ByteArrayInputStream(byteArray); }
Example 14
Source File: MapTest.java From dragonwell8_jdk with GNU General Public License v2.0 | 5 votes |
/** * Maps blah file with a random offset and checks to see if read * from the ByteBuffer gets the right line number */ private static void testRead() throws Exception { StringBuilder sb = new StringBuilder(); sb.setLength(4); for (int x=0; x<1000; x++) { try (FileInputStream fis = new FileInputStream(blah)) { FileChannel fc = fis.getChannel(); long offset = generator.nextInt(10000); long expectedResult = offset / CHARS_PER_LINE; offset = expectedResult * CHARS_PER_LINE; MappedByteBuffer b = fc.map(MapMode.READ_ONLY, offset, 100); for (int i=0; i<4; i++) { byte aByte = b.get(i); sb.setCharAt(i, (char)aByte); } int result = Integer.parseInt(sb.toString()); if (result != expectedResult) { err.println("I expected "+expectedResult); err.println("I got "+result); throw new Exception("Read test failed"); } } } }
Example 15
Source File: SortMergeJoinPlan.java From phoenix with Apache License 2.0 | 5 votes |
@Override protected Tuple readFromBuffer(MappedByteBuffer buffer) { int length = buffer.getInt(); if (length < 0) return null; byte[] b = new byte[length]; buffer.get(b); Result result = ResultUtil.toResult(new ImmutableBytesWritable(b)); return new ResultTuple(result); }
Example 16
Source File: LongMappedBufferMany.java From jelectrum with MIT License | 5 votes |
public void setBit(long bit) { long t1=System.nanoTime(); long data_pos = bit / 8; int file = (int) (data_pos / MAP_SIZE); int file_offset = (int) (data_pos % MAP_SIZE); int bit_in_byte = (int)(bit % 8); byte[] b = new byte[1]; MappedByteBuffer map = map_list.get(file); b[0]=map.get(file_offset); byte n = (byte)(b[0] | byte_mappings[bit_in_byte]); if (b[0] != n) { map.put(file_offset, n); } //BitSet bs = BitSet.valueOf(b); //if (!bs.get(bit_in_byte)) //{ // bs.set(bit_in_byte); // b = bs.toByteArray(); // map.put(file_offset, b[0]); //} TimeRecord.record(t1, "long_map_set_bit"); }
Example 17
Source File: MapTest.java From TencentKona-8 with GNU General Public License v2.0 | 5 votes |
/** * Maps blah file with a random offset and checks to see if read * from the ByteBuffer gets the right line number */ private static void testRead() throws Exception { StringBuilder sb = new StringBuilder(); sb.setLength(4); for (int x=0; x<1000; x++) { try (FileInputStream fis = new FileInputStream(blah)) { FileChannel fc = fis.getChannel(); long offset = generator.nextInt(10000); long expectedResult = offset / CHARS_PER_LINE; offset = expectedResult * CHARS_PER_LINE; MappedByteBuffer b = fc.map(MapMode.READ_ONLY, offset, 100); for (int i=0; i<4; i++) { byte aByte = b.get(i); sb.setCharAt(i, (char)aByte); } int result = Integer.parseInt(sb.toString()); if (result != expectedResult) { err.println("I expected "+expectedResult); err.println("I got "+result); throw new Exception("Read test failed"); } } } }
Example 18
Source File: MapTest.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
/** * Maps blah file with a random offset and checks to see if read * from the ByteBuffer gets the right line number */ private static void testRead() throws Exception { StringBuilder sb = new StringBuilder(); sb.setLength(4); for (int x=0; x<1000; x++) { try (FileInputStream fis = new FileInputStream(blah)) { FileChannel fc = fis.getChannel(); long offset = generator.nextInt(10000); long expectedResult = offset / CHARS_PER_LINE; offset = expectedResult * CHARS_PER_LINE; MappedByteBuffer b = fc.map(MapMode.READ_ONLY, offset, 100); for (int i=0; i<4; i++) { byte aByte = b.get(i); sb.setCharAt(i, (char)aByte); } int result = Integer.parseInt(sb.toString()); if (result != expectedResult) { err.println("I expected "+expectedResult); err.println("I got "+result); throw new Exception("Read test failed"); } } } }
Example 19
Source File: GeoEngine.java From L2jBrasil with GNU General Public License v3.0 | 4 votes |
public static boolean loadGeodataFile(byte rx, byte ry) { String fname = "./data/geodata/"+rx+"_"+ry+".l2j"; short regionoffset = (short)((rx << 5) + ry); _log.info("Geo Engine: - Loading: "+fname+" -> region offset: "+regionoffset+"X: "+rx+" Y: "+ry); File Geo = new File(fname); int size, index = 0, block = 0, flor = 0; FileChannel roChannel = null; try { // Create a read-only memory-mapped file roChannel = new RandomAccessFile(Geo, "r").getChannel(); size = (int)roChannel.size(); MappedByteBuffer geo; if (Config.FORCE_GEODATA) //Force O/S to Loads this buffer's content into physical memory. //it is not guarantee, because the underlying operating system may have paged out some of the buffer's data geo = roChannel.map(FileChannel.MapMode.READ_ONLY, 0, size).load(); else geo = roChannel.map(FileChannel.MapMode.READ_ONLY, 0, size); geo.order(ByteOrder.LITTLE_ENDIAN); if (size > 196608) { // Indexing geo files, so we will know where each block starts IntBuffer indexs = IntBuffer.allocate(65536); while(block < 65536) { byte type = geo.get(index); indexs.put(block,index); block++; index++; if(type == 0) index += 2; // 1x short else if(type == 1) index += 128; // 64 x short else { int b; for(b=0;b<64;b++) { byte layers = geo.get(index); index += (layers << 1) + 1; if (layers > flor) flor = layers; } } } _geodataIndex.put(regionoffset, indexs); } _geodata.put(regionoffset,geo); _log.info("Geo Engine: - Max Layers: "+flor+" Size: "+size+" Loaded: "+index); } catch (Exception e) { e.printStackTrace(); _log.warning("Failed to Load GeoFile at block: "+block+"\n"); return false; } return true; }
Example 20
Source File: GeoWorldLoader.java From aion-germany with GNU General Public License v3.0 | 4 votes |
@SuppressWarnings("resource") public static Map<String, Spatial> loadMeshs(String fileName) throws IOException { Map<String, Spatial> geoms = new HashMap<String, Spatial>(); File geoFile = new File(fileName); FileChannel roChannel = null; MappedByteBuffer geo = null; roChannel = new RandomAccessFile(geoFile, "r").getChannel(); int size = (int) roChannel.size(); geo = roChannel.map(FileChannel.MapMode.READ_ONLY, 0, size).load(); geo.order(ByteOrder.LITTLE_ENDIAN); while (geo.hasRemaining()) { short namelenght = geo.getShort(); byte[] nameByte = new byte[namelenght]; geo.get(nameByte); String name = new String(nameByte).intern(); Node node = new Node(DEBUG ? name : null); byte intentions = 0; byte singleChildMaterialId = -1; int modelCount = geo.getShort(); for (int c = 0; c < modelCount; c++) { Mesh m = new Mesh(); int vectorCount = (geo.getInt()) * 3; ByteBuffer floatBuffer = ByteBuffer.allocateDirect(vectorCount * 4); FloatBuffer vertices = floatBuffer.asFloatBuffer(); for (int x = 0; x < vectorCount; x++) { vertices.put(geo.getFloat()); } int triangles = geo.getInt(); ByteBuffer shortBuffer = ByteBuffer.allocateDirect(triangles * 2); ShortBuffer indexes = shortBuffer.asShortBuffer(); for (int x = 0; x < triangles; x++) { indexes.put(geo.getShort()); } Geometry geom = null; m.setCollisionFlags(geo.getShort()); if ((m.getIntentions() & CollisionIntention.MOVEABLE.getId()) != 0) { // TODO: skip moveable collisions (ships, shugo boxes), not handled yet continue; } intentions |= m.getIntentions(); m.setBuffer(VertexBuffer.Type.Position, 3, vertices); m.setBuffer(VertexBuffer.Type.Index, 3, indexes); m.createCollisionData(); if ((intentions & CollisionIntention.DOOR.getId()) != 0 && (intentions & CollisionIntention.PHYSICAL.getId()) != 0) { if (!GeoDataConfig.GEO_DOORS_ENABLE) { continue; } geom = new DoorGeometry(name, m); // what if doors have few models ? } else { MaterialTemplate mtl = DataManager.MATERIAL_DATA.getTemplate(m.getMaterialId()); geom = new Geometry(null, m); if (mtl != null || m.getMaterialId() == 11) { node.setName(name); } if (modelCount == 1) { geom.setName(name); singleChildMaterialId = geom.getMaterialId(); } else { geom.setName(("child" + c + "_" + name).intern()); } node.attachChild(geom); } geoms.put(geom.getName(), geom); } node.setCollisionFlags((short) (intentions << 8 | singleChildMaterialId & 0xFF)); if (!node.getChildren().isEmpty()) { geoms.put(name, node); } } destroyDirectByteBuffer(geo); return geoms; }