List of usage examples for java.nio.channels SeekableByteChannel read
@Override int read(ByteBuffer dst) throws IOException;
From source file:Main.java
public static void readData(SeekableByteChannel seekableChannel, Charset cs) throws IOException { ByteBuffer byteBuffer = ByteBuffer.allocate(128); String encoding = System.getProperty("file.encoding"); while (seekableChannel.read(byteBuffer) > 0) { byteBuffer.rewind();//from w w w . j a va 2 s . c o m CharBuffer charBuffer = cs.decode(byteBuffer); System.out.print(charBuffer); byteBuffer.flip(); } }
From source file:internal.diff.aws.service.AmazonS3ETagFileChecksumServiceImpl.java
@Override public String calculateChecksum(Path file) throws IOException { long fileSize = Files.size(file); int parts = (int) (fileSize / S3_MULTIPART_SIZE_LIMIT_IN_BYTES); parts += fileSize % S3_MULTIPART_SIZE_LIMIT_IN_BYTES > 0 ? 1 : 0; ByteBuffer checksumBuffer = ByteBuffer.allocate(parts * 16); SeekableByteChannel byteChannel = Files.newByteChannel(file); for (int part = 0; part < parts; part++) { int partSizeInBytes; if (part < parts - 1 || fileSize % S3_MULTIPART_SIZE_LIMIT_IN_BYTES == 0) { partSizeInBytes = S3_MULTIPART_SIZE_LIMIT_IN_BYTES; } else {//w ww. ja va2 s . c om partSizeInBytes = (int) (fileSize % S3_MULTIPART_SIZE_LIMIT_IN_BYTES); } ByteBuffer partBuffer = ByteBuffer.allocate(partSizeInBytes); boolean endOfFile; do { endOfFile = byteChannel.read(partBuffer) == -1; } while (!endOfFile && partBuffer.hasRemaining()); checksumBuffer.put(DigestUtils.md5(partBuffer.array())); } if (parts > 1) { return DigestUtils.md5Hex(checksumBuffer.array()) + "-" + parts; } else { return Hex.encodeHexString(checksumBuffer.array()); } }
From source file:org.cryptomator.crypto.aes256.Aes256Cryptor.java
@Override public Long decryptedContentLength(SeekableByteChannel encryptedFile) throws IOException { // skip 128bit IV + 256 bit MAC: encryptedFile.position(48);//w w w. j a v a 2 s . co m // read encrypted value: final ByteBuffer encryptedFileSizeBuffer = ByteBuffer.allocate(AES_BLOCK_LENGTH); final int numFileSizeBytesRead = encryptedFile.read(encryptedFileSizeBuffer); // return "unknown" value, if EOF if (numFileSizeBytesRead != encryptedFileSizeBuffer.capacity()) { return null; } // decrypt size: try { final Cipher sizeCipher = aesEcbCipher(primaryMasterKey, Cipher.DECRYPT_MODE); final byte[] decryptedFileSize = sizeCipher.doFinal(encryptedFileSizeBuffer.array()); final ByteBuffer fileSizeBuffer = ByteBuffer.wrap(decryptedFileSize); return fileSizeBuffer.getLong(); } catch (IllegalBlockSizeException | BadPaddingException e) { throw new IllegalStateException(e); } }
From source file:com.arpnetworking.metrics.common.tailer.StatefulTailer.java
private boolean readLines(final SeekableByteChannel reader) throws IOException { // Compute the hash if not already set if (!_hash.isPresent() && reader.size() >= REQUIRED_BYTES_FOR_HASH) { _hash = computeHash(reader, REQUIRED_BYTES_FOR_HASH); }/*from www . j a v a 2s . c o m*/ // Track current position in file and next read position // NOTE: The next read position is always the beginning of a line long position = reader.position(); long nextReadPosition = position; // Reset buffers _buffer.clear(); _lineBuffer.reset(); // Process available data int bufferSize = reader.read(_buffer); boolean hasData = false; boolean hasCR = false; while (isRunning() && bufferSize != -1) { hasData = true; for (int i = 0; i < bufferSize; i++) { final byte ch = _buffer.get(i); switch (ch) { case '\n': hasCR = false; handleLine(); nextReadPosition = position + i + 1; updateCheckpoint(nextReadPosition); break; case '\r': if (hasCR) { _lineBuffer.write('\r'); } hasCR = true; break; default: if (hasCR) { hasCR = false; handleLine(); nextReadPosition = position + i + 1; updateCheckpoint(nextReadPosition); } _lineBuffer.write(ch); } } position = reader.position(); _buffer.clear(); bufferSize = reader.read(_buffer); } reader.position(nextReadPosition); return hasData; }
From source file:com.arpnetworking.tsdcore.tailer.StatefulTailer.java
private Optional<String> computeHash(final SeekableByteChannel reader, final int hashSize) throws IOException { // Don't hash empty data sets if (hashSize <= 0) { return Optional.absent(); }/*w w w. j a v a2s. co m*/ // Validate sufficient data to compute the hash final long oldPosition = reader.position(); reader.position(0); if (reader.size() < hashSize) { reader.position(oldPosition); LOGGER.trace(String.format("Reader size insufficient to compute hash; hashSize=%s, hashSize=%d", Integer.valueOf(hashSize), Long.valueOf(reader.size()))); return Optional.absent(); } // Read the data to hash final ByteBuffer buffer = ByteBuffer.allocate(hashSize); int totalBytesRead = 0; while (totalBytesRead < hashSize) { final int bytesRead = reader.read(buffer); if (bytesRead < 0) { LOGGER.warn(String.format("Unexpected end of file reached; totalBytesRead=%d", Long.valueOf(totalBytesRead))); return Optional.absent(); } totalBytesRead += bytesRead; } // Compute the hash _md5.reset(); final byte[] digest = _md5.digest(buffer.array()); final String hash = Hex.encodeHexString(digest); LOGGER.trace(String.format("Computed hash; hash=%s, bytes=%s", hash, Hex.encodeHexString(buffer.array()))); // Return the reader to its original state reader.position(oldPosition); return Optional.of(hash); }
From source file:com.arpnetworking.metrics.common.tailer.StatefulTailer.java
private Optional<String> computeHash(final SeekableByteChannel reader, final int hashSize) throws IOException { // Don't hash empty data sets if (hashSize <= 0) { return Optional.empty(); }//from w ww .ja va 2s.c o m // Validate sufficient data to compute the hash final long oldPosition = reader.position(); reader.position(0); if (reader.size() < hashSize) { reader.position(oldPosition); LOGGER.trace().setMessage("Reader size insufficient to compute hash").addData("hashSize", hashSize) .addData("readerSize", reader.size()).log(); return Optional.empty(); } // Read the data to hash final ByteBuffer buffer = ByteBuffer.allocate(hashSize); int totalBytesRead = 0; while (totalBytesRead < hashSize) { final int bytesRead = reader.read(buffer); if (bytesRead < 0) { LOGGER.warn().setMessage("Unexpected end of file reached").addData("totalBytesRead", totalBytesRead) .log(); return Optional.empty(); } totalBytesRead += bytesRead; } // Compute the hash _md5.reset(); final byte[] digest = _md5.digest(buffer.array()); final String hash = Hex.encodeHexString(digest); LOGGER.trace().setMessage("Computed hash").addData("hash", hash).log(); // Return the reader to its original state reader.position(oldPosition); return Optional.of(hash); }
From source file:io.neba.core.logviewer.Tail.java
@Override public void run() { SeekableByteChannel channel = null; try {/*from w w w . j ava 2s . com*/ channel = newByteChannel(this.file.toPath(), READ); long availableInByte = this.file.length(); long startingFromInByte = max(availableInByte - this.bytesToTail, 0); channel.position(startingFromInByte); long position = startingFromInByte; long totalBytesRead = 0L; // Read up to this amount of data from the file at once. ByteBuffer readBuffer = allocate(4096); while (!this.stopped) { // The file might be temporarily gone during rotation. Wait, then decide // whether the file is considered gone permanently or whether a rotation has occurred. if (!this.file.exists()) { sleep(AWAIT_FILE_ROTATION_MILLIS); } if (!this.file.exists()) { this.remoteEndpoint.sendString("file not found"); return; } if (position > this.file.length()) { this.remoteEndpoint.sendString("file rotated"); position = 0; closeQuietly(channel); channel = newByteChannel(this.file.toPath(), READ); } int read = channel.read(readBuffer); if (read == -1) { if (mode == TAIL) { // EOF, we are done. return; } // If we are in follow mode, reaching the end of the file might signal a file rotation. Sleep and re-try. sleep(TAIL_CHECK_INTERVAL_MILLIS); continue; } totalBytesRead += read; position = channel.position(); readBuffer.flip(); this.remoteEndpoint.sendBytes(readBuffer); readBuffer.clear(); if (mode == TAIL && totalBytesRead >= this.bytesToTail) { return; } } } catch (IOException e) { this.logger.error("Unable to tail " + this.file.getAbsolutePath() + ".", e); } catch (InterruptedException e) { if (!this.stopped) { this.logger.error("Stopped tailing " + this.file.getAbsolutePath() + ", got interrupted.", e); } } finally { closeQuietly(channel); } }
From source file:org.cryptomator.crypto.aes256.Aes256Cryptor.java
@Override public boolean authenticateContent(SeekableByteChannel encryptedFile) throws IOException { // init mac:/*from www. j av a 2 s .com*/ final Mac calculatedMac = this.hmacSha256(hMacMasterKey); // read stored mac: encryptedFile.position(16); final ByteBuffer storedMac = ByteBuffer.allocate(calculatedMac.getMacLength()); final int numMacBytesRead = encryptedFile.read(storedMac); // check validity of header: if (numMacBytesRead != calculatedMac.getMacLength()) { throw new IOException("Failed to read file header."); } // read all encrypted data and calculate mac: encryptedFile.position(64); final InputStream in = new SeekableByteChannelInputStream(encryptedFile); final InputStream macIn = new MacInputStream(in, calculatedMac); IOUtils.copyLarge(macIn, new NullOutputStream()); // compare (in constant time): return MessageDigest.isEqual(storedMac.array(), calculatedMac.doFinal()); }
From source file:org.cryptomator.crypto.aes256.Aes256Cryptor.java
@Override public Long decryptedFile(SeekableByteChannel encryptedFile, OutputStream plaintextFile) throws IOException { // read iv://from ww w. j ava 2 s . c om encryptedFile.position(0); final ByteBuffer countingIv = ByteBuffer.allocate(AES_BLOCK_LENGTH); final int numIvBytesRead = encryptedFile.read(countingIv); // read file size: final Long fileSize = decryptedContentLength(encryptedFile); // check validity of header: if (numIvBytesRead != AES_BLOCK_LENGTH || fileSize == null) { throw new IOException("Failed to read file header."); } // go to begin of content: encryptedFile.position(64); // generate cipher: final Cipher cipher = this.aesCtrCipher(primaryMasterKey, countingIv.array(), Cipher.DECRYPT_MODE); // read content final InputStream in = new SeekableByteChannelInputStream(encryptedFile); final InputStream cipheredIn = new CipherInputStream(in, cipher); return IOUtils.copyLarge(cipheredIn, plaintextFile, 0, fileSize); }