本文整理汇总了Java中java.nio.MappedByteBuffer.position方法的典型用法代码示例。如果您正苦于以下问题:Java MappedByteBuffer.position方法的具体用法?Java MappedByteBuffer.position怎么用?Java MappedByteBuffer.position使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.MappedByteBuffer
的用法示例。
在下文中一共展示了MappedByteBuffer.position方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: screenshot
import java.nio.MappedByteBuffer; //导入方法依赖的package包/类
/**
* Takes a screenshot of the current frame. This method is
* entirely copied from http://www.javagaming.org/forums/index.php?topic=8747.0
* @param gl FengGUIs opengl interface
* @param width the width of the screenshot
* @param height the height of the screenhost
* @param file the file where to store the screenshot
*/
private void screenshot(IOpenGL gl, int width, int height, File file)
{
try
{
RandomAccessFile out = new RandomAccessFile(file, "rw");
FileChannel ch = out.getChannel();
int fileLength = TARGA_HEADER_SIZE + width * height * 3;
out.setLength(fileLength);
MappedByteBuffer image = ch.map(FileChannel.MapMode.READ_WRITE, 0, fileLength);
// write the TARGA header
image.put(0, (byte) 0).put(1, (byte) 0);
image.put(2, (byte) 2); // uncompressed type
image.put(12, (byte) (width & 0xFF)); // width
image.put(13, (byte) (width >> 8)); // width
image.put(14, (byte) (height & 0xFF));// height
image.put(15, (byte) (height >> 8));// height
image.put(16, (byte) 24); // pixel size
// go to image data position
image.position(TARGA_HEADER_SIZE);
// jogl needs a sliced buffer
ByteBuffer bgr = image.slice();
// read the BGR values into the image buffer
gl.readPixels(0, 0, width, height, bgr);
// close the file channel
ch.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
示例2: insertTagAndShiftViaMappedByteBuffer
import java.nio.MappedByteBuffer; //导入方法依赖的package包/类
/**
* Insert new metadata into file by using memory mapped file
* <p>
* But this is problematic on 32bit systems for large flac files may not be able to map a contiguous address space large enough
* for a large audio size , so no longer used
*
* @param tag
* @param mappedFile
* @param fc
* @param targetSizeBeforeAudioData
* @param totalTargetSize
* @param blockInfo
* @param flacStream
* @param neededRoom
* @param availableRoom
* @throws IOException
* @throws UnsupportedEncodingException
*/
private void insertTagAndShiftViaMappedByteBuffer(Tag tag, MappedByteBuffer mappedFile, FileChannel fc, long targetSizeBeforeAudioData, long totalTargetSize, MetadataBlockInfo blockInfo, FlacStreamReader flacStream, int neededRoom, int availableRoom) throws IOException, UnsupportedEncodingException {
//Find end of metadata blacks (start of Audio)
int currentEndOfFilePosition = safeLongToInt(fc.size());
/*
* First shift data to the 'right' of the tag to the end of the file, whose position is currentEndOfTagsPosition
*/
int currentEndOfTagsPosition = safeLongToInt((targetSizeBeforeAudioData - FlacTagCreator.DEFAULT_PADDING) - neededRoom + availableRoom);
int lengthDiff = safeLongToInt(totalTargetSize - currentEndOfFilePosition);
final int BLOCK_SIZE = safeLongToInt(TagOptionSingleton.getInstance().getWriteChunkSize());
int currentPos = currentEndOfFilePosition - BLOCK_SIZE;
byte[] buffer = new byte[BLOCK_SIZE];
for (; currentPos >= currentEndOfTagsPosition; currentPos -= BLOCK_SIZE) {
mappedFile.position(currentPos);
mappedFile.get(buffer, 0, BLOCK_SIZE);
mappedFile.position(currentPos + lengthDiff);
mappedFile.put(buffer, 0, BLOCK_SIZE);
}
/*
* Final movement of start bytes. This also covers cases where BLOCK_SIZE is larger than the audio data
*/
int remainder = (currentPos + BLOCK_SIZE) - currentEndOfTagsPosition;
if (remainder > 0) {
mappedFile.position(currentEndOfTagsPosition);
mappedFile.get(buffer, 0, remainder);
mappedFile.position(currentEndOfTagsPosition + lengthDiff);
mappedFile.put(buffer, 0, remainder);
}
DirectByteBufferUtils.release(mappedFile);
/* Now overwrite the tag */
writeTags(tag, fc, blockInfo, flacStream);
}
示例3: length
import java.nio.MappedByteBuffer; //导入方法依赖的package包/类
/**
* Compute lenght of this sequence - quite expensive operation, indeed.
*/
@Override
public int length() {
if (length != -1) {
return length;
}
long start = System.currentTimeMillis();
int charactersRead = 0;
long bytesRead = 0;
MappedByteBuffer mappedByteBuffer = null;
CharBuffer charBuffer = CharBuffer.allocate(SIZE_LIMIT);
CharsetDecoder decoder = prepareDecoder(charset);
decoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
try {
while (bytesRead < fileSize) {
mappedByteBuffer = fileChannel.map(
FileChannel.MapMode.READ_ONLY, bytesRead,
Math.min(SIZE_LIMIT, fileSize - bytesRead));
CoderResult result;
do {
charBuffer.clear();
result = decoder.decode(
mappedByteBuffer, charBuffer,
bytesRead + SIZE_LIMIT >= fileSize);
if (result.isUnmappable() || result.isMalformed()
|| result.isError()) {
throw new IOException("Error decoding file: "
+ result.toString() + " ");
}
if (bytesRead + SIZE_LIMIT >= fileSize) {
LOG.info("Coding end");
}
charactersRead += charBuffer.position();
} while (result.isOverflow());
int readNow = mappedByteBuffer.position();
bytesRead += readNow;
unmap(mappedByteBuffer);
}
charBuffer.clear();
boolean repeat;
do {
repeat = decoder.flush(charBuffer).isOverflow();
charactersRead += charBuffer.position();
charBuffer.clear();
} while (repeat);
} catch (IOException ex) {
if (mappedByteBuffer != null) {
unmap(mappedByteBuffer);
}
Exceptions.printStackTrace(ex);
}
length = charactersRead;
LOG.log(Level.INFO, "Length computed in {0} ms.", //NOI18N
System.currentTimeMillis() - start);
return length;
}
示例4: charAt
import java.nio.MappedByteBuffer; //导入方法依赖的package包/类
@Override
public char charAt(int index) {
if (index < lastIndex) {
returns++;
}
lastIndex = index;
if (index > length()) {
throw new IndexOutOfBoundsException();
}
if (isInBuffer(index)) {
return getFromBuffer(index);
} else {
if (index < currentStart || currentStart == -1) {
reset();
}
retrieves++;
MappedByteBuffer mappedByteBuffer = null;
try {
while (readBytes < fileSize) {
try {
mappedByteBuffer = fileChannel.map(
FileChannel.MapMode.READ_ONLY,
readBytes,
Math.min(SIZE_LIMIT, fileSize - readBytes));
maps++;
CoderResult result;
do {
currentStart = currentStart == -1 ? 0
: currentStart + currentBuffer.limit();
currentBuffer.clear();
result = currentDecoder.decode(mappedByteBuffer,
currentBuffer,
readBytes + SIZE_LIMIT >= fileSize);
currentBuffer.flip();
int readChars = currentBuffer.limit();
if (currentStart + readChars > index) {
return getFromBuffer(index);
}
if (result.isUnmappable() || result.isMalformed()
|| result.isError()) {
throw new IOException("Error decoding file: "
+ result.toString() + " ");
}
} while (result.isOverflow());
} finally {
if (mappedByteBuffer != null) {
int readNow = mappedByteBuffer.position();
readBytes += readNow;
unmap(mappedByteBuffer);
}
}
}
boolean repeat;
do {
repeat = currentDecoder.flush(currentBuffer).isOverflow();
int size = currentBuffer.position();
if (size + currentStart > index) {
currentBuffer.flip();
return currentBuffer.get(index - currentStart);
}
currentBuffer.clear();
currentStart += size;
} while (repeat);
} catch (IOException ex) {
if (mappedByteBuffer != null) {
unmap(mappedByteBuffer);
}
Exceptions.printStackTrace(ex);
}
}
throw new IllegalStateException(
"Cannot get character."); //NOI18N
}
示例5: get
import java.nio.MappedByteBuffer; //导入方法依赖的package包/类
synchronized void get(long position, byte[] chars) {
MappedByteBuffer buffer = dumpBuffer[getBufferIndex(position)];
buffer.position(getBufferOffset(position));
buffer.get(chars);
}