List of usage examples for java.nio.channels SeekableByteChannel write
@Override int write(ByteBuffer src) throws IOException;
From source file:Main.java
public static void writeData(SeekableByteChannel seekableChannel, Charset cs) throws IOException { String separator = System.getProperty("line.separator"); StringBuilder sb = new StringBuilder(); sb.append("test"); sb.append(separator);//from w ww . j av a 2s . co m sb.append("test2"); sb.append(separator); CharBuffer charBuffer = CharBuffer.wrap(sb); ByteBuffer byteBuffer = cs.encode(charBuffer); seekableChannel.write(byteBuffer); }
From source file:com.spectralogic.ds3client.helpers.FileObjectGetter_Test.java
@Test public void testThatSymbolicLinksAreResolved() { Assume.assumeFalse(Platform.isWindows()); final String message = "Hello World"; final String file = "file.txt"; try {/* w w w .ja v a 2 s .co m*/ final Path tempDirectory = Files.createTempDirectory(Paths.get(System.getProperty("java.io.tmpdir")), "ds3"); final Path realDirectory = Files.createDirectory(Paths.get(tempDirectory.toString(), "dir")); final Path symbolicDirectory = Paths.get(tempDirectory.toString(), "symbolic"); Files.createSymbolicLink(symbolicDirectory, realDirectory); Files.createFile(Paths.get(realDirectory.toString(), file)); final ByteBuffer bb = ByteBuffer.wrap(message.getBytes()); final SeekableByteChannel getterChannel = new FileObjectGetter(symbolicDirectory).buildChannel(file); getterChannel.write(bb); getterChannel.close(); final String content = new String(Files.readAllBytes(Paths.get(realDirectory.toString(), file))); assertTrue(message.equals(content)); } catch (final IOException e) { fail("Symbolic links are not handled correctly"); } }
From source file:com.sastix.cms.server.services.content.impl.HashedDirectoryServiceImpl.java
/** * Writes file to disk and copy the contents of the input byte array. * * @param file a Path with file path. * @param contents a byte[] with the contents * @throws IOException//from w ww . j a va 2s . c o m */ private void writeFile(final Path file, final byte[] contents) throws IOException { final ByteBuffer bb = ByteBuffer.wrap(contents); //Create the file final SeekableByteChannel sbc = Files.newByteChannel(file, FILE_OPEN_OPTIONS); //Copy contents to the new File sbc.write(bb); //Close the byte channel sbc.close(); }
From source file:com.sastix.cms.server.services.content.impl.HashedDirectoryServiceImpl.java
private void replaceFile(final Path file, final byte[] contents) throws IOException { final ByteBuffer bb = ByteBuffer.wrap(contents); //Create the file final SeekableByteChannel sbc = Files.newByteChannel(file, FILE_REPLACE_OPTIONS); //Copy contents to the new File sbc.write(bb); //Close the byte channel sbc.close();//from ww w .j av a2s. c o m }
From source file:com.sastix.cms.server.services.content.impl.HashedDirectoryServiceImpl.java
private void replaceFile(final Path file, final URL url) throws IOException { //Create the file SeekableByteChannel sbc = null; try {/*from w w w. ja va 2 s .c o m*/ //Creates a new Readable Byte channel from URL final ReadableByteChannel rbc = Channels.newChannel(url.openStream()); //Create the file sbc = Files.newByteChannel(file, FILE_REPLACE_OPTIONS); //Clears the buffer buffer.clear(); //Read input Channel while (rbc.read(buffer) != -1) { // prepare the buffer to be drained buffer.flip(); // write to the channel, may block sbc.write(buffer); // If partial transfer, shift remainder down // If buffer is empty, same as doing clear() buffer.compact(); } // EOF will leave buffer in fill state buffer.flip(); // make sure the buffer is fully drained. while (buffer.hasRemaining()) { sbc.write(buffer); } } finally { if (sbc != null) { sbc.close(); } } }
From source file:com.sastix.cms.server.services.content.impl.HashedDirectoryServiceImpl.java
/** * Writes file to disk and copy the contents of the input byte array. * * @param file a Path with file path.//w ww . j a v a2 s .c o m * @param url a remote/local url to be saved as file * @throws IOException */ private void writeFile(final Path file, final URL url) throws IOException { if (url.toString().startsWith("jar:file:///")) { try { writeZipFile(file, url); } catch (Exception e) { throw new IOException(e); } } else { //Create the file SeekableByteChannel sbc = null; try { //Creates a new Readable Byte channel from URL final ReadableByteChannel rbc = Channels.newChannel(url.openStream()); //Create the file sbc = Files.newByteChannel(file, FILE_OPEN_OPTIONS); //Clears the buffer buffer.clear(); //Read input Channel while (rbc.read(buffer) != -1) { // prepare the buffer to be drained buffer.flip(); // write to the channel, may block sbc.write(buffer); // If partial transfer, shift remainder down // If buffer is empty, same as doing clear() buffer.compact(); } // EOF will leave buffer in fill state buffer.flip(); // make sure the buffer is fully drained. while (buffer.hasRemaining()) { sbc.write(buffer); } } finally { if (sbc != null) { sbc.close(); } } } }
From source file:org.cryptomator.crypto.aes256.Aes256Cryptor.java
@Override public Long encryptFile(InputStream plaintextFile, SeekableByteChannel encryptedFile) throws IOException { // truncate file encryptedFile.truncate(0);/* www.j a v a2s . c o m*/ // use an IV, whose last 8 bytes store a long used in counter mode and write initial value to file. final ByteBuffer countingIv = ByteBuffer.wrap(randomData(AES_BLOCK_LENGTH)); countingIv.putLong(AES_BLOCK_LENGTH - Long.BYTES, 0l); countingIv.position(0); encryptedFile.write(countingIv); // init crypto stuff: final Mac mac = this.hmacSha256(hMacMasterKey); final Cipher cipher = this.aesCtrCipher(primaryMasterKey, countingIv.array(), Cipher.ENCRYPT_MODE); // init mac buffer and skip 32 bytes final ByteBuffer macBuffer = ByteBuffer.allocate(mac.getMacLength()); encryptedFile.write(macBuffer); // init filesize buffer and skip 16 bytes final ByteBuffer encryptedFileSizeBuffer = ByteBuffer.allocate(AES_BLOCK_LENGTH); encryptedFile.write(encryptedFileSizeBuffer); // write content: final OutputStream out = new SeekableByteChannelOutputStream(encryptedFile); final OutputStream macOut = new MacOutputStream(out, mac); final OutputStream cipheredOut = new CipherOutputStream(macOut, cipher); final OutputStream blockSizeBufferedOut = new BufferedOutputStream(cipheredOut, AES_BLOCK_LENGTH); final Long plaintextSize = IOUtils.copyLarge(plaintextFile, blockSizeBufferedOut); // ensure total byte count is a multiple of the block size, in CTR mode: final int remainderToFillLastBlock = AES_BLOCK_LENGTH - (int) (plaintextSize % AES_BLOCK_LENGTH); blockSizeBufferedOut.write(new byte[remainderToFillLastBlock]); // append a few blocks of fake data: final int numberOfPlaintextBlocks = (int) Math.ceil(plaintextSize / AES_BLOCK_LENGTH); final int upToTenPercentFakeBlocks = (int) Math.ceil(Math.random() * 0.1 * numberOfPlaintextBlocks); final byte[] emptyBytes = new byte[AES_BLOCK_LENGTH]; for (int i = 0; i < upToTenPercentFakeBlocks; i += AES_BLOCK_LENGTH) { blockSizeBufferedOut.write(emptyBytes); } blockSizeBufferedOut.flush(); // write MAC of total ciphertext: macBuffer.position(0); macBuffer.put(mac.doFinal()); macBuffer.position(0); encryptedFile.position(16); // right behind the IV encryptedFile.write(macBuffer); // 256 bit MAC // encrypt and write plaintextSize try { final ByteBuffer fileSizeBuffer = ByteBuffer.allocate(Long.BYTES); fileSizeBuffer.putLong(plaintextSize); final Cipher sizeCipher = aesEcbCipher(primaryMasterKey, Cipher.ENCRYPT_MODE); final byte[] encryptedFileSize = sizeCipher.doFinal(fileSizeBuffer.array()); encryptedFileSizeBuffer.position(0); encryptedFileSizeBuffer.put(encryptedFileSize); encryptedFileSizeBuffer.position(0); encryptedFile.position(48); // right behind the IV and MAC encryptedFile.write(encryptedFileSizeBuffer); } catch (IllegalBlockSizeException | BadPaddingException e) { throw new IllegalStateException( "Block size must be valid, as padding is requested. BadPaddingException not possible in encrypt mode.", e); } return plaintextSize; }