public void loadDump(File dumpFile, File lineFile, File dumpInfoFile) throws FileNotFoundException, IOException { FileInputStream vertexStream = new FileInputStream(dumpFile); MappedByteBuffer inVertex = vertexStream.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, dumpFile.length()); vertices = inVertex.asFloatBuffer(); FileInputStream vertexLineStream = new FileInputStream(lineFile); MappedByteBuffer inLine; inLine = vertexLineStream.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, lineFile.length()); stripVertexCounts = inLine.asIntBuffer(); Scanner scanner = new Scanner(dumpInfoFile); String line; line = scanner.nextLine(); minX = Float.parseFloat(line); line = scanner.nextLine(); maxX = Float.parseFloat(line); line = scanner.nextLine(); minY = Float.parseFloat(line); line = scanner.nextLine(); maxY = Float.parseFloat(line); }
private static int[] loadRawInteger(String source) throws Exception { System.out.println("Loading data..."); long time = System.nanoTime(); RandomAccessFile raf = new RandomAccessFile(source, "r"); FileChannel inChannel = raf.getChannel(); MappedByteBuffer buffer = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, inChannel.size()); IntBuffer dbuf = buffer.asIntBuffer(); int[] dest = new int[dbuf.remaining()]; dbuf.get(dest); long elapsed = System.nanoTime() - time; System.out.println("Load complete... " + (elapsed / 1000000) + "ms"); raf.close(); return dest; }
// single pass attempt to copy if any can not accept the data then they are skipped // and true will be returned instead of false. private static boolean doingCopy( TapeWriteStage ss, int byteTailPos, int primaryTailPos, int totalPrimaryCopy, int totalBytesCopy) { IntBuffer primaryInts = RingBuffer.wrappedStructuredLayoutRingBuffer(ss.source); ByteBuffer secondaryBytes = RingBuffer.wrappedUnstructuredLayoutRingBufferA(ss.source); primaryInts.position(primaryTailPos); primaryInts.limit( primaryTailPos + totalPrimaryCopy); // TODO: AA, this will not work on the wrap, we must mask and do // muliple copies secondaryBytes.position(byteTailPos); secondaryBytes.limit(byteTailPos + totalBytesCopy); ss.header.clear(); ss.headerAsInts.put(totalBytesCopy + (totalPrimaryCopy << 2)); ss.headerAsInts.put(totalPrimaryCopy << 2); // TODO: must return false if there is no room to write. // TODO: BB, this creates a bit of garbage for the map, perhaps we should map larger blocks // expecting to use them for multiple writes. MappedByteBuffer mapped; try { mapped = ss.fileChannel.map( MapMode.READ_WRITE, ss.fileChannel.position(), 8 + totalBytesCopy + (totalPrimaryCopy << 2)); mapped.put(ss.header); IntBuffer asIntBuffer = mapped.asIntBuffer(); asIntBuffer.position(2); asIntBuffer.put(primaryInts); mapped.position(mapped.position() + (totalPrimaryCopy << 2)); mapped.put(secondaryBytes); } catch (IOException e) { throw new RuntimeException(e); } return true; }
public void readFile(File file) { try { FileChannel channel = new FileInputStream(file).getChannel(); MappedByteBuffer bb = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()); IntBuffer ib = bb.asIntBuffer(); _nx = ib.get(); _ny = ib.get(); _nz = ib.get(); bb.position(4 * ib.position()); DoubleBuffer db = bb.asDoubleBuffer(); if (_a == null || _a.length != _nx * _ny * _nz) _a = new double[_nx * _ny * _nz]; db.get(_a); channel.close(); } catch (IOException e) { System.out.println(e.getMessage()); } }
/** * A regression test for failing to correctly set capacity of underlying wrapped buffer from a * mapped byte buffer. */ public void testasIntBuffer() throws IOException { // Map file FileInputStream fis = new FileInputStream(tmpFile); FileChannel fc = fis.getChannel(); MappedByteBuffer mmb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); int len = mmb.capacity(); assertEquals("Got wrong number of bytes", 46, len); // $NON-NLS-1$ // Read in our 26 bytes for (int i = 0; i < 26; i++) { byte b = mmb.get(); assertEquals("Got wrong byte value", (byte) 'A' + i, b); // $NON-NLS-1$ } // Now convert to an IntBuffer to read our ints IntBuffer ibuffer = mmb.asIntBuffer(); for (int i = 0; i < 5; i++) { int val = ibuffer.get(); assertEquals("Got wrong int value", i + 1, val); // $NON-NLS-1$ } fc.close(); }