Java Code Examples for java.nio.MappedByteBuffer#remaining()

The following examples show how to use java.nio.MappedByteBuffer#remaining() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BasicRoutine.java    From SoftwarePilot with MIT License 6 votes vote down vote up
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];
}
 
Example 2
Source File: FileChannelUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenFile_whenReadAFileSectionIntoMemoryWithFileChannel_thenCorrect() throws IOException {

    try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r"); 
        FileChannel channel = reader.getChannel(); 
        ByteArrayOutputStream out = new ByteArrayOutputStream()) {

        MappedByteBuffer buff = channel.map(FileChannel.MapMode.READ_ONLY, 6, 5);

        if (buff.hasRemaining()) {
            byte[] data = new byte[buff.remaining()];
            buff.get(data);
            assertEquals("world", new String(data, StandardCharsets.UTF_8));
        }
    }
}
 
Example 3
Source File: LargeMappedByteBuffer.java    From proxyee-down with Apache License 2.0 6 votes vote down vote up
public final void put(ByteBuffer byteBuffer) throws IOException {
  try {
    int index = getIndex();
    long length = byteBuffer.limit() - byteBuffer.position();
    this.position += length;
    MappedByteBuffer mappedBuffer = bufferList.get(index);
    if (mappedBuffer.remaining() < length) {
      byte[] temp = new byte[mappedBuffer.remaining()];
      byteBuffer.get(temp);
      bufferList.get(index).put(temp);
      bufferList.get(index + 1).put(byteBuffer);
    } else {
      bufferList.get(index).put(byteBuffer);
    }
  } catch (Exception e) {
    throw new IOException(
        "LargeMappedByteBuffer put rawPosition-" + rawPosition + "\tposition-" + position
            + "\tsize-" + size, e);
  }
}
 
Example 4
Source File: VisionTest.java    From SoftwarePilot with MIT License 6 votes vote down vote up
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];
}
 
Example 5
Source File: CubeSim.java    From SoftwarePilot with MIT License 6 votes vote down vote up
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];
}
 
Example 6
Source File: BasicVision.java    From SoftwarePilot with MIT License 6 votes vote down vote up
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];
}
 
Example 7
Source File: RoutineTemplate.java    From SoftwarePilot with MIT License 6 votes vote down vote up
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];
}
 
Example 8
Source File: WaypointMissionTestBackup.java    From SoftwarePilot with MIT License 6 votes vote down vote up
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];
}
 
Example 9
Source File: WaypointMissionTest.java    From SoftwarePilot with MIT License 6 votes vote down vote up
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];
}
 
Example 10
Source File: AutoSelfie.java    From SoftwarePilot with MIT License 6 votes vote down vote up
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];
}
 
Example 11
Source File: ReadOnlyMemMap.java    From sparkey-java with Apache License 2.0 5 votes vote down vote up
public void skipBytes(long amount) throws IOException {
  MappedByteBuffer curChunk = getCurChunk();
  int remaining = curChunk.remaining();
  if (remaining >= amount) {
    curChunk.position((int) (curChunk.position() + amount));
  } else {
    next();
    skipBytes(amount - remaining);
  }
}
 
Example 12
Source File: ReadOnlyMemMap.java    From sparkey-java with Apache License 2.0 5 votes vote down vote up
@Override
public long readLittleEndianLong() throws IOException {
  MappedByteBuffer curChunk = getCurChunk();
  if (curChunk.remaining() >= 8) {
    return curChunk.getLong();
  }

  // Value is on the chunk boundary - edge case so it is ok if it's a bit slower.
  return Util.readLittleEndianLongSlowly(this);
}
 
Example 13
Source File: ReadWriteMemMap.java    From sparkey-java with Apache License 2.0 5 votes vote down vote up
@Override
public int readLittleEndianInt() throws IOException {
  MappedByteBuffer curChunk = getCurChunk();
  if (curChunk.remaining() >= 4) {
    return curChunk.getInt();
  }

  // Value is on the chunk boundary - edge case so it is ok if it's a bit slower.
  return Util.readLittleEndianIntSlowly(this);
}
 
Example 14
Source File: ReadOnlyMemMap.java    From sparkey-java with Apache License 2.0 5 votes vote down vote up
@Override
public int readLittleEndianInt() throws IOException {
  MappedByteBuffer curChunk = getCurChunk();
  if (curChunk.remaining() >= 4) {
    return curChunk.getInt();
  }

  // Value is on the chunk boundary - edge case so it is ok if it's a bit slower.
  return Util.readLittleEndianIntSlowly(this);
}
 
Example 15
Source File: ReadOnlyMemMap.java    From sparkey-java with Apache License 2.0 5 votes vote down vote up
public void readFully(byte[] buffer, int offset, int length) throws IOException {
  MappedByteBuffer curChunk = getCurChunk();
  long remaining = curChunk.remaining();
  if (remaining >= length) {
    curChunk.get(buffer, offset, length);
  } else {
    int remaining1 = (int) remaining;
    curChunk.get(buffer, offset, remaining1);
    length -= remaining1;
    offset += remaining1;
    next();
    readFully(buffer, offset, length);
  }
}
 
Example 16
Source File: EditorIDScanner.java    From akarnokd-misc with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    File file = new File(
            "c:\\Program Files (x86)\\Bethesda.net Launcher\\games\\Fallout76\\Data\\SeventySix.esm");

    /*
     * char[4] = 'EDID'
     * uint16 = length
     * uint8[length] = characters
     */
    int i = 1;
    try (PrintWriter out = new PrintWriter(new FileWriter("editorids.txt"))) {
        try (RandomAccessFile raf = new RandomAccessFile(file, "r")) {

            MappedByteBuffer mbb = raf.getChannel().map(MapMode.READ_ONLY, 0, raf.length());
            
            
            while (mbb.remaining() > 0) {
                if (mbb.get() == 'E') {
                    if (mbb.get() == 'D') {
                        if (mbb.get() == 'I') {
                            if (mbb.get() == 'D') {
                                int len = Short.reverseBytes(mbb.getShort()) & 0xFFFF;
                                byte[] data = new byte[len];
                                mbb.get(data);
                                out.println(new String(data, StandardCharsets.ISO_8859_1));
                                if (i % 100 == 0) {
                                    System.out.println("Records so far: " + i);
                                }
                                i++;
                            }
                        }
                    }
                }
            }
        }
    }
}
 
Example 17
Source File: ReadOnlyMemMap.java    From sparkey-java with Apache License 2.0 5 votes vote down vote up
@Override
public int readUnsignedByte() throws IOException {
  MappedByteBuffer curChunk = getCurChunk();
  if (curChunk.remaining() == 0) {
    next();
    curChunk = getCurChunk();
  }
  return ((int) curChunk.get()) & 0xFF;
}
 
Example 18
Source File: ReadWriteMemMap.java    From sparkey-java with Apache License 2.0 5 votes vote down vote up
@Override
public long readLittleEndianLong() throws IOException {
  MappedByteBuffer curChunk = getCurChunk();
  if (curChunk.remaining() >= 8) {
    return curChunk.getLong();
  }

  // Value is on the chunk boundary - edge case so it is ok if it's a bit slower.
  return Util.readLittleEndianLongSlowly(this);
}
 
Example 19
Source File: GeoEngine.java    From L2jOrg with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Loads geodata from a file. When file does not exist, is corrupted or not consistent, loads none geodata.
 *
 *
 * @param filePath : The Geodata File Path
 * @param regionX : Geodata file region X coordinate.
 * @param regionY : Geodata file region Y coordinate.
 * @return boolean : True, when geodata file was loaded without problem.
 */
private boolean loadGeoBlocks(Path filePath, int regionX, int regionY) {

    // standard load
    try (RandomAccessFile raf = new RandomAccessFile(filePath.toAbsolutePath().toString(), "r");
         FileChannel fc = raf.getChannel()) {
        // initialize file buffer
        MappedByteBuffer buffer = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()).load();
        buffer.order(ByteOrder.LITTLE_ENDIAN);

        // get block indexes
        final int blockX = (regionX - World.TILE_X_MIN) * GeoStructure.REGION_BLOCKS_X;
        final int blockY = (regionY - World.TILE_Y_MIN) * GeoStructure.REGION_BLOCKS_Y;

        // loop over region blocks
        for (int ix = 0; ix < GeoStructure.REGION_BLOCKS_X; ix++) {
            for (int iy = 0; iy < GeoStructure.REGION_BLOCKS_Y; iy++) {
                // get block type
                final byte type = buffer.get();

                // load block according to block type
                blocks[blockX + ix][blockY + iy] = switch (type) {
                    case GeoStructure.TYPE_FLAT_L2D -> new BlockFlat(buffer, GeoFormat.L2D);
                    case GeoStructure.TYPE_COMPLEX_L2D -> new BlockComplex(buffer, GeoFormat.L2D);
                    case GeoStructure.TYPE_MULTILAYER_L2D -> new BlockMultilayer(buffer, GeoFormat.L2D);
                    default -> throw new IllegalArgumentException("Unknown block type: " + type);
                };
            }
        }

        // check data consistency
        if (buffer.remaining() > 0) {
            LOGGER.warn("GeoEngine: Region file {} can be corrupted, remaining {} bytes to read.", filePath, buffer.remaining());
        }

        // loading was successful
        return true;
    } catch (Exception e) {
        LOGGER.error("Error while loading {} region file.", filePath);
        LOGGER.error(e.getMessage());

        // replace whole region file with null blocks
        loadNullBlocks(regionX, regionY);

        // loading was not successful
        return false;
    }
}
 
Example 20
Source File: StackLogDecoder.java    From unidbg with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws IOException {
    File stackLog = new File("target/stack-logs.78490.2000.unidbg.zcmkle.index");
    FileInputStream inputStream = new FileInputStream(stackLog);
    FileChannel channel = inputStream.getChannel();
    MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, stackLog.length());
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    int i = 0;
    while (buffer.remaining() >= 16) {
        long size = buffer.getInt() & 0xffffffffL;
        long addr = (buffer.getInt() & 0xffffffffL) ^ 0x00005555;
        long offset_and_flags_l = buffer.getInt() & 0xffffffffL;
        long offset_and_flags_h = buffer.getInt() & 0xffffffffL;
        int flag = (int) ((offset_and_flags_h & 0xff000000) >> 24);
        long stackId = ((offset_and_flags_h & 0x00ffffff) << 32) | offset_and_flags_l;
        String action = "OTHER";
        boolean isFree = false;
        switch (flag) {
            case MALLOC_LOG_TYPE_ALLOCATE:
                action = "ALLOC";
                isFree = false;
                break;
            case MALLOC_LOG_TYPE_DEALLOCATE:
                action = "FREE ";
                isFree = true;
                break;
            case stack_logging_type_vm_allocate:
                action = "MMAP ";
                isFree = false;
                break;
            case stack_logging_type_vm_deallocate:
                action = "UNMAP";
                isFree = true;
                break;
            default:
                if ((flag & stack_logging_type_mapped_file_or_shared_mem) != 0 && (flag & stack_logging_type_vm_allocate) != 0) {
                    action = "MMAPF";
                    isFree = false;
                    break;
                }

                System.err.println(flag);
                break;
        }
        String msg = String.format("[%08d]: %s, stackId=0x%014x, address=0x%08x, size=0x%x", i++, action, stackId, addr, size);
        if (isFree) {
            System.err.println(msg);
        } else {
            System.out.println(msg);
        }
    }
    channel.close();
    inputStream.close();
}