@Test public void readCorrectlyReadsLastByte() throws IOException { byte[] data = TestUtils.generateRandomBytes(4096 * 2 * 2); Files.write(tempFile, data); ByteBuffer buffer = reader.read(process, data.length - 1, 1); assertEquals(1, buffer.limit()); assertEquals(data[data.length - 1], buffer.get()); }
@Override public ByteBuffer read(NativeProcess process, int offset, int bytesToRead) { Objects.requireNonNull(process, "process cannot be null!"); if (bytesToRead < 1) { throw new IllegalArgumentException("Cannot read fewer than 1 byte!"); } ByteBuffer buffer = ByteBuffer.allocate(bytesToRead); buffer.order(ByteOrder.LITTLE_ENDIAN); try (SeekableByteChannel input = readableMemoryProvider.openRead(process.getPid())) { long bytesToSkip = 0xFFFFFFFFL & offset; input.position(bytesToSkip); int bytesRead = input.read(buffer); if (bytesRead == -1) { throw new MemoryAccessException("Reading process memory failed! Reached end of memory!"); } if (bytesRead != bytesToRead) { throw new MemoryAccessException("Only " + bytesRead + " bytes out of " + bytesToRead + " could be read!"); } buffer.flip(); return buffer; } catch (ProcessOpenException | IOException ex) { throw new MemoryAccessException("Reading process memory failed, see getCause()!", ex); } }
LinuxRawMemoryReader implements RawMemoryReader { @Override public ByteBuffer read(NativeProcess process, int offset, int bytesToRead) { Objects.requireNonNull(process, "process cannot be null!"); if (bytesToRead < 1) { throw new IllegalArgumentException("Cannot read fewer than 1 byte!"); } ByteBuffer buffer = ByteBuffer.allocate(bytesToRead); buffer.order(ByteOrder.LITTLE_ENDIAN); try (SeekableByteChannel input = readableMemoryProvider.openRead(process.getPid())) { long bytesToSkip = 0xFFFFFFFFL & offset; input.position(bytesToSkip); int bytesRead = input.read(buffer); if (bytesRead == -1) { throw new MemoryAccessException("Reading process memory failed! Reached end of memory!"); } if (bytesRead != bytesToRead) { throw new MemoryAccessException("Only " + bytesRead + " bytes out of " + bytesToRead + " could be read!"); } buffer.flip(); return buffer; } catch (ProcessOpenException | IOException ex) { throw new MemoryAccessException("Reading process memory failed, see getCause()!", ex); } } }
LinuxRawMemoryReader implements RawMemoryReader { @Override public ByteBuffer read(NativeProcess process, int offset, int bytesToRead) { Objects.requireNonNull(process, "process cannot be null!"); if (bytesToRead < 1) { throw new IllegalArgumentException("Cannot read fewer than 1 byte!"); } ByteBuffer buffer = ByteBuffer.allocate(bytesToRead); buffer.order(ByteOrder.LITTLE_ENDIAN); try (SeekableByteChannel input = readableMemoryProvider.openRead(process.getPid())) { long bytesToSkip = 0xFFFFFFFFL & offset; input.position(bytesToSkip); int bytesRead = input.read(buffer); if (bytesRead == -1) { throw new MemoryAccessException("Reading process memory failed! Reached end of memory!"); } if (bytesRead != bytesToRead) { throw new MemoryAccessException("Only " + bytesRead + " bytes out of " + bytesToRead + " could be read!"); } buffer.flip(); return buffer; } catch (ProcessOpenException | IOException ex) { throw new MemoryAccessException("Reading process memory failed, see getCause()!", ex); } } LinuxRawMemoryReader(ReadableMemoryProvider readableMemoryProvider); }
LinuxRawMemoryReader implements RawMemoryReader { @Override public ByteBuffer read(NativeProcess process, int offset, int bytesToRead) { Objects.requireNonNull(process, "process cannot be null!"); if (bytesToRead < 1) { throw new IllegalArgumentException("Cannot read fewer than 1 byte!"); } ByteBuffer buffer = ByteBuffer.allocate(bytesToRead); buffer.order(ByteOrder.LITTLE_ENDIAN); try (SeekableByteChannel input = readableMemoryProvider.openRead(process.getPid())) { long bytesToSkip = 0xFFFFFFFFL & offset; input.position(bytesToSkip); int bytesRead = input.read(buffer); if (bytesRead == -1) { throw new MemoryAccessException("Reading process memory failed! Reached end of memory!"); } if (bytesRead != bytesToRead) { throw new MemoryAccessException("Only " + bytesRead + " bytes out of " + bytesToRead + " could be read!"); } buffer.flip(); return buffer; } catch (ProcessOpenException | IOException ex) { throw new MemoryAccessException("Reading process memory failed, see getCause()!", ex); } } LinuxRawMemoryReader(ReadableMemoryProvider readableMemoryProvider); @Override ByteBuffer read(NativeProcess process, int offset, int bytesToRead); }
LinuxRawMemoryReader implements RawMemoryReader { @Override public ByteBuffer read(NativeProcess process, int offset, int bytesToRead) { Objects.requireNonNull(process, "process cannot be null!"); if (bytesToRead < 1) { throw new IllegalArgumentException("Cannot read fewer than 1 byte!"); } ByteBuffer buffer = ByteBuffer.allocate(bytesToRead); buffer.order(ByteOrder.LITTLE_ENDIAN); try (SeekableByteChannel input = readableMemoryProvider.openRead(process.getPid())) { long bytesToSkip = 0xFFFFFFFFL & offset; input.position(bytesToSkip); int bytesRead = input.read(buffer); if (bytesRead == -1) { throw new MemoryAccessException("Reading process memory failed! Reached end of memory!"); } if (bytesRead != bytesToRead) { throw new MemoryAccessException("Only " + bytesRead + " bytes out of " + bytesToRead + " could be read!"); } buffer.flip(); return buffer; } catch (ProcessOpenException | IOException ex) { throw new MemoryAccessException("Reading process memory failed, see getCause()!", ex); } } LinuxRawMemoryReader(ReadableMemoryProvider readableMemoryProvider); @Override ByteBuffer read(NativeProcess process, int offset, int bytesToRead);  }