本文整理汇总了Java中java.nio.MappedByteBuffer.put方法的典型用法代码示例。如果您正苦于以下问题:Java MappedByteBuffer.put方法的具体用法?Java MappedByteBuffer.put怎么用?Java MappedByteBuffer.put使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.MappedByteBuffer
的用法示例。
在下文中一共展示了MappedByteBuffer.put方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: writeFileFromBytesByMap
import java.nio.MappedByteBuffer; //导入方法依赖的package包/类
/**
* 将字节数组写入文件
*
* @param file 文件
* @param bytes 字节数组
* @param append 是否追加在文件末
* @param isForce 是否写入文件
* @return {@code true}: 写入成功<br>{@code false}: 写入失败
*/
public static boolean writeFileFromBytesByMap(final File file, final byte[] bytes, final boolean append, final boolean isForce) {
if (bytes == null || !createOrExistsFile(file)) return false;
FileChannel fc = null;
try {
fc = new FileOutputStream(file, append).getChannel();
MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_WRITE, fc.size(), bytes.length);
mbb.put(bytes);
if (isForce) mbb.force();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
CloseUtils.closeIO(fc);
}
}
示例2: testMap_readWrite_creates
import java.nio.MappedByteBuffer; //导入方法依赖的package包/类
public void testMap_readWrite_creates() throws IOException {
// Test data
int size = 1024;
byte[] expectedBytes = newPreFilledByteArray(1024);
// Setup
File file = createTempFile();
boolean deleted = file.delete();
assertTrue(deleted);
assertFalse(file.exists());
// Test
MappedByteBuffer map = Files.map(file, MapMode.READ_WRITE, size);
map.put(expectedBytes);
// Verify
assertTrue(file.exists());
assertTrue(file.isFile());
assertEquals(size, file.length());
byte[] actualBytes = Files.toByteArray(file);
assertTrue(Arrays.equals(expectedBytes, actualBytes));
}
示例3: testMap_readWrite
import java.nio.MappedByteBuffer; //导入方法依赖的package包/类
public void testMap_readWrite() throws IOException {
// Test data
int size = 1024;
byte[] expectedBytes = new byte[size];
byte[] bytes = newPreFilledByteArray(1024);
// Setup
File file = createTempFile();
Files.write(bytes, file);
Random random = new Random();
random.nextBytes(expectedBytes);
// Test
MappedByteBuffer map = Files.map(file, MapMode.READ_WRITE);
map.put(expectedBytes);
// Verify
byte[] actualBytes = Files.toByteArray(file);
assertTrue(Arrays.equals(expectedBytes, actualBytes));
}
示例4: writeFileFromBytesByMap
import java.nio.MappedByteBuffer; //导入方法依赖的package包/类
/**
* 将字节数组写入文件
*
* @param file 文件
* @param bytes 字节数组
* @param append 是否追加在文件末
* @param isForce 是否写入文件
* @return {@code true}: 写入成功<br>{@code false}: 写入失败
*/
public static boolean writeFileFromBytesByMap(File file, final byte[] bytes, boolean append, boolean isForce) {
if (bytes == null || !FileUtils.createOrExistsFile(file)) return false;
FileChannel fc = null;
try {
fc = new FileOutputStream(file, append).getChannel();
MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_WRITE, fc.size(), bytes.length);
mbb.put(bytes);
if (isForce) mbb.force();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
CloseUtils.closeIO(fc);
}
}
示例5: writeFileFromBytesByMap
import java.nio.MappedByteBuffer; //导入方法依赖的package包/类
/**
* 将字节数组写入文件
*
* @param file 文件
* @param bytes 字节数组
* @param append 是否追加在文件末
* @param isForce 是否写入文件
* @return {@code true}: 写入成功<br>{@code false}: 写入失败
*/
public static boolean writeFileFromBytesByMap(final File file, final byte[] bytes, final boolean append, final boolean isForce) {
if (bytes == null || !createOrExistsFile(file)) {
return false;
}
FileChannel fc = null;
try {
fc = new FileOutputStream(file, append).getChannel();
MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_WRITE, fc.size(), bytes.length);
mbb.put(bytes);
if (isForce) {
mbb.force();
}
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
CloseUtils.closeIO(fc);
}
}
示例6: testWrite
import java.nio.MappedByteBuffer; //导入方法依赖的package包/类
/**
* 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");
}
}
}
示例7: uploadFileByMappedByteBuffer
import java.nio.MappedByteBuffer; //导入方法依赖的package包/类
@Override
public void uploadFileByMappedByteBuffer(MultipartFileParam param) throws IOException {
String fileName = param.getName();
String uploadDirPath = finalDirPath + param.getMd5();
String tempFileName = fileName + "_tmp";
File tmpDir = new File(uploadDirPath);
File tmpFile = new File(uploadDirPath, tempFileName);
if (!tmpDir.exists()) {
tmpDir.mkdirs();
}
RandomAccessFile tempRaf = new RandomAccessFile(tmpFile, "rw");
FileChannel fileChannel = tempRaf.getChannel();
//写入该分片数据
long offset = CHUNK_SIZE * param.getChunk();
byte[] fileData = param.getFile().getBytes();
MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, offset, fileData.length);
mappedByteBuffer.put(fileData);
// 释放
FileMD5Util.freedMappedByteBuffer(mappedByteBuffer);
fileChannel.close();
boolean isOk = checkAndSetUploadProgress(param, uploadDirPath);
if (isOk) {
boolean flag = renameFile(tmpFile, fileName);
System.out.println("upload complete !!" + flag + " name=" + fileName);
}
}
示例8: wipeFile
import java.nio.MappedByteBuffer; //导入方法依赖的package包/类
public static void wipeFile(String file2wipe) throws IOException, FileNotFoundException
{
File f2w = new File(file2wipe);
if (f2w.exists())
{
SecureRandom sr = new SecureRandom();
RandomAccessFile raf = new RandomAccessFile(f2w, "rw");
FileChannel channel = raf.getChannel();
MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, raf.length());
while (buffer.hasRemaining())
{
buffer.put((byte) 0);
}
buffer.force();
buffer.rewind();
while (buffer.hasRemaining())
{
buffer.put((byte) 0xFF);
}
buffer.force();
buffer.rewind();
byte[] data = new byte[1];
while (buffer.hasRemaining())
{
sr.nextBytes(data);
buffer.put(data[0]);
}
buffer.force();
raf.close();
f2w.delete();
}
}
示例9: main
import java.nio.MappedByteBuffer; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception{
fc=
new RandomAccessFile("./src/main/java/io/source/data","rw").getChannel();
MappedByteBuffer out=
fc.map(FileChannel.MapMode.READ_WRITE,0,LENGTH);
for(int i=0;i<LENGTH;i++){
out.put((byte)'x');
}
new LockAndModify(out,0,0+LENGTH/3);
new LockAndModify(out,LENGTH/2,LENGTH/2+LENGTH/4);
}
示例10: mmap_write
import java.nio.MappedByteBuffer; //导入方法依赖的package包/类
static void mmap_write() throws Exception {
RandomAccessFile file = new RandomAccessFile("/dev/shm/cache", "rw");
MappedByteBuffer out = file.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 100);
String stub = "deadbeaf";
out.put(stub.getBytes());
file.close();
}
示例11: 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();
}
}
示例12: writeToCache
import java.nio.MappedByteBuffer; //导入方法依赖的package包/类
/**
* 把responseBody的字节读入path文件中
*
* @param responseBody
* @param path
* @param readLength
* @param totalLength
*/
public static void writeToCache(ResponseBody responseBody, String path, long readLength, long totalLength) {
try {
File file = newFile(path);
long writeLength;
if (totalLength == 0) {
writeLength = responseBody.contentLength();
} else {
writeLength = totalLength;
}
FileChannel fileChannel;
RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rwd");
fileChannel = randomAccessFile.getChannel();
MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, readLength, writeLength);
byte[] buffer = new byte[1024 * 8];
int length = 0;
while ((length = responseBody.byteStream().read(buffer)) != -1) {
mappedByteBuffer.put(buffer, 0, length);
}
responseBody.byteStream().close();
if (fileChannel != null) {
fileChannel.close();
}
if (randomAccessFile != null) {
randomAccessFile.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
示例13: writeToCache
import java.nio.MappedByteBuffer; //导入方法依赖的package包/类
public static void writeToCache(ResponseBody responseBody, String savedFilePath, long readLength, long totalLength) {
try {
File file = newFile(savedFilePath);
long writeLength;
if (totalLength == 0) {
writeLength = responseBody.contentLength();
} else {
writeLength = totalLength;
}
FileChannel fileChannel;
RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rwd");
fileChannel = randomAccessFile.getChannel();
MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, readLength, writeLength);
byte[] buffer = new byte[1024 * 8];
int length = 0;
while ((length = responseBody.byteStream().read(buffer)) != -1) {
mappedByteBuffer.put(buffer, 0, length);
}
responseBody.byteStream().close();
if (fileChannel != null) {
fileChannel.close();
}
if (randomAccessFile != null) {
randomAccessFile.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
示例14: processByteBufferWrite
import java.nio.MappedByteBuffer; //导入方法依赖的package包/类
private int processByteBufferWrite(Region region, Hasher hasher, MappedByteBuffer mappedByteBuffer) {
for (int i = 0; i < region.getSize(); i++) {
byte b = 0;
mappedByteBuffer.put(i, b);
hasher.putByte(b);
}
return 0;
}
示例15: 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);
}