Search in sources :

Example 11 with ReadableByteChannel

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);
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.Test)

Example 12 with ReadableByteChannel

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;
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) FileOutputStream(java.io.FileOutputStream)

Example 13 with ReadableByteChannel

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);
    }
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) OutputStream(java.io.OutputStream) Mode(alluxio.security.authorization.Mode) BlockReader(alluxio.worker.block.io.BlockReader) WritableByteChannel(java.nio.channels.WritableByteChannel) ArrayList(java.util.ArrayList) IOException(java.io.IOException) FileInfo(alluxio.wire.FileInfo) UnderFileSystem(alluxio.underfs.UnderFileSystem) BlockMeta(alluxio.worker.block.meta.BlockMeta) BlockDoesNotExistException(alluxio.exception.BlockDoesNotExistException) InvalidWorkerStateException(alluxio.exception.InvalidWorkerStateException)

Example 14 with ReadableByteChannel

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);
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) InputStream(java.io.InputStream) ByteBuffer(java.nio.ByteBuffer)

Example 15 with ReadableByteChannel

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.
}
Also used : Scanner(java.util.Scanner) ReadableByteChannel(java.nio.channels.ReadableByteChannel) FileChannel(java.nio.channels.FileChannel) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Aggregations

ReadableByteChannel (java.nio.channels.ReadableByteChannel)307 ByteBuffer (java.nio.ByteBuffer)111 IOException (java.io.IOException)84 FileOutputStream (java.io.FileOutputStream)62 WritableByteChannel (java.nio.channels.WritableByteChannel)62 Test (org.junit.Test)52 File (java.io.File)50 FileChannel (java.nio.channels.FileChannel)49 FileInputStream (java.io.FileInputStream)43 ByteArrayInputStream (java.io.ByteArrayInputStream)38 InputStream (java.io.InputStream)36 URL (java.net.URL)35 ByteArrayOutputStream (java.io.ByteArrayOutputStream)21 Path (java.nio.file.Path)18 Test (org.testng.annotations.Test)14 FileNotFoundException (java.io.FileNotFoundException)13 ArrayList (java.util.ArrayList)12 DbusEventGenerator (com.linkedin.databus.core.test.DbusEventGenerator)11 MalformedURLException (java.net.MalformedURLException)11 Vector (java.util.Vector)11