下面列出了java.nio.MappedByteBuffer#capacity ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
byte[] read4k(){
try {
File file = new File(Environment.getExternalStorageDirectory().getPath()+"/AUAVtmp/fullPic.JPG");
//File file = new File("../tmp/pictmp.jpg");
FileChannel fileChannel = new RandomAccessFile(file, "r").getChannel();
MappedByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size());
pic = new byte[buffer.capacity()];
while(buffer.hasRemaining()){
int remaining = pic.length;
if(buffer.remaining() < remaining){
remaining = buffer.remaining();
}
buffer.get(pic, 0, remaining);
}
return pic;
} catch(Exception e){
e.printStackTrace();
}
return new byte[0];
}
byte[] read4k(){
try {
File file = new File(Environment.getExternalStorageDirectory().getPath()+"/AUAVtmp/fullPic.JPG");
//File file = new File("../tmp/pictmp.jpg");
FileChannel fileChannel = new RandomAccessFile(file, "r").getChannel();
MappedByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size());
pic = new byte[buffer.capacity()];
while(buffer.hasRemaining()){
int remaining = pic.length;
if(buffer.remaining() < remaining){
remaining = buffer.remaining();
}
buffer.get(pic, 0, remaining);
}
return pic;
} catch(Exception e){
e.printStackTrace();
}
return new byte[0];
}
byte[] readImg(){
try {
//BufferedImage origImage = ImageIO.read(imgPath);
//ByteArrayOutputStream baos = new ByteArrayOutputStream();
//ImageIO.write(origImage, "jpg", baos);
//return baos.toByteArray();
File file = new File(Environment.getExternalStorageDirectory().getPath()+"/AUAVtmp/fullPic.JPG");
//File file = new File("../tmp/pictmp.jpg");
FileChannel fileChannel = new RandomAccessFile(file, "r").getChannel();
MappedByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size());
pic = new byte[buffer.capacity()];
while(buffer.hasRemaining()){
int remaining = pic.length;
if(buffer.remaining() < remaining){
remaining = buffer.remaining();
}
buffer.get(pic, 0, remaining);
}
return pic;
} catch(Exception e){
e.printStackTrace();
}
return new byte[0];
}
byte[] readWaypoint(String fname){
//read in the file
try{
File f = new File(Environment.getExternalStorageDirectory().getPath()+fname);
FileChannel fileChannel = new RandomAccessFile(f,"r").getChannel();
MappedByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY,0,fileChannel.size());
data = new byte[buffer.capacity()];
while(buffer.hasRemaining()){
int remaining = data.length;
if (buffer.remaining()<remaining){
remaining = buffer.remaining();
}
buffer.get(data,0,remaining);
}
return data;
}catch (IOException e) {
e.printStackTrace();
}
return new byte[0];
}
byte[] read4k(){
try {
File file = new File(Environment.getExternalStorageDirectory().getPath()+"/AUAVtmp/fullPic.JPG");
//File file = new File("../tmp/pictmp.jpg");
FileChannel fileChannel = new RandomAccessFile(file, "r").getChannel();
MappedByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size());
pic = new byte[buffer.capacity()];
while(buffer.hasRemaining()){
int remaining = pic.length;
if(buffer.remaining() < remaining){
remaining = buffer.remaining();
}
buffer.get(pic, 0, remaining);
}
return pic;
} catch(Exception e){
e.printStackTrace();
}
return new byte[0];
}
byte[] read4k(){
try {
File file = new File(Environment.getExternalStorageDirectory().getPath()+"/AUAVtmp/fullPic.JPG");
//File file = new File("../tmp/pictmp.jpg");
FileChannel fileChannel = new RandomAccessFile(file, "r").getChannel();
MappedByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size());
pic = new byte[buffer.capacity()];
while(buffer.hasRemaining()){
int remaining = pic.length;
if(buffer.remaining() < remaining){
remaining = buffer.remaining();
}
buffer.get(pic, 0, remaining);
}
return pic;
} catch(Exception e){
e.printStackTrace();
}
return new byte[0];
}
byte[] read4k(){
try {
File file = new File(Environment.getExternalStorageDirectory().getPath()+"/AUAVtmp/fullPic.JPG");
//File file = new File("../tmp/pictmp.jpg");
FileChannel fileChannel = new RandomAccessFile(file, "r").getChannel();
MappedByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size());
pic = new byte[buffer.capacity()];
while(buffer.hasRemaining()){
int remaining = pic.length;
if(buffer.remaining() < remaining){
remaining = buffer.remaining();
}
buffer.get(pic, 0, remaining);
}
return pic;
} catch(Exception e){
e.printStackTrace();
}
return new byte[0];
}
byte[] read4k(){
try {
File file = new File(Environment.getExternalStorageDirectory().getPath()+"/AUAVtmp/fullPic.JPG");
//File file = new File("../tmp/pictmp.jpg");
FileChannel fileChannel = new RandomAccessFile(file, "r").getChannel();
MappedByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size());
pic = new byte[buffer.capacity()];
while(buffer.hasRemaining()){
int remaining = pic.length;
if(buffer.remaining() < remaining){
remaining = buffer.remaining();
}
buffer.get(pic, 0, remaining);
}
return pic;
} catch(Exception e){
e.printStackTrace();
}
return new byte[0];
}
byte[] readImg(){
try {
//BufferedImage origImage = ImageIO.read(imgPath);
//ByteArrayOutputStream baos = new ByteArrayOutputStream();
//ImageIO.write(origImage, "jpg", baos);
//return baos.toByteArray();
//File file = new File(Environment.getExternalStorageDirectory().getPath()+"/AUAVtmp/fullPic.JPG");
File file = new File("../tmp/pictmp.jpg");
FileChannel fileChannel = new RandomAccessFile(file, "r").getChannel();
MappedByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size());
byte[] pic = new byte[buffer.capacity()];
while(buffer.hasRemaining()){
int remaining = pic.length;
if(buffer.remaining() < remaining){
remaining = buffer.remaining();
}
buffer.get(pic, 0, remaining);
}
return pic;
} catch(Exception e){
e.printStackTrace();
}
return new byte[0];
}
byte[] readImg(){
try {
//BufferedImage origImage = ImageIO.read(imgPath);
//ByteArrayOutputStream baos = new ByteArrayOutputStream();
//ImageIO.write(origImage, "jpg", baos);
//return baos.toByteArray();
File file = new File(Environment.getExternalStorageDirectory().getPath()+"/AUAVtmp/fullPic.JPG");
//File file = new File("../tmp/pictmp.jpg");
FileChannel fileChannel = new RandomAccessFile(file, "r").getChannel();
MappedByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size());
byte[] pic = new byte[buffer.capacity()];
while(buffer.hasRemaining()){
int remaining = pic.length;
if(buffer.remaining() < remaining){
remaining = buffer.remaining();
}
buffer.get(pic, 0, remaining);
}
return pic;
} catch(Exception e){
e.printStackTrace();
}
return new byte[0];
}
public RdmaStorageLocalEndpoint(InetSocketAddress datanodeAddr) throws Exception {
LOG.info("new local endpoint for address " + datanodeAddr);
String dataPath = RdmaStorageServer.getDatanodeDirectory(datanodeAddr);
File dataDir = new File(dataPath);
if (!dataDir.exists()){
throw new Exception("Local RDMA data path missing");
}
this.address = datanodeAddr;
this.bufferMap = new ConcurrentHashMap<Long, CrailBuffer>();
this.unsafe = getUnsafe();
long lba = 0;
for (File dataFile : dataDir.listFiles()) {
MappedByteBuffer mappedBuffer = mmap(dataFile);
bufferMap.put(lba, OffHeapBuffer.wrap(mappedBuffer));
lba += mappedBuffer.capacity();
}
}
/**
* get an input stream for the file that is fast, trading memory for speed
*
* @param file
* @return InputStream
*/
public static InputStream fastStream(File file) {
FileInputStream fis = null;
FileChannel ch = null;
byte[] byteArray = null;
try {
fis = new FileInputStream(file);
if (fis != null) {
ch = fis.getChannel();
if (ch.size() > 1000000000) {
return new BufferedInputStream(fis, BUFFER_SIZE);
}
MappedByteBuffer mb = ch.map(FileChannel.MapMode.READ_ONLY, 0L, ch.size());
byteArray = new byte[mb.capacity()];
int got;
while (mb.hasRemaining()) {
got = Math.min(mb.remaining(), byteArray.length);
mb.get(byteArray, 0, got);
}
}
} catch (FileNotFoundException fnfe) {
throw new IOError(fnfe);
} catch (IOException ioe) {
throw new IOError(ioe);
} finally {
if (ch != null) {
try {
ch.close();
fis.close();
} catch (IOException ioe2) {
}
}
}
return new ByteArrayInputStream(byteArray);
}
@Override
public void run() {
for (int i=this.sm.spaces.length-1; i>=0; i--) {
Space ii = this.sm.spaces[i];
if (ii != null) {
MappedByteBuffer buf = ii.mmf.buf;
for (int j=0; i<buf.capacity(); j+=0x100) {
this.checksum ^= buf.get(j);
}
}
}
}
public MappedMemory(MappedByteBuffer buffer, MappedMemoryAllocator allocator) {
super(((DirectBuffer) buffer).address(), buffer.capacity(), allocator);
this.buffer = buffer;
}
public MappedMemory(MappedByteBuffer buffer, MappedMemoryAllocator allocator) {
this.buffer = buffer;
this.allocator = allocator;
this.size = buffer.capacity();
}
/**
* Creates ring buffer with given capacity which mapped to file.
*
* @param buf {@link MappedByteBuffer} instance.
* @param metrics Metrics.
*/
public SegmentedRingByteBuffer(MappedByteBuffer buf, DataStorageMetricsImpl metrics) {
this(buf.capacity(), buf.capacity(), buf, MAPPED, metrics);
}