use of java.nio.channels.SeekableByteChannel in project FinalCrypt by ron-from-nl.
the class DeviceController method readLBA.
// Read byte[] from device
public static synchronized byte[] readLBA(FCPath fcPath, long lba, long length) {
long readInputDeviceChannelTransfered = 0;
ByteBuffer inputDeviceBuffer = ByteBuffer.allocate((int) length);
inputDeviceBuffer.clear();
try (final SeekableByteChannel readInputDeviceChannel = Files.newByteChannel(fcPath.path, EnumSet.of(StandardOpenOption.READ))) {
readInputDeviceChannel.position(getLBAOffSet(bytesPerSector, fcPath.size, lba));
readInputDeviceChannelTransfered = readInputDeviceChannel.read(inputDeviceBuffer);
inputDeviceBuffer.flip();
readInputDeviceChannel.close();
// ui.log("Read LBA " + lba + " Transfered: " + readInputDeviceChannelTransfered + "\r\n");
} catch (IOException ex) {
ui.status("Device().read(..) " + ex.getMessage(), true);
}
return inputDeviceBuffer.array();
}
use of java.nio.channels.SeekableByteChannel in project com.revolsys.open by revolsys.
the class XbaseRecordWriter method close.
@SuppressWarnings("deprecation")
@Override
public void close() {
try {
if (this.out != null) {
try {
this.recordBuffer.put((byte) 0x1a);
Buffers.writeAll(this.out, this.recordBuffer);
if (this.out instanceof SeekableByteChannel) {
final SeekableByteChannel out = (SeekableByteChannel) this.out;
out.position(1);
final ByteBuffer buffer = ByteBuffer.allocate(7);
buffer.order(ByteOrder.LITTLE_ENDIAN);
final Date now = new Date();
buffer.put((byte) now.getYear());
buffer.put((byte) (now.getMonth() + 1));
buffer.put((byte) now.getDate());
buffer.putInt(this.recordCount);
Buffers.writeAll(out, buffer);
}
} finally {
try {
this.out.close();
} finally {
this.out = null;
}
}
}
} catch (final IOException e) {
throw new RuntimeException(e);
}
}
use of java.nio.channels.SeekableByteChannel in project sis by apache.
the class TestUtilities method createTemporaryFile.
/**
* Copies the full content of the given test resource in a temporary file and returns the channel for that file.
* The file is opened with {@link StandardOpenOption#DELETE_ON_CLOSE}, together with read and write options.
*
* @param caller defines the root from which to search for the {@code resource}.
* @param resource path (relative to the {@code caller}) of the test file to copy.
* @return a channel opened on a copy of the content of the given test resource.
* @throws IOException if an error occurred while copying the data.
*
* @since 0.8
*/
public static SeekableByteChannel createTemporaryFile(final Class<?> caller, final String resource) throws IOException {
final SeekableByteChannel channel;
try (ReadableByteChannel in = Channels.newChannel(caller.getResourceAsStream(resource))) {
final int s = resource.lastIndexOf('.');
final Path file = Files.createTempFile("SIS", (s >= 0) ? resource.substring(s) : null);
channel = Files.newByteChannel(file, StandardOpenOption.DELETE_ON_CLOSE, StandardOpenOption.READ, StandardOpenOption.WRITE);
final ByteBuffer buffer = ByteBuffer.allocate(4000);
while (in.read(buffer) >= 0) {
buffer.flip();
channel.write(buffer);
buffer.clear();
}
}
return channel.position(0);
}
use of java.nio.channels.SeekableByteChannel in project tink by google.
the class StreamingTestUtil method testFileEncryptionWithChannel.
// Methods for testFileEncryption.
/**
* Encrypt some plaintext to a file, then decrypt from the file
*/
private static void testFileEncryptionWithChannel(StreamingAead ags, File tmpFile) throws Exception {
byte[] aad = TestUtil.hexDecode("aabbccddeeff");
int plaintextSize = 1 << 18;
SeekableByteBufferChannel plaintext = new SeekableByteBufferChannel(generatePlaintext(plaintextSize));
// Encrypt to file
WritableByteChannel bc = ags.newEncryptingChannel(new FileOutputStream(tmpFile).getChannel(), aad);
int chunkSize = 1000;
ByteBuffer chunk = ByteBuffer.allocate(chunkSize);
int read;
do {
chunk.clear();
read = plaintext.read(chunk);
if (read > 0) {
chunk.flip();
bc.write(chunk);
}
} while (read != -1);
bc.close();
// Decrypt the whole file and compare to plaintext
plaintext.rewind();
ReadableByteChannel ptStream = ags.newDecryptingChannel(new FileInputStream(tmpFile).getChannel(), aad);
int decryptedSize = 0;
do {
ByteBuffer decrypted = ByteBuffer.allocate(512);
read = ptStream.read(decrypted);
if (read > 0) {
ByteBuffer expected = ByteBuffer.allocate(read);
plaintext.read(expected);
decrypted.flip();
TestUtil.assertByteBufferContains(expected.array(), decrypted);
decryptedSize += read;
}
} while (read != -1);
assertEquals(plaintextSize, decryptedSize);
// Decrypt file partially using FileChannel and compare to plaintext
plaintext.rewind();
SeekableByteChannel ptChannel = ags.newSeekableDecryptingChannel(new FileInputStream(tmpFile).getChannel(), aad);
SecureRandom random = new SecureRandom();
for (int samples = 0; samples < 100; samples++) {
int start = random.nextInt(plaintextSize);
int length = random.nextInt(plaintextSize / 100 + 1);
ByteBuffer decrypted = ByteBuffer.allocate(length);
ptChannel.position(start);
read = ptChannel.read(decrypted);
// Hence we also expect that ptChannel returns the maximal number of bytes.
if (read < length && read + start < plaintextSize) {
fail("Plaintext size is smaller than expected; read:" + read + " position:" + start + " length:" + length);
}
byte[] expected = new byte[read];
plaintext.position(start);
plaintext.read(ByteBuffer.wrap(expected));
decrypted.flip();
TestUtil.assertByteBufferContains(expected, decrypted);
}
}
use of java.nio.channels.SeekableByteChannel in project tink by google.
the class StreamingAeadThreadSafetyTest method testRandomAccessDecryption.
/**
* Test for thread safety using SeekableByteChannels. This test is an incorrect use case for
* StreamingAead implementations, since SeekableByteChannels can't be used in multiple threads.
* I.e. an implementation cannot guarantee that no other thread modifies the stream between a call
* to SeekableByteChannel.position(long) and SeekableByteChannel.read(ByteBuffer). Therefore, the
* test here only checks whether the operations are atomic. E.g. a read should read contiuous
* bytes.
*/
public void testRandomAccessDecryption(StreamingAead stream, byte[] aad, int plaintextSize) throws Exception {
int numberOfReads = 128;
int numberOfThreads = 10;
byte[] plaintext = new byte[plaintextSize];
for (int i = 0; i < plaintextSize; i++) {
// Setting plaintex[i] to (byte) i, allows the decrypting thread to check that the
// plaintext is from a continuous part of the plaintext.
plaintext[i] = (byte) i;
}
byte[] ciphertext = encrypt(stream, plaintext, aad);
SeekableByteChannel ctChannel = new SeekableByteBufferChannel(ciphertext);
SeekableByteChannel decChannel = stream.newSeekableDecryptingChannel(ctChannel, aad);
ExceptionHandler exceptionHandler = new ExceptionHandler();
Thread[] thread = new Thread[numberOfThreads];
for (int i = 0; i < numberOfThreads; i++) {
thread[i] = new RandomAccessThread(decChannel, plaintextSize, numberOfReads);
thread[i].setUncaughtExceptionHandler(exceptionHandler);
}
for (int i = 0; i < numberOfThreads; i++) {
thread[i].start();
}
for (int i = 0; i < numberOfThreads; i++) {
thread[i].join();
}
exceptionHandler.check();
}
Aggregations