Example usage for java.nio.channels ReadableByteChannel close

List of usage examples for java.nio.channels ReadableByteChannel close

Introduction

In this page you can find the example usage for java.nio.channels ReadableByteChannel close.

Prototype

public void close() throws IOException;

Source Link

Document

Closes this channel.

Usage

From source file:com.cloud.maint.UpgradeManagerImpl.java

public String deployNewAgent(String url) {
    s_logger.info("Updating agent with binary from " + url);

    final HttpClient client = new HttpClient(s_httpClientManager);
    final GetMethod method = new GetMethod(url);
    int response;
    File file = null;//from  w w  w .ja v a2s. co  m
    try {
        response = client.executeMethod(method);
        if (response != HttpURLConnection.HTTP_OK) {
            s_logger.warn("Retrieving the agent gives response code: " + response);
            return "Retrieving the file from " + url + " got response code: " + response;
        }

        final InputStream is = method.getResponseBodyAsStream();
        file = File.createTempFile("agent-", "-" + Long.toString(new Date().getTime()));
        file.deleteOnExit();

        s_logger.debug("Retrieving new agent into " + file.getAbsolutePath());

        final FileOutputStream fos = new FileOutputStream(file);

        final ByteBuffer buffer = ByteBuffer.allocate(2048);
        final ReadableByteChannel in = Channels.newChannel(is);
        final WritableByteChannel out = fos.getChannel();

        while (in.read(buffer) != -1) {
            buffer.flip();
            out.write(buffer);
            buffer.clear();
        }

        in.close();
        out.close();

        s_logger.debug("New Agent zip file is now retrieved");
    } catch (final HttpException e) {
        return "Unable to retrieve the file from " + url;
    } catch (final IOException e) {
        return "Unable to retrieve the file from " + url;
    } finally {
        method.releaseConnection();
    }

    file.delete();

    return "File will be deployed.";
}

From source file:com.slytechs.capture.FileFactory.java

public FormatType.Detail formatTypeDetail(final File file) throws IOException {

    try {//from w  w  w  .j a  va2s  .  com
        new PcapFileCapture(file, FileMode.ReadOnlyNoMap, null).close();

        if (logger.isTraceEnabled()) {
            logger.trace(file.getName() + ", type=" + FormatType.Pcap);
        }

        return new DefaultFormatTypeDetail(FormatType.Pcap);
    } catch (final Exception e) {
    }

    try {
        new SnoopFileCapture(file, FileMode.ReadOnlyNoMap, null).close();

        if (logger.isTraceEnabled()) {
            logger.trace(file.getName() + ", type=" + FormatType.Pcap);
        }

        return new DefaultFormatTypeDetail(FormatType.Snoop);
    } catch (final Exception e) {
    }

    /*
     * Now try InputCapture which may also yield a known format
     */
    ReadableByteChannel channel = new RandomAccessFile(file, "r").getChannel();
    FormatType.Detail detail = formatTypeDetail(channel);
    channel.close();

    if (logger.isTraceEnabled()) {
        logger.trace(file.getName() + ", type=" + FormatType.Pcap + ", detail=" + detail);
    }

    return detail;
}

From source file:com.feilong.commons.core.io.IOWriteUtil.java

/**
 * NIO API ?? ()./* ww  w. j  av  a 2 s. c om*/
 *
 * @param bufferLength
 *            the buffer length
 * @param inputStream
 *            the input stream
 * @param outputStream
 *            the output stream
 * @throws UncheckedIOException
 *             the unchecked io exception
 * @since 1.0.8
 */
private static void writeUseNIO(int bufferLength, InputStream inputStream, OutputStream outputStream)
        throws UncheckedIOException {
    int i = 0;
    int sumSize = 0;
    int j = 0;

    ///2 
    //As creme de la creme with regard to performance, you could use NIO Channels and ByteBuffer. 

    ReadableByteChannel readableByteChannel = Channels.newChannel(inputStream);
    WritableByteChannel writableByteChannel = Channels.newChannel(outputStream);

    ByteBuffer byteBuffer = ByteBuffer.allocate(bufferLength);

    try {
        while (readableByteChannel.read(byteBuffer) != -1) {
            byteBuffer.flip();
            j = writableByteChannel.write(byteBuffer);
            sumSize += j;
            byteBuffer.clear();
            i++;
        }

        if (log.isDebugEnabled()) {
            log.debug("Write data over,sumSize:[{}],bufferLength:[{}],loopCount:[{}]",
                    FileUtil.formatSize(sumSize), bufferLength, i);
        }
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    } finally {
        try {
            if (writableByteChannel != null) {
                outputStream.close();
                writableByteChannel.close();
            }
            if (readableByteChannel != null) {
                inputStream.close();
                readableByteChannel.close();
            }
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }
}

From source file:com.thinkberg.vfs.s3.jets3t.Jets3tFileObject.java

protected InputStream doGetInputStream() throws Exception {
    if (!contentCached) {
        object = service.getObject(bucket, getS3Key());
        LOG.debug(String.format("caching content of '%s'", object.getKey()));

        InputStream objectInputStream = object.getDataInputStream();
        if (object.getContentLength() > 0) {
            ReadableByteChannel rbc = Channels.newChannel(objectInputStream);
            FileChannel cacheFc = getCacheFile().getChannel();
            cacheFc.transferFrom(rbc, 0, object.getContentLength());
            cacheFc.close();/*  w w w.j av a2s  . c o  m*/
            rbc.close();
        } else {
            objectInputStream.close();
        }
        contentCached = true;
    }

    return Channels.newInputStream(getCacheFile().getChannel());
}

From source file:org.canova.image.lfw.LFWLoader.java

public void getIfNotExists() throws Exception {
    if (!lfwDir.exists()) {
        lfwDir.mkdir();/*from  www .  j  a va  2  s  .  com*/
        log.info("Grabbing LFW...");

        URL website = new URL(LFW_URL);
        ReadableByteChannel rbc = Channels.newChannel(website.openStream());
        if (!lfwTarFile.exists())
            lfwTarFile.createNewFile();
        FileOutputStream fos = new FileOutputStream(lfwTarFile);
        fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
        fos.flush();
        IOUtils.closeQuietly(fos);
        rbc.close();
        log.info("Downloaded lfw");
        untarFile(baseDir, lfwTarFile);

    }

    File firstImage = null;
    try {
        firstImage = lfwDir.listFiles()[0].listFiles()[0];

    } catch (Exception e) {
        FileUtils.deleteDirectory(lfwDir);
        log.warn("Error opening first image; probably corrupt download...trying again", e);
        getIfNotExists();

    }

    //number of input neurons
    numPixelColumns = ArrayUtil.flatten(loader.fromFile(firstImage)).length;

    //each subdir is a person
    numNames = lfwDir.getAbsoluteFile().listFiles().length;

    @SuppressWarnings("unchecked")
    Collection<File> allImages = FileUtils.listFiles(lfwDir,
            org.apache.commons.io.filefilter.FileFileFilter.FILE,
            org.apache.commons.io.filefilter.DirectoryFileFilter.DIRECTORY);
    for (File f : allImages) {
        images.add(f.getAbsolutePath());
    }
    for (File dir : lfwDir.getAbsoluteFile().listFiles())
        outcomes.add(dir.getAbsolutePath());

}

From source file:org.geowebcache.layer.wms.WMSHttpHelper.java

/**
 * Executes the actual HTTP request, checks the response headers (status and MIME) and
 * //  w w w .ja  va  2 s .c  o m
 * @param tileRespRecv
 * @param wmsBackendUrl
 * @param data
 * @param wmsparams
 * @return
 * @throws GeoWebCacheException
 */
private void connectAndCheckHeaders(TileResponseReceiver tileRespRecv, URL wmsBackendUrl,
        Map<String, String> wmsParams, String requestMime, Integer backendTimeout, Resource target)
        throws GeoWebCacheException {

    GetMethod getMethod = null;
    final int responseCode;
    final int responseLength;

    try { // finally
        try {
            getMethod = executeRequest(wmsBackendUrl, wmsParams, backendTimeout);
            responseCode = getMethod.getStatusCode();
            responseLength = (int) getMethod.getResponseContentLength();

            // Do not set error at this stage
        } catch (IOException ce) {
            if (log.isDebugEnabled()) {
                String message = "Error forwarding request " + wmsBackendUrl.toString();
                log.debug(message, ce);
            }
            throw new GeoWebCacheException(ce);
        }
        // Check that the response code is okay
        tileRespRecv.setStatus(responseCode);
        if (responseCode != 200 && responseCode != 204) {
            tileRespRecv.setError();
            throw new ServiceException("Unexpected response code from backend: " + responseCode + " for "
                    + wmsBackendUrl.toString());
        }

        // Check that we're not getting an error MIME back.
        String responseMime = getMethod.getResponseHeader("Content-Type").getValue();

        if (responseCode != 204 && responseMime != null && !mimeStringCheck(requestMime, responseMime)) {
            String message = null;
            if (responseMime.equalsIgnoreCase(ErrorMime.vnd_ogc_se_inimage.getFormat())) {
                // TODO: revisit: I don't understand why it's trying to create a String message
                // out of an ogc_se_inimage response?
                InputStream stream = null;
                try {
                    stream = getMethod.getResponseBodyAsStream();
                    byte[] error = IOUtils.toByteArray(stream);
                    message = new String(error);
                } catch (IOException ioe) {
                    // Do nothing
                } finally {
                    IOUtils.closeQuietly(stream);
                }
            } else if (responseMime != null
                    && responseMime.toLowerCase().startsWith("application/vnd.ogc.se_xml")) {
                InputStream stream = null;
                try {
                    stream = getMethod.getResponseBodyAsStream();
                    message = IOUtils.toString(stream);
                } catch (IOException e) {
                    //
                } finally {
                    IOUtils.closeQuietly(stream);
                }
            }
            String msg = "MimeType mismatch, expected " + requestMime + " but got " + responseMime + " from "
                    + wmsBackendUrl.toString() + (message == null ? "" : (":\n" + message));
            tileRespRecv.setError();
            tileRespRecv.setErrorMessage(msg);
            log.warn(msg);
        }

        // Everything looks okay, try to save expiration
        if (tileRespRecv.getExpiresHeader() == GWCVars.CACHE_USE_WMS_BACKEND_VALUE) {
            String expireValue = getMethod.getResponseHeader("Expires").getValue();
            long expire = ServletUtils.parseExpiresHeader(expireValue);
            if (expire != -1) {
                tileRespRecv.setExpiresHeader(expire / 1000);
            }
        }

        // Read the actual data
        if (responseCode != 204) {
            try {
                InputStream inStream = getMethod.getResponseBodyAsStream();
                if (inStream == null) {
                    String uri = getMethod.getURI().getURI();
                    log.error("No response for " + getMethod.getName() + " " + uri);
                } else {
                    ReadableByteChannel channel = Channels.newChannel(inStream);
                    try {
                        target.transferFrom(channel);
                    } finally {
                        channel.close();
                    }
                }
                if (responseLength > 0) {
                    int readAccu = (int) target.getSize();
                    if (readAccu != responseLength) {
                        tileRespRecv.setError();
                        throw new GeoWebCacheException(
                                "Responseheader advertised " + responseLength + " bytes, but only received "
                                        + readAccu + " from " + wmsBackendUrl.toString());
                    }
                }
            } catch (IOException ioe) {
                tileRespRecv.setError();
                log.error("Caught IO exception, " + wmsBackendUrl.toString() + " " + ioe.getMessage());
            }
        }

    } finally {
        if (getMethod != null) {
            getMethod.releaseConnection();
        }
    }
}

From source file:org.alfresco.repo.content.AbstractReadOnlyContentStoreTest.java

/**
 * Checks that the various methods of obtaining a reader are supported.
 *//*from   w w  w  .  jav a2  s  .co m*/
@Test
public void testGetReaderForExistingContentUrl() throws Exception {
    ContentStore store = getStore();
    String contentUrl = getExistingContentUrl();
    if (contentUrl == null) {
        logger.warn(
                "Store test testGetReaderForExistingContentUrl not possible on " + store.getClass().getName());
        return;
    }
    // Get the reader
    assertTrue("URL returned in set seems to no longer exist", store.exists(contentUrl));
    ContentReader reader = store.getReader(contentUrl);
    assertNotNull("Reader should never be null", reader);
    assertTrue("Reader says content doesn't exist", reader.exists());
    assertFalse("Reader should not be closed before a read", reader.isClosed());
    assertFalse("The reader channel should not be open yet", reader.isChannelOpen());

    // Open the channel
    ReadableByteChannel readChannel = reader.getReadableChannel();
    readChannel.read(ByteBuffer.wrap(new byte[500]));
    assertFalse("Reader should not be closed during a read", reader.isClosed());
    assertTrue("The reader channel should be open during a read", reader.isChannelOpen());

    // Close the channel
    readChannel.close();
    assertTrue("Reader should be closed after a read", reader.isClosed());
    assertFalse("The reader channel should be closed after a read", reader.isChannelOpen());
}

From source file:edu.usc.pgroup.floe.client.FloeClient.java

/**
 * Uploads the file to the coordinator./*from  ww w. jav  a  2  s  .c o  m*/
 * The file is uploaded relative to the coordinator's scratch folder.
 *
 * @param fileName name of the file to be stored on the coordinator.
 * @return the base fileName which may be used for downloading the file
 * later.
 */
public final String uploadFileSync(final String fileName) {
    String baseFile = FilenameUtils.getName(fileName);
    try {
        int fid = getClient().beginFileUpload(baseFile);
        ReadableByteChannel inChannel = Channels.newChannel(new FileInputStream(fileName));
        ByteBuffer buffer = ByteBuffer.allocate(Utils.Constants.BUFFER_SIZE);
        while (inChannel.read(buffer) > 0) {
            buffer.flip();
            getClient().uploadChunk(fid, buffer);
            buffer.clear();
        }
        inChannel.close();
        getClient().finishUpload(fid);
    } catch (TException e) {
        LOGGER.error(e.getMessage());
        throw new RuntimeException(e);
    } catch (FileNotFoundException e) {
        LOGGER.error(e.getMessage());
        throw new RuntimeException(e);
    } catch (IOException e) {
        LOGGER.error(e.getMessage());
        throw new RuntimeException(e);
    }
    return baseFile;
}

From source file:io.github.microcks.service.ServiceService.java

/** Download a remote HTTP URL into a temporary local file. */
private String handleRemoteFileDownload(String remoteUrl) throws IOException {
    // Build remote Url and local file.
    URL website = new URL(remoteUrl);
    String localFile = System.getProperty("java.io.tmpdir") + "/microcks-" + System.currentTimeMillis()
            + ".project";
    // Set authenticator instance.
    Authenticator.setDefault(new UsernamePasswordAuthenticator(username, password));
    ReadableByteChannel rbc = null;
    FileOutputStream fos = null;/*  www .  j a  va2s.  c  om*/
    try {
        rbc = Channels.newChannel(website.openStream());
        // Transfer file to local.
        fos = new FileOutputStream(localFile);
        fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
    } finally {
        if (fos != null)
            fos.close();
        if (rbc != null)
            rbc.close();
    }
    return localFile;
}

From source file:org.callimachusproject.server.AccessLog.java

InputStream logOnClose(final String addr, final String username, final String line, final int code,
        final long length, final Header referer, final Header agent, InputStream in) {
    final ReadableByteChannel delegate = ChannelUtil.newChannel(in);
    return ChannelUtil.newInputStream(new ReadableByteChannel() {
        private long size = 0;
        private boolean complete;
        private boolean error;

        public boolean isOpen() {
            return delegate.isOpen();
        }//from   w w w  . j ava 2  s  .co  m

        public synchronized void close() throws IOException {
            delegate.close();
            if (!complete) {
                complete = true;
                if (error) {
                    log(addr, username, line, 599, size, referer, agent);
                } else if (size < length) {
                    log(addr, username, line, 499, size, referer, agent);
                } else {
                    log(addr, username, line, code, size, referer, agent);
                }
            }
        }

        public synchronized int read(ByteBuffer dst) throws IOException {
            error = true;
            int read = delegate.read(dst);
            if (read < 0) {
                complete = true;
                log(addr, username, line, code, size, referer, agent);
            } else {
                size += read;
            }
            error = false;
            return read;
        }
    });
}