Search in sources :

Example 71 with ReadableByteChannel

use of java.nio.channels.ReadableByteChannel in project vespa by vespa-engine.

the class SerializeAnnotationsTestCase method readFile.

private static ByteBuffer readFile(String fileName) throws IOException {
    fileName = PATH + fileName;
    // read tree from disk
    ReadableByteChannel channel = new FileInputStream(fileName).getChannel();
    ByteBuffer readBuf = ByteBuffer.allocate(4096);
    channel.read(readBuf);
    readBuf.flip();
    channel.close();
    return readBuf;
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) ByteBuffer(java.nio.ByteBuffer) GrowableByteBuffer(com.yahoo.io.GrowableByteBuffer) FileInputStream(java.io.FileInputStream)

Example 72 with ReadableByteChannel

use of java.nio.channels.ReadableByteChannel in project camunda-bpm-platform by camunda.

the class TestHelper method isEqual.

protected static boolean isEqual(InputStream stream1, InputStream stream2) throws IOException {
    ReadableByteChannel channel1 = Channels.newChannel(stream1);
    ReadableByteChannel channel2 = Channels.newChannel(stream2);
    ByteBuffer buffer1 = ByteBuffer.allocateDirect(1024);
    ByteBuffer buffer2 = ByteBuffer.allocateDirect(1024);
    try {
        while (true) {
            int bytesReadFromStream1 = channel1.read(buffer1);
            int bytesReadFromStream2 = channel2.read(buffer2);
            if (bytesReadFromStream1 == -1 || bytesReadFromStream2 == -1)
                return bytesReadFromStream1 == bytesReadFromStream2;
            buffer1.flip();
            buffer2.flip();
            for (int i = 0; i < Math.min(bytesReadFromStream1, bytesReadFromStream2); i++) if (buffer1.get() != buffer2.get())
                return false;
            buffer1.compact();
            buffer2.compact();
        }
    } finally {
        if (stream1 != null)
            stream1.close();
        if (stream2 != null)
            stream2.close();
    }
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) ByteBuffer(java.nio.ByteBuffer)

Example 73 with ReadableByteChannel

use of java.nio.channels.ReadableByteChannel in project tomcat70 by apache.

the class NioReplicationTask method drainChannel.

/**
 * The actual code which drains the channel associated with
 * the given key.  This method assumes the key has been
 * modified prior to invocation to turn off selection
 * interest in OP_READ.  When this method completes it
 * re-enables OP_READ and calls wakeup() on the selector
 * so the selector will resume watching this channel.
 */
protected void drainChannel(final SelectionKey key, ObjectReader reader) throws Exception {
    reader.setLastAccess(System.currentTimeMillis());
    reader.access();
    ReadableByteChannel channel = (ReadableByteChannel) key.channel();
    int count = -1;
    // make buffer empty
    buffer.clear();
    SocketAddress saddr = null;
    if (channel instanceof SocketChannel) {
        // loop while data available, channel is non-blocking
        while ((count = channel.read(buffer)) > 0) {
            // make buffer readable
            buffer.flip();
            if (buffer.hasArray())
                reader.append(buffer.array(), 0, count, false);
            else
                reader.append(buffer, count, false);
            // make buffer empty
            buffer.clear();
            // do we have at least one package?
            if (reader.hasPackage())
                break;
        }
    } else if (channel instanceof DatagramChannel) {
        DatagramChannel dchannel = (DatagramChannel) channel;
        saddr = dchannel.receive(buffer);
        // make buffer readable
        buffer.flip();
        if (buffer.hasArray())
            reader.append(buffer.array(), 0, buffer.limit() - buffer.position(), false);
        else
            reader.append(buffer, buffer.limit() - buffer.position(), false);
        // make buffer empty
        buffer.clear();
        // did we get a package
        count = reader.hasPackage() ? 1 : -1;
    }
    int pkgcnt = reader.count();
    if (count < 0 && pkgcnt == 0) {
        // end of stream, and no more packages to process
        remoteEof(key);
        return;
    }
    ChannelMessage[] msgs = pkgcnt == 0 ? ChannelData.EMPTY_DATA_ARRAY : reader.execute();
    // register to read new data, before we send it off to avoid dead locks
    registerForRead(key, reader);
    for (int i = 0; i < msgs.length; i++) {
        /**
         * Use send ack here if you want to ack the request to the remote
         * server before completing the request
         * This is considered an asynchronous request
         */
        if (ChannelData.sendAckAsync(msgs[i].getOptions()))
            sendAck(key, (WritableByteChannel) channel, Constants.ACK_COMMAND, saddr);
        try {
            if (Logs.MESSAGES.isTraceEnabled()) {
                try {
                    Logs.MESSAGES.trace("NioReplicationThread - Received msg:" + new UniqueId(msgs[i].getUniqueId()) + " at " + new java.sql.Timestamp(System.currentTimeMillis()));
                } catch (Throwable t) {
                }
            }
            // process the message
            getCallback().messageDataReceived(msgs[i]);
            /**
             * Use send ack here if you want the request to complete on this
             * server before sending the ack to the remote server
             * This is considered a synchronized request
             */
            if (ChannelData.sendAckSync(msgs[i].getOptions()))
                sendAck(key, (WritableByteChannel) channel, Constants.ACK_COMMAND, saddr);
        } catch (RemoteProcessException e) {
            if (log.isDebugEnabled())
                log.error("Processing of cluster message failed.", e);
            if (ChannelData.sendAckSync(msgs[i].getOptions()))
                sendAck(key, (WritableByteChannel) channel, Constants.FAIL_ACK_COMMAND, saddr);
        } catch (Exception e) {
            log.error("Processing of cluster message failed.", e);
            if (ChannelData.sendAckSync(msgs[i].getOptions()))
                sendAck(key, (WritableByteChannel) channel, Constants.FAIL_ACK_COMMAND, saddr);
        }
        if (getUseBufferPool()) {
            BufferPool.getBufferPool().returnBuffer(msgs[i].getMessage());
            msgs[i].setMessage(null);
        }
    }
    if (count < 0) {
        remoteEof(key);
        return;
    }
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) SocketChannel(java.nio.channels.SocketChannel) UniqueId(org.apache.catalina.tribes.UniqueId) DatagramChannel(java.nio.channels.DatagramChannel) WritableByteChannel(java.nio.channels.WritableByteChannel) RemoteProcessException(org.apache.catalina.tribes.RemoteProcessException) CancelledKeyException(java.nio.channels.CancelledKeyException) IOException(java.io.IOException) ChannelMessage(org.apache.catalina.tribes.ChannelMessage) RemoteProcessException(org.apache.catalina.tribes.RemoteProcessException) SocketAddress(java.net.SocketAddress)

Example 74 with ReadableByteChannel

use of java.nio.channels.ReadableByteChannel in project nd4j by deeplearning4j.

the class BaseLoader method load.

/**
 * Load an ndarray from a blob
 *
 * @param blob the blob to load from
 * @return the loaded ndarray
 */
@Override
public INDArray load(Blob blob) throws SQLException {
    if (blob == null)
        return null;
    try (InputStream is = blob.getBinaryStream()) {
        ByteBuffer direct = ByteBuffer.allocateDirect((int) blob.length());
        ReadableByteChannel readableByteChannel = Channels.newChannel(is);
        readableByteChannel.read(direct);
        Buffer byteBuffer = (Buffer) direct;
        byteBuffer.rewind();
        return BinarySerde.toArray(direct);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : ByteBuffer(java.nio.ByteBuffer) Buffer(java.nio.Buffer) ReadableByteChannel(java.nio.channels.ReadableByteChannel) ByteBuffer(java.nio.ByteBuffer)

Example 75 with ReadableByteChannel

use of java.nio.channels.ReadableByteChannel in project registry by hortonworks.

the class MySqlDriverHelper method downloadMysqlJarAndCopyToLibDir.

/*
      This method is solely for the use of downloading the mysql java driver jar.
      It will download the zip file from the provided url.
      Unzips the file and copies the jar into bootstrap/lib and libs.

      @params url mysql zip file URL
      @returns the mysql jar file name.
     */
public static String downloadMysqlJarAndCopyToLibDir(File bootstrapLibDir, String url, String fileNamePattern, Proxy proxy) throws IOException {
    System.out.println("Downloading mysql jar from url: " + url);
    String tmpFileName;
    try {
        URL downloadUrl = new URL(url);
        if (proxy == null || !Proxy.Type.HTTP.equals(proxy.type())) {
            // defensive coding - if proxy is not set or set to some other type then default it to no proxy
            proxy = Proxy.NO_PROXY;
            System.out.println("Downloading mysql jar without using proxy.");
        } else {
            System.out.println("Downloading mysql jar using http proxy " + proxy);
        }
        String[] pathSegments = downloadUrl.getPath().split("/");
        String fileName = pathSegments[pathSegments.length - 1];
        String tmpDir = System.getProperty("java.io.tmpdir");
        tmpFileName = tmpDir + File.separator + fileName;
        System.out.println("Downloading file " + fileName + " into " + tmpDir);
        // Using openConnection with explicit proxy argument since setting the system property https.proxyHost and https.proxyPort did not mandate the use
        // of proxy. As a result the jar was still downloaded successfully if an invalid proxy server was configured because it was falling on the wifi
        // connection if the machine is connected. It was difficult to verify if the download was actually going through the proxy or not. This seems
        // to be a better approach since passing proxy as an argument now forces it to go through the proxy server and Proxy.NO_PROXY by default works fine.
        ReadableByteChannel rbc = Channels.newChannel(downloadUrl.openConnection(proxy).getInputStream());
        FileOutputStream fos = new FileOutputStream(tmpFileName);
        fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
    } catch (IOException ie) {
        System.err.println("Failed to download the mysql driver from " + url);
        throw new IOException(ie);
    }
    System.out.println("Copying mysql libraries into lib dir...");
    String libDir = bootstrapLibDir.getAbsolutePath() + File.separator + "../../libs/";
    System.out.println("Unzipping downloaded mysql driver and copying");
    try {
        String mysqlJarFileName = MySqlDriverHelper.copyFileFromZipToDir(tmpFileName, fileNamePattern, bootstrapLibDir);
        File bootstrapLibFile = new File(bootstrapLibDir + File.separator + mysqlJarFileName);
        File libFile = new File(libDir + File.separator + mysqlJarFileName);
        System.out.println("Copying file to libs " + libFile);
        MySqlDriverHelper.copyFile(bootstrapLibFile, libFile);
        return mysqlJarFileName;
    } catch (IOException ie) {
        ie.printStackTrace();
        System.err.println("Failed to copy mysql driver into " + bootstrapLibDir + " and " + libDir);
    } catch (Exception e) {
        e.printStackTrace();
        System.err.println("Failed to copy mysql driver into " + bootstrapLibDir + " and " + libDir);
    }
    return null;
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) ZipFile(java.util.zip.ZipFile) URL(java.net.URL) IOException(java.io.IOException)

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