org.h2gis.functions.io.utility.WriteBufferManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of h2gis Show documentation
Show all versions of h2gis Show documentation
Main module of the H2GIS distribution. It extends H2 by adding spatial storage and analysis
capabilities.
/**
* H2GIS is a library that brings spatial support to the H2 Database Engine
* http://www.h2database.com. H2GIS is developed by CNRS
* http://www.cnrs.fr/.
*
* This code is part of the H2GIS project. H2GIS is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* Lesser General Public License as published by the Free Software Foundation;
* version 3.0 of the License.
*
* H2GIS is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details .
*
*
* For more information, please consult: http://www.h2gis.org/
* or contact directly: info_at_h2gis.org
*/
package org.h2gis.functions.io.utility;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.FileChannel;
/**
* Class to write files using nio.
*
* @author Fernando Gonzalez Cortes
*/
public final class WriteBufferManager {
private static final int BUFFER_SIZE = 1024 * 128;
private FileChannel channel;
private ByteBuffer buffer;
/**
* Creates a new WriteBufferManager that writes to the specified file
* channel
*
* @param channel
* @throws java.io.IOException
*/
public WriteBufferManager(FileChannel channel) throws IOException {
this.channel = channel;
buffer = ByteBuffer.allocate(BUFFER_SIZE);
}
/**
* Puts the specified byte at the current position
*
* @param b
* @throws java.io.IOException
*/
public void putShort(byte b) throws IOException {
prepareToAddBytes(1);
buffer.put(b);
}
public int position(){
return buffer.position();
}
/**
* Moves the window
*
* @param numBytes
* @throws java.io.IOException
*/
private void prepareToAddBytes(int numBytes) throws IOException {
if (buffer.remaining() < numBytes) {
buffer.flip();
channel.write(buffer);
int bufferCapacity = Math.max(BUFFER_SIZE, numBytes);
if (bufferCapacity != buffer.capacity()) {
ByteOrder order = buffer.order();
buffer = ByteBuffer.allocate(bufferCapacity);
buffer.order(order);
} else {
buffer.clear();
}
}
}
/**
* Puts the specified bytes at the current position
*
* @param bs
* @throws java.io.IOException
*/
public void putShort(byte[] bs) throws IOException {
prepareToAddBytes(bs.length);
buffer.put(bs);
}
/**
* flushes the cached contents into the channel. It is mandatory to call
* this method to finish the writing of the channel
*
* @throws java.io.IOException
*/
public void flush() throws IOException {
buffer.flip();
channel.write(buffer);
}
/**
* Specifies the byte order. One of the constants in {@link java.nio.ByteBuffer}
*
* @param order
*/
public void order(ByteOrder order) {
this.buffer.order(order);
}
/**
* Puts the specified int at the current position
*
* @param value
* @throws java.io.IOException
*/
public void putInt(int value) throws IOException {
prepareToAddBytes(4);
buffer.putInt(value);
}
/**
* Puts the specified double at the current position
*
* @param d
* @throws java.io.IOException
*/
public void putDouble(double d) throws IOException {
prepareToAddBytes(8);
buffer.putDouble(d);
}
public void flip() {
buffer.flip();
}
public ByteBuffer getBuffer() {
return buffer;
}
public void putShort(short i) throws IOException {
prepareToAddBytes(2);
buffer.putShort(i);
}
public void putFloat(float value) throws IOException {
prepareToAddBytes(4);
buffer.putFloat(value);
}
public void putLong(long value) throws IOException {
prepareToAddBytes(8);
buffer.putLong(value);
}
public void putBytes(byte[] byteArray) throws IOException {
prepareToAddBytes(byteArray.length);
buffer.put(byteArray);
}
public void putBytes(byte[] byteArray, int offset, int length) throws IOException {
prepareToAddBytes(length);
buffer.put(byteArray, offset, length);
}
public void clear() {
buffer.clear();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy