use of java.nio.channels.ReadableByteChannel in project neo4j by neo4j.
the class PackStreamTest method handlesDataCrossingBufferBoundaries.
@Test
public void handlesDataCrossingBufferBoundaries() throws Throwable {
// Given
Machine machine = new Machine();
PackStream.Packer packer = machine.packer();
packer.pack(Long.MAX_VALUE);
packer.pack(Long.MAX_VALUE);
packer.flush();
ReadableByteChannel ch = Channels.newChannel(new ByteArrayInputStream(machine.output()));
PackStream.Unpacker unpacker = new PackStream.Unpacker(new BufferedChannelInput(11).reset(ch));
// Serialized ch will look like, and misalign with the 11-byte unpack buffer:
// [XX][XX][XX][XX][XX][XX][XX][XX][XX][XX][XX][XX][XX][XX][XX][XX][XX][XX]
// mkr \___________data______________/ mkr \___________data______________/
// \____________unpack buffer_________________/
// When
long first = unpacker.unpackLong();
long second = unpacker.unpackLong();
// Then
assertEquals(Long.MAX_VALUE, first);
assertEquals(Long.MAX_VALUE, second);
}
use of java.nio.channels.ReadableByteChannel in project ACS by ACS-Community.
the class UrlDownloader method download.
/**
* Download the url in the thread.
*
* @return The name of the downloaded file
*/
public String download() throws IOException {
String outFolder = getTempFolder();
String outFName = outFolder + File.separator + fileName;
System.out.println("UrlDownloader: Downloading " + url + " as " + outFName);
ReadableByteChannel rbc = Channels.newChannel(url.openStream());
FileOutputStream fos = null;
try {
fos = new FileOutputStream(outFName);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
} finally {
if (fos != null) {
fos.close();
}
}
System.out.println("UrlDownloader: " + url + " downloaded as " + outFName);
return outFName;
}
use of java.nio.channels.ReadableByteChannel in project alluxio by Alluxio.
the class FileDataManager method persistFile.
/**
* Persists the blocks of a file into the under file system.
*
* @param fileId the id of the file
* @param blockIds the list of block ids
* @throws AlluxioException if an unexpected Alluxio exception is thrown
* @throws IOException if the file persistence fails
*/
public void persistFile(long fileId, List<Long> blockIds) throws AlluxioException, IOException {
Map<Long, Long> blockIdToLockId;
synchronized (mLock) {
blockIdToLockId = mPersistingInProgressFiles.get(fileId);
if (blockIdToLockId == null || !blockIdToLockId.keySet().equals(new HashSet<>(blockIds))) {
throw new IOException("Not all the blocks of file " + fileId + " are locked");
}
}
String dstPath = prepareUfsFilePath(fileId);
UnderFileSystem ufs = UnderFileSystem.Factory.get(dstPath);
FileInfo fileInfo = mBlockWorker.getFileInfo(fileId);
OutputStream outputStream = ufs.create(dstPath, CreateOptions.defaults().setOwner(fileInfo.getOwner()).setGroup(fileInfo.getGroup()).setMode(new Mode((short) fileInfo.getMode())));
final WritableByteChannel outputChannel = Channels.newChannel(outputStream);
List<Throwable> errors = new ArrayList<>();
try {
for (long blockId : blockIds) {
long lockId = blockIdToLockId.get(blockId);
if (Configuration.getBoolean(PropertyKey.WORKER_FILE_PERSIST_RATE_LIMIT_ENABLED)) {
BlockMeta blockMeta = mBlockWorker.getBlockMeta(Sessions.CHECKPOINT_SESSION_ID, blockId, lockId);
mPersistenceRateLimiter.acquire((int) blockMeta.getBlockSize());
}
// obtain block reader
BlockReader reader = mBlockWorker.readBlockRemote(Sessions.CHECKPOINT_SESSION_ID, blockId, lockId);
// write content out
ReadableByteChannel inputChannel = reader.getChannel();
BufferUtils.fastCopy(inputChannel, outputChannel);
reader.close();
}
} catch (BlockDoesNotExistException | InvalidWorkerStateException e) {
errors.add(e);
} finally {
// make sure all the locks are released
for (long lockId : blockIdToLockId.values()) {
try {
mBlockWorker.unlockBlock(lockId);
} catch (BlockDoesNotExistException e) {
errors.add(e);
}
}
// Process any errors
if (!errors.isEmpty()) {
StringBuilder errorStr = new StringBuilder();
errorStr.append("the blocks of file").append(fileId).append(" are failed to persist\n");
for (Throwable e : errors) {
errorStr.append(e).append('\n');
}
throw new IOException(errorStr.toString());
}
}
outputStream.flush();
outputChannel.close();
outputStream.close();
synchronized (mLock) {
mPersistingInProgressFiles.remove(fileId);
mPersistedFiles.add(fileId);
}
}
use of java.nio.channels.ReadableByteChannel in project jdk8u_jdk by JetBrains.
the class ReadOffset method main.
public static void main(String[] args) throws IOException {
ReadableByteChannel rbc = new ReadableByteChannel() {
public int read(ByteBuffer dst) {
dst.put((byte) 0);
return 1;
}
public boolean isOpen() {
return true;
}
public void close() {
}
};
InputStream in = Channels.newInputStream(rbc);
byte[] b = new byte[3];
in.read(b, 0, 1);
// throws IAE
in.read(b, 2, 1);
}
use of java.nio.channels.ReadableByteChannel in project robovm by robovm.
the class ScannerTest method test_ConstructorLjava_nio_channels_ReadableByteChannelLjava_lang_String.
/**
* @tests java.util.Scanner#Scanner(ReadableByteChannel, String)
*/
public void test_ConstructorLjava_nio_channels_ReadableByteChannelLjava_lang_String() throws IOException {
File tmpFile = File.createTempFile("TestFileForScanner", ".tmp");
FileChannel fc = new FileOutputStream(tmpFile).getChannel();
s = new Scanner(fc, Charset.defaultCharset().name());
assertNotNull(s);
s.close();
fc = new FileOutputStream(tmpFile).getChannel();
try {
s = new Scanner(fc, "invalid charset");
fail();
} catch (IllegalArgumentException expected) {
}
fc.close();
assertTrue(tmpFile.delete());
// Scanner(ReadableByteChannel = null, Charset = null)
try {
s = new Scanner((ReadableByteChannel) null, null);
fail();
} catch (NullPointerException expected) {
}
// Scanner(ReadableByteChannel = null, Charset = invalid)
try {
s = new Scanner((ReadableByteChannel) null, "invalid");
fail();
} catch (NullPointerException expected) {
}
// Scanner(ReadableByteChannel, Charset = null)
try {
s = new Scanner(fc, null);
fail();
} catch (IllegalArgumentException expected) {
}
// TODO: test if the specified charset is used.
}
Aggregations