use of java.nio.channels.FileChannel in project platform_frameworks_base by android.
the class Hyphenator method loadHyphenator.
private static Hyphenator loadHyphenator(String languageTag) {
String patternFilename = "hyph-" + languageTag.toLowerCase(Locale.US) + ".hyb";
File patternFile = new File(getSystemHyphenatorLocation(), patternFilename);
try {
RandomAccessFile f = new RandomAccessFile(patternFile, "r");
try {
FileChannel fc = f.getChannel();
MappedByteBuffer buf = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
long nativePtr = StaticLayout.nLoadHyphenator(buf, 0);
return new Hyphenator(nativePtr, buf);
} finally {
f.close();
}
} catch (IOException e) {
Log.e(TAG, "error loading hyphenation " + patternFile, e);
return null;
}
}
use of java.nio.channels.FileChannel in project cassandra by apache.
the class EncryptionUtilsTest method fullRoundTrip.
@Test
public void fullRoundTrip() throws IOException, BadPaddingException, ShortBufferException, IllegalBlockSizeException {
// compress
byte[] buf = new byte[(1 << 12) - 7];
random.nextBytes(buf);
ByteBuffer compressedBuffer = EncryptionUtils.compress(ByteBuffer.wrap(buf), ByteBuffer.allocate(0), true, compressor);
// encrypt
CipherFactory cipherFactory = new CipherFactory(tdeOptions);
Cipher encryptor = cipherFactory.getEncryptor(tdeOptions.cipher, tdeOptions.key_alias);
File f = File.createTempFile("commitlog-enc-utils-", ".tmp");
f.deleteOnExit();
FileChannel channel = new RandomAccessFile(f, "rw").getChannel();
EncryptionUtils.encryptAndWrite(compressedBuffer, channel, true, encryptor);
// decrypt
Cipher decryptor = cipherFactory.getDecryptor(tdeOptions.cipher, tdeOptions.key_alias, encryptor.getIV());
ByteBuffer decryptedBuffer = EncryptionUtils.decrypt(RandomAccessReader.open(f), ByteBuffer.allocate(0), true, decryptor);
// uncompress
ByteBuffer uncompressedBuffer = EncryptionUtils.uncompress(decryptedBuffer, ByteBuffer.allocate(0), true, compressor);
Assert.assertArrayEquals(buf, uncompressedBuffer.array());
}
use of java.nio.channels.FileChannel in project cassandra by apache.
the class EncryptionUtilsTest method encrypt.
@Test
public void encrypt() throws BadPaddingException, ShortBufferException, IllegalBlockSizeException, IOException {
byte[] buf = new byte[(1 << 12) - 7];
random.nextBytes(buf);
// encrypt
CipherFactory cipherFactory = new CipherFactory(tdeOptions);
Cipher encryptor = cipherFactory.getEncryptor(tdeOptions.cipher, tdeOptions.key_alias);
File f = File.createTempFile("commitlog-enc-utils-", ".tmp");
f.deleteOnExit();
FileChannel channel = new RandomAccessFile(f, "rw").getChannel();
EncryptionUtils.encryptAndWrite(ByteBuffer.wrap(buf), channel, true, encryptor);
channel.close();
// decrypt
Cipher decryptor = cipherFactory.getDecryptor(tdeOptions.cipher, tdeOptions.key_alias, encryptor.getIV());
ByteBuffer decryptedBuffer = EncryptionUtils.decrypt(RandomAccessReader.open(f), ByteBuffer.allocate(0), true, decryptor);
// normally, we'd just call BB.array(), but that gives you the *entire* backing array, not with any of the offsets (position,limit) applied.
// thus, just for this test, we copy the array and perform an array-level comparison with those offsets
decryptedBuffer.limit(buf.length);
byte[] b = new byte[buf.length];
System.arraycopy(decryptedBuffer.array(), 0, b, 0, buf.length);
Assert.assertArrayEquals(buf, b);
}
use of java.nio.channels.FileChannel in project cordova-android by apache.
the class CordovaResourceApi method copyResource.
// Copies the input to the output in the most efficient manner possible.
// Closes both streams.
public void copyResource(OpenForReadResult input, OutputStream outputStream) throws IOException {
assertBackgroundThread();
try {
InputStream inputStream = input.inputStream;
if (inputStream instanceof FileInputStream && outputStream instanceof FileOutputStream) {
FileChannel inChannel = ((FileInputStream) input.inputStream).getChannel();
FileChannel outChannel = ((FileOutputStream) outputStream).getChannel();
long offset = 0;
long length = input.length;
if (input.assetFd != null) {
offset = input.assetFd.getStartOffset();
}
// transferFrom()'s 2nd arg is a relative position. Need to set the absolute
// position first.
inChannel.position(offset);
outChannel.transferFrom(inChannel, 0, length);
} else {
final int BUFFER_SIZE = 8192;
byte[] buffer = new byte[BUFFER_SIZE];
for (; ; ) {
int bytesRead = inputStream.read(buffer, 0, BUFFER_SIZE);
if (bytesRead <= 0) {
break;
}
outputStream.write(buffer, 0, bytesRead);
}
}
} finally {
input.inputStream.close();
if (outputStream != null) {
outputStream.close();
}
}
}
use of java.nio.channels.FileChannel in project cassandra by apache.
the class HintsWriter method create.
// HintsWriter owns channel
@SuppressWarnings("resource")
static HintsWriter create(File directory, HintsDescriptor descriptor) throws IOException {
File file = new File(directory, descriptor.fileName());
FileChannel channel = FileChannel.open(file.toPath(), StandardOpenOption.WRITE, StandardOpenOption.CREATE_NEW);
int fd = CLibrary.getfd(channel);
CRC32 crc = new CRC32();
try (DataOutputBuffer dob = DataOutputBuffer.scratchBuffer.get()) {
// write the descriptor
descriptor.serialize(dob);
ByteBuffer descriptorBytes = dob.buffer();
updateChecksum(crc, descriptorBytes);
channel.write(descriptorBytes);
} catch (Throwable e) {
channel.close();
throw e;
}
if (descriptor.isEncrypted())
return new EncryptedHintsWriter(directory, descriptor, file, channel, fd, crc);
if (descriptor.isCompressed())
return new CompressedHintsWriter(directory, descriptor, file, channel, fd, crc);
return new HintsWriter(directory, descriptor, file, channel, fd, crc);
}
Aggregations