use of java.nio.channels.FileChannel in project gephi by gephi.
the class SimpleHTMLReport method copy.
public void copy(File source, File dest) throws IOException {
FileChannel in = null, out = null;
try {
if (dest.isDirectory()) {
dest = new File(dest, source.getName());
}
in = new FileInputStream(source).getChannel();
out = new FileOutputStream(dest).getChannel();
long size = in.size();
MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, 0, size);
out.write(buf);
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
use of java.nio.channels.FileChannel in project xUtils3 by wyouflf.
the class MD5 method md5.
public static String md5(File file) throws IOException {
MessageDigest messagedigest = null;
FileInputStream in = null;
FileChannel ch = null;
byte[] encodeBytes = null;
try {
messagedigest = MessageDigest.getInstance("MD5");
in = new FileInputStream(file);
ch = in.getChannel();
MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0, file.length());
messagedigest.update(byteBuffer);
encodeBytes = messagedigest.digest();
} catch (NoSuchAlgorithmException neverHappened) {
throw new RuntimeException(neverHappened);
} finally {
IOUtil.closeQuietly(in);
IOUtil.closeQuietly(ch);
}
return toHexString(encodeBytes);
}
use of java.nio.channels.FileChannel in project hazelcast by hazelcast.
the class NearCachePreloaderLockTest method setUp.
@Before
public void setUp() throws Exception {
preloaderLock = new NearCachePreloaderLock(logger, preloaderLockFile.getAbsolutePath());
FileChannel realChannel = new RandomAccessFile(lockFile, "rw").getChannel();
channel = spy(realChannel);
}
use of java.nio.channels.FileChannel in project cassandra by apache.
the class SegmentReaderTest method underlyingEncryptedSegmenterTest.
public void underlyingEncryptedSegmenterTest(BiFunction<FileDataInput, Integer, ByteBuffer> readFun) throws IOException {
EncryptionContext context = EncryptionContextGenerator.createContext(true);
CipherFactory cipherFactory = new CipherFactory(context.getTransparentDataEncryptionOptions());
int plainTextLength = (1 << 13) - 137;
ByteBuffer plainTextBuffer = ByteBuffer.allocate(plainTextLength);
random.nextBytes(plainTextBuffer.array());
ByteBuffer compressedBuffer = EncryptionUtils.compress(plainTextBuffer, null, true, context.getCompressor());
Cipher cipher = cipherFactory.getEncryptor(context.getTransparentDataEncryptionOptions().cipher, context.getTransparentDataEncryptionOptions().key_alias);
File encryptedFile = File.createTempFile("encrypted-segment-", ".log");
encryptedFile.deleteOnExit();
FileChannel channel = new RandomAccessFile(encryptedFile, "rw").getChannel();
channel.write(ByteBufferUtil.bytes(plainTextLength));
EncryptionUtils.encryptAndWrite(compressedBuffer, channel, true, cipher);
channel.close();
try (RandomAccessReader reader = RandomAccessReader.open(encryptedFile)) {
context = EncryptionContextGenerator.createContext(cipher.getIV(), true);
EncryptedSegmenter segmenter = new EncryptedSegmenter(reader, context);
SyncSegment syncSegment = segmenter.nextSegment(0, (int) reader.length());
// EncryptedSegmenter includes the Sync header length in the syncSegment.endPosition (value)
Assert.assertEquals(plainTextLength, syncSegment.endPosition - CommitLogSegment.SYNC_MARKER_SIZE);
ByteBuffer fileBuffer = readFun.apply(syncSegment.input, plainTextLength);
plainTextBuffer.position(0);
Assert.assertEquals(plainTextBuffer, fileBuffer);
}
}
use of java.nio.channels.FileChannel in project cassandra by apache.
the class CompressorTest method testMappedFile.
public void testMappedFile() throws IOException {
byte[] data = new byte[1 << 20];
new Random().nextBytes(data);
ByteBuffer src = makeBB(data.length);
src.put(data);
src.flip();
// create a temp file
File temp = File.createTempFile("tempfile", ".tmp");
temp.deleteOnExit();
// Prepend some random bytes to the output and compress
final int outOffset = 3;
byte[] garbage = new byte[outOffset + compressor.initialCompressedBufferLength(data.length)];
new Random().nextBytes(garbage);
ByteBuffer dest = makeBB(outOffset + compressor.initialCompressedBufferLength(data.length));
dest.put(garbage);
dest.clear();
dest.position(outOffset);
compressor.compress(src, dest);
int compressedLength = dest.position() - outOffset;
FileChannel channel = FileChannel.open(temp.toPath(), StandardOpenOption.READ, StandardOpenOption.WRITE);
dest.clear();
channel.write(dest);
MappedByteBuffer mappedData = Files.map(temp);
ByteBuffer result = makeBB(data.length + 100);
mappedData.position(outOffset).limit(outOffset + compressedLength);
compressor.uncompress(mappedData, result);
channel.close();
result.flip();
Assert.assertEquals(data.length, result.limit());
for (int i = 0; i < result.limit(); i++) {
Assert.assertEquals("Decompression mismatch at byte " + i, data[i], result.get());
}
}
Aggregations