/** Allocate some buffers for writing. */ private void allocateBuffers() { writeBuffer = NIOUtilities.allocate(HEADER_SIZE + RECORD_SIZE * 1024); }
/** Make sure our buffer is of size. */ private void checkShapeBuffer(int size) { if (shapeBuffer.capacity() < size) { if (shapeBuffer != null) NIOUtilities.clean(shapeBuffer, false); shapeBuffer = NIOUtilities.allocate(size); } }
private void writeHeader() throws IOException { ByteBuffer buffer = NIOUtilities.allocate(HEADER_SIZE); try { buffer.put((byte) 1); buffer.putLong(recordIndex); buffer.putInt(removes); buffer.flip(); channel.write(buffer, 0); } finally { NIOUtilities.clean(buffer, false); } }
public ScrollingBuffer(FileChannel channel, ByteOrder order, boolean useMemoryMapping) throws IOException { this.channel = channel; this.order = order; this.useMemoryMapping = useMemoryMapping; this.bufferStart = channel.position(); if (useMemoryMapping) { this.buffer = channel.map( MapMode.READ_ONLY, channel.position(), channel.size() - channel.position()); this.buffer.order(order); } else { // start with an 8kb buffer this.buffer = NIOUtilities.allocate(8 * 1024); this.buffer.order(order); channel.read(buffer); buffer.flip(); } }
private ByteBuffer ensureCapacity(ByteBuffer buffer, int size, boolean useMemoryMappedBuffer) { // This sucks if you accidentally pass is a MemoryMappedBuffer of size // 80M // like I did while messing around, within moments I had 1 gig of // swap... if (buffer.isReadOnly() || useMemoryMappedBuffer) { return buffer; } int limit = buffer.limit(); while (limit < size) { limit *= 2; } if (limit != buffer.limit()) { // clean up the old buffer and allocate a new one buffer = NIOUtilities.allocate(limit); } return buffer; }
/** * @param requiredSize * @throws IOException */ void refillBuffer(int requiredSize) throws IOException { // compute the actual position up to we have read something long currentPosition = bufferStart + buffer.position(); // if the buffer is not big enough enlarge it if (buffer.capacity() < requiredSize) { int size = buffer.capacity(); while (size < requiredSize) size *= 2; buffer = NIOUtilities.allocate(size); buffer.order(order); } readBuffer(currentPosition); }
private void init(ShpFiles shpFiles, ReadableByteChannel in) throws IOException { this.typeName = shpFiles.getTypeName() + "."; this.fidBuilder = new StringBuilder(typeName); this.readChannel = in; streamLogger.open(); getHeader(shpFiles); buffer = NIOUtilities.allocate(IndexedFidWriter.RECORD_SIZE * 1024); buffer.position(buffer.limit()); }
private void readHeader(ReadableByteChannel channel) throws IOException { ByteBuffer buffer = NIOUtilities.allocate(100); try { while (buffer.remaining() > 0) { channel.read(buffer); } buffer.flip(); header = new ShapefileHeader(); header.read(buffer, true); } finally { NIOUtilities.clean(buffer, false); } }
private void readRecords(ReadableByteChannel channel) throws IOException { check(); int remaining = (header.getFileLength() * 2) - 100; ByteBuffer buffer = NIOUtilities.allocate(remaining); try { buffer.order(ByteOrder.BIG_ENDIAN); while (buffer.remaining() > 0) { channel.read(buffer); } buffer.flip(); int records = remaining / 4; content = new int[records]; IntBuffer ints = buffer.asIntBuffer(); ints.get(content); } finally { NIOUtilities.clean(buffer, false); } }
headerLength = MINIMUM_HEADER; ByteBuffer buffer = NIOUtilities.allocate(headerLength); try { buffer.order(ByteOrder.LITTLE_ENDIAN);
private void init(boolean strict, GeometryFactory gf) throws IOException, ShapefileException { geometryFactory = gf; if (channel instanceof FileChannel && useMemoryMappedBuffer) { FileChannel fc = (FileChannel) channel; buffer = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); buffer.position(0); this.currentOffset = 0; } else { // force useMemoryMappedBuffer to false this.useMemoryMappedBuffer = false; // start small buffer = NIOUtilities.allocate(1024); fill(buffer, channel); buffer.flip(); this.currentOffset = 0; } header = new ShapefileHeader(); header.read(buffer, strict); fileShapeType = header.getShapeType(); handler = fileShapeType.getShapeHandler(gf); if (handler == null) { throw new IOException("Unsuported shape type:" + fileShapeType); } headerTransfer = ByteBuffer.allocate(8); headerTransfer.order(ByteOrder.BIG_ENDIAN); // make sure the record end is set now... record.end = this.toFileOffset(buffer.position()); }
private void getHeader(ShpFiles shpFiles) throws IOException { ByteBuffer buffer = NIOUtilities.allocate(IndexedFidWriter.HEADER_SIZE); try { ShapefileReader.fill(buffer, readChannel); if (buffer.position() == 0) { done = true; count = 0; return; } buffer.position(0); byte version = buffer.get(); if (version != 1) { throw new IOException( "File is not of a compatible version for this reader or file is corrupt."); } this.count = buffer.getLong(); this.removes = buffer.getInt(); if (removes > getCount() / 2) { URL url = shpFiles.acquireRead(FIX, this); try { URLs.urlToFile(url).deleteOnExit(); } finally { shpFiles.unlockRead(url, this); } } } finally { NIOUtilities.clean(buffer, false); } }
} else { LOGGER.finest("Reading from file..."); this.buf = NIOUtilities.allocate(8 * RECS_IN_BUFFER); channel.read(buf); buf.flip();
buffer = NIOUtilities.allocate(header.getRecordLength());
Arrays.fill(nullValues[i], (byte) nullChar); buffer = NIOUtilities.allocate(header.getRecordLength());