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:net.technicpack.launchercore.util.Download.java

@Override
@SuppressWarnings("unused")
public void run() {
    ReadableByteChannel rbc = null;
    FileOutputStream fos = null;/*from   w ww .j  a  va  2  s .c  om*/
    try {
        HttpURLConnection conn = Utils.openHttpConnection(url);
        int response = conn.getResponseCode();
        int responseFamily = response / 100;

        if (responseFamily == 3) {
            throw new DownloadException(
                    "The server issued a redirect response which Technic failed to follow.");
        } else if (responseFamily != 2) {
            throw new DownloadException("The server issued a " + response + " response code.");
        }

        InputStream in = getConnectionInputStream(conn);

        size = conn.getContentLength();
        outFile = new File(outPath);
        outFile.delete();

        rbc = Channels.newChannel(in);
        fos = new FileOutputStream(outFile);

        stateChanged();

        Thread progress = new MonitorThread(Thread.currentThread(), rbc);
        progress.start();

        fos.getChannel().transferFrom(rbc, 0, size > 0 ? size : Integer.MAX_VALUE);
        in.close();
        rbc.close();
        progress.interrupt();
        if (size > 0) {
            if (size == outFile.length()) {
                result = Result.SUCCESS;
            }
        } else {
            result = Result.SUCCESS;
        }
    } catch (PermissionDeniedException e) {
        exception = e;
        result = Result.PERMISSION_DENIED;
    } catch (DownloadException e) {
        exception = e;
        result = Result.FAILURE;
    } catch (Exception e) {
        exception = e;
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(fos);
        IOUtils.closeQuietly(rbc);
    }
}

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

/**
 * {@inheritDoc}/*from www . java2  s  . c  o m*/
 */
public FileChannel getFileChannel(boolean truncate) throws ContentIOException {
    /*
     * By calling this method, clients indicate that they wish to make random
     * changes to the file.  It is possible that the client might only want
     * to update a tiny proportion of the file (truncate == false) or
     * start afresh (truncate == true).
     * 
     * Where the underlying support is not present for this method, a temporary
     * file will be used as a substitute.  When the write is complete, the
     * results are copied directly to the underlying channel.
     */

    // get the underlying implementation's best writable channel
    channel = getWritableChannel();
    // now use this channel if it can provide the random access, otherwise spoof it
    FileChannel clientFileChannel = null;
    if (channel instanceof FileChannel) {
        // all the support is provided by the underlying implementation
        clientFileChannel = (FileChannel) channel;
        // copy over the existing content, if required
        if (!truncate && existingContentReader != null) {
            ReadableByteChannel existingContentChannel = existingContentReader.getReadableChannel();
            long existingContentLength = existingContentReader.getSize();
            // copy the existing content
            try {
                clientFileChannel.transferFrom(existingContentChannel, 0, existingContentLength);
                // copy complete
                if (logger.isDebugEnabled()) {
                    logger.debug("Copied content for random access: \n" + "   writer: " + this + "\n"
                            + "   existing: " + existingContentReader);
                }
            } catch (IOException e) {
                throw new ContentIOException("Failed to copy from existing content to enable random access: \n"
                        + "   writer: " + this + "\n" + "   existing: " + existingContentReader, e);
            } finally {
                try {
                    existingContentChannel.close();
                } catch (IOException e) {
                }
            }
        }
        // debug
        if (logger.isDebugEnabled()) {
            logger.debug("Content writer provided direct support for FileChannel: \n" + "   writer: " + this);
        }
    } else {
        // No random access support is provided by the implementation.
        // Spoof it by providing a 2-stage write via a temp file
        File tempFile = TempFileProvider.createTempFile("random_write_spoof_", ".bin");
        final FileContentWriter spoofWriter = new FileContentWriter(tempFile, // the file to write to
                getExistingContentReader()); // this ensures that the existing content is pulled in
        // Attach a listener
        // - to ensure that the content gets loaded from the temp file once writing has finished
        // - to ensure that the close call gets passed on to the underlying channel
        ContentStreamListener spoofListener = new ContentStreamListener() {
            public void contentStreamClosed() throws ContentIOException {
                // the spoofed temp channel has been closed, so get a new reader for it
                ContentReader spoofReader = spoofWriter.getReader();
                FileChannel spoofChannel = spoofReader.getFileChannel();
                // upload all the temp content to the real underlying channel
                try {
                    long spoofFileSize = spoofChannel.size();
                    spoofChannel.transferTo(0, spoofFileSize, channel);
                } catch (IOException e) {
                    throw new ContentIOException(
                            "Failed to copy from spoofed temporary channel to permanent channel: \n"
                                    + "   writer: " + this + "\n" + "   temp: " + spoofReader,
                            e);
                } finally {
                    try {
                        spoofChannel.close();
                    } catch (Throwable e) {
                    }
                    try {
                        channel.close();
                    } catch (IOException e) {
                        throw new ContentIOException("Failed to close underlying channel", e);
                    }
                }
            }
        };
        spoofWriter.addListener(spoofListener);
        // we now have the spoofed up channel that the client can work with
        clientFileChannel = spoofWriter.getFileChannel(truncate);
        // debug
        if (logger.isDebugEnabled()) {
            logger.debug("Content writer provided indirect support for FileChannel: \n" + "   writer: " + this
                    + "\n" + "   temp writer: " + spoofWriter);
        }
    }
    // the file is now available for random access
    return clientFileChannel;
}

From source file:org.spoutcraft.launcher.api.util.Download.java

@SuppressWarnings("unused")
public void run() {
    ReadableByteChannel rbc = null;
    FileOutputStream fos = null;//from ww w  .j  av  a2s . co m
    try {
        URLConnection conn = url.openConnection();
        conn.setDoInput(true);
        conn.setDoOutput(false);
        System.setProperty("http.agent",
                "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.162 Safari/535.19");
        conn.setRequestProperty("User-Agent",
                "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.162 Safari/535.19");
        HttpURLConnection.setFollowRedirects(true);
        conn.setUseCaches(false);
        ((HttpURLConnection) conn).setInstanceFollowRedirects(true);
        int response = ((HttpURLConnection) conn).getResponseCode();
        InputStream in = getConnectionInputStream(conn);

        size = conn.getContentLength();
        outFile = new File(outPath);
        outFile.delete();

        rbc = Channels.newChannel(in);
        fos = new FileOutputStream(outFile);

        stateChanged();

        Thread progress = new MonitorThread(Thread.currentThread(), rbc);
        progress.start();

        fos.getChannel().transferFrom(rbc, 0, size > 0 ? size : Integer.MAX_VALUE);
        in.close();
        rbc.close();
        progress.interrupt();
        if (size > 0) {
            if (size == outFile.length()) {
                result = Result.SUCCESS;
            }
        } else {
            result = Result.SUCCESS;
        }
    } catch (PermissionDeniedException e) {
        result = Result.PERMISSION_DENIED;
    } catch (DownloadException e) {
        result = Result.FAILURE;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(fos);
        IOUtils.closeQuietly(rbc);
    }
}

From source file:net.bobah.mail.Dupes.java

@Override
public void run() {
    final Multimap<HashCode, File> dupes = Multimaps.newListMultimap(new HashMap<HashCode, Collection<File>>(),
            new Supplier<List<File>>() {
                @Override//ww  w  .  j  a v  a  2 s.c om
                public List<File> get() {
                    return new LinkedList<File>();
                }
            });

    for (final File dir : dirs) {
        if (!dir.isDirectory()) {
            log.warn("{} does not exist or is not a directory, ignored", dir);
        }

        final Collection<File> files = findFiles(dir, "");
        log.info("found {} files in {}, submitting to analyzer", files.size(), dir.getAbsolutePath());

        for (final File file : files) {
            executor.submit(new Runnable() {
                @Override
                public void run() {
                    final ExecutionContext cxt = Dupes.this.cxt.get();

                    ReadableByteChannel ch = null;
                    try {
                        cxt.sw.start();
                        // map file, take just 1 meg of data to cxt.hash and calc the function
                        // final HashCode code = Files.hash(file, hashfunc);
                        ch = Channels.newChannel(new FileInputStream(file));
                        ByteBuffer buf = ByteBuffer.wrap(cxt.buf);
                        final int len = ch.read(buf);
                        if (len == 0)
                            return;

                        final HashCode code = hashfunc.hashBytes(cxt.buf, 0, Ints.checkedCast(len));
                        synchronized (dupes) {
                            dupes.put(code, file);
                        }

                        cxt.sw.stop();
                        log.debug("{} -> {} ({}) - {} us", file, code,
                                DateFormat.getInstance().format(file.lastModified()),
                                cxt.sw.elapsed(TimeUnit.MILLISECONDS));
                    } catch (Exception e) {
                        log.debug("exception", e);
                    } finally {
                        cxt.recycle();
                        if (ch != null)
                            try {
                                ch.close();
                            } catch (IOException unused) {
                            }
                        ;
                    }
                }
            });
        }
        log.info("done submitting {} to analyzer", dir.getAbsolutePath());
    }

    try {
        shutdownExecutor(executor, log);
    } catch (InterruptedException e) {
        log.debug("exception", e);
    }

    for (Collection<File> filez : dupes.asMap().values()) {
        if (filez.size() == 1)
            continue;
        log.info("dupes found: {}", filez);
    }
}

From source file:org.spoutcraft.launcher.util.Download.java

@SuppressWarnings("unused")
public void run() {
    ReadableByteChannel rbc = null;
    FileOutputStream fos = null;// ww  w. j  av a  2  s .  co  m
    try {
        URLConnection conn = url.openConnection();
        conn.setDoInput(true);
        conn.setDoOutput(false);
        System.setProperty("http.agent",
                "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.162 Safari/535.19");
        conn.setRequestProperty("User-Agent",
                "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.162 Safari/535.19");
        HttpURLConnection.setFollowRedirects(true);
        conn.setUseCaches(false);
        ((HttpURLConnection) conn).setInstanceFollowRedirects(true);
        int response = ((HttpURLConnection) conn).getResponseCode();
        InputStream in = getConnectionInputStream(conn);

        size = conn.getContentLength();
        outFile = new File(outPath);
        outFile.delete();

        rbc = Channels.newChannel(in);
        fos = new FileOutputStream(outFile);

        stateChanged();

        Thread progress = new MonitorThread(Thread.currentThread(), rbc);
        progress.start();

        fos.getChannel().transferFrom(rbc, 0, size > 0 ? size : Integer.MAX_VALUE);
        in.close();
        rbc.close();
        progress.interrupt();
        if (size > 0) {
            if (size == outFile.length()) {
                result = Result.SUCCESS;
            }
        } else {
            result = Result.SUCCESS;
        }
    } catch (PermissionDeniedException e) {
        exception = e;
        result = Result.PERMISSION_DENIED;
    } catch (DownloadException e) {
        exception = e;
        result = Result.FAILURE;
    } catch (Exception e) {
        exception = e;
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(fos);
        IOUtils.closeQuietly(rbc);
    }
}

From source file:org.commoncrawl.util.ArcFileReader.java

@Test
public void testReader(InputStream stream) throws IOException {

    setIOTimeoutValue(30000);/*w  ww .j a  va  2  s.c  o  m*/

    resetState();

    Thread thread = new Thread(new Runnable() {

        public void run() {
            try {

                while (hasMoreItems()) {
                    ArcFileItem item = new ArcFileItem();

                    getNextItem(item);

                    LOG.info("GOT Item URL:" + item.getUri() + " StreamPos:" + item.getArcFilePos()
                            + " Content Length:" + item.getContent().getCount());
                    for (ArcFileHeaderItem headerItem : item.getHeaderItems()) {
                        if (headerItem.isFieldDirty(ArcFileHeaderItem.Field_ITEMKEY)) {
                            // LOG.info("Header Item:" + headerItem.getItemKey() + " :" +
                            // headerItem.getItemValue());
                        } else {
                            // LOG.info("Header Item:" + headerItem.getItemValue());
                        }
                    }
                    // LOG.info("Content Length:" + item.getContent().getCount());
                    // LOG.info("Content:");
                    /*
                     * ByteArrayInputStream inputStream = new
                     * ByteArrayInputStream(item.getContent
                     * ().getReadOnlyBytes(),0,item.getContent().getCount());
                     * BufferedReader reader = new BufferedReader(new
                     * InputStreamReader(inputStream,Charset.forName("ASCII"))); String
                     * line = null; while ((line = reader.readLine()) != null) {
                     * LOG.info(line); }
                     */
                }
                LOG.info("NO MORE ITEMS... BYE");
            } catch (IOException e) {
                LOG.error(StringUtils.stringifyException(e));
            }
        }

    });

    // run the thread ...
    thread.start();

    ReadableByteChannel channel = Channels.newChannel(stream);

    try {

        int totalBytesRead = 0;
        for (;;) {

            ByteBuffer buffer = ByteBuffer.allocate(32768);

            int bytesRead = channel.read(buffer);
            // LOG.info("Read "+bytesRead + " From File");

            if (bytesRead == -1) {
                finished();
                break;
            } else {
                buffer.flip();
                totalBytesRead += buffer.remaining();
                available(buffer);
            }
        }
    } finally {
        channel.close();
    }

    // now wait for thread to die ...
    LOG.info("Done Reading File.... Waiting for ArcFileThread to DIE");
    try {
        thread.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    LOG.info("Done Reading File.... ArcFileThread to DIED");
}

From source file:org.commoncrawl.hadoop.io.deprecated.ArcFileReader.java

@Test
public void testReader(File file) throws Exception {

    checkCRLFStateMachine();//from ww  w .  j a  v a  2  s.  com

    setIOTimeoutValue(30000);

    resetState();

    Thread thread = new Thread(new Runnable() {

        public void run() {
            try {

                while (hasMoreItems()) {
                    ArcFileItem item = new ArcFileItem();

                    getNextItem(item);

                    LOG.info("GOT Item URL:" + item.getUri() + " StreamPos:" + item.getArcFilePos()
                            + " Content Length:" + item.getContent().getCount());
                    for (ArcFileHeaderItem headerItem : item.getHeaderItems()) {
                        if (headerItem.isFieldDirty(ArcFileHeaderItem.Field_ITEMKEY)) {
                            // LOG.info("Header Item:" + headerItem.getItemKey() + " :" +
                            // headerItem.getItemValue());
                        } else {
                            // LOG.info("Header Item:" + headerItem.getItemValue());
                        }
                    }
                    // LOG.info("Content Length:" + item.getContent().getCount());
                    // LOG.info("Content:");
                    /*
                     * ByteArrayInputStream inputStream = new
                     * ByteArrayInputStream(item.getContent
                     * ().getReadOnlyBytes(),0,item.getContent().getCount());
                     * BufferedReader reader = new BufferedReader(new
                     * InputStreamReader(inputStream,Charset.forName("ASCII"))); String
                     * line = null; while ((line = reader.readLine()) != null) {
                     * LOG.info(line); }
                     */
                }
                LOG.info("NO MORE ITEMS... BYE");
            } catch (IOException e) {
                LOG.error(StringUtils.stringifyException(e));
            }
        }

    });

    // run the thread ...
    thread.start();

    ReadableByteChannel channel = Channels.newChannel(new FileInputStream(file));

    try {

        int totalBytesRead = 0;
        for (;;) {

            ByteBuffer buffer = ByteBuffer.allocate(32768);

            int bytesRead = channel.read(buffer);
            // LOG.info("Read "+bytesRead + " From File");

            if (bytesRead == -1) {
                finished();
                break;
            } else {
                buffer.flip();
                totalBytesRead += buffer.remaining();
                available(buffer);
            }
        }
    } finally {
        channel.close();
    }

    // now wait for thread to die ...
    LOG.info("Done Reading File.... Waiting for ArcFileThread to DIE");
    thread.join();
    LOG.info("Done Reading File.... ArcFileThread to DIED");
}

From source file:org.commoncrawl.util.StreamingArcFileReader.java

public void testReader(File arcFileItem) throws Exception {

    resetState();//from   w  w w  .j ava2s . c  om

    Thread thread = new Thread(new Runnable() {

        public void run() {
            try {

                TriStateResult result;

                while ((result = hasMoreItems()) != TriStateResult.NoMoreItems) {

                    if (result == TriStateResult.MoreItems) {

                        ArcFileItem item = null;

                        while ((item = getNextItem()) == null) {
                            LOG.info("Waiting to Read Next Item...");
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                            }
                        }

                        LOG.info("GOT Item URL:" + item.getUri() + " OFFSET:" + item.getArcFilePos()
                                + " ContentSize:" + item.getContent().getCount());
                        for (ArcFileHeaderItem headerItem : item.getHeaderItems()) {
                            if (headerItem.isFieldDirty(ArcFileHeaderItem.Field_ITEMKEY)) {
                                //LOG.info("Header Item:" + headerItem.getItemKey() + " :" + headerItem.getItemValue());
                            } else {
                                //LOG.info("Header Item:" + headerItem.getItemValue());
                            }
                        }
                        //LOG.info("Content Length:" + item.getContent().getCount());
                    } else {
                        // LOG.info("Has More Items Returned Need More Data. Sleeping");
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                        }
                    }
                }
                LOG.info("NO MORE ITEMS... BYE");
            } catch (IOException e) {
                LOG.error(StringUtils.stringifyException(e));
            }
        }

    });

    // run the thread ... 
    thread.start();

    ReadableByteChannel channel = Channels.newChannel(new FileInputStream(arcFileItem));

    try {

        for (;;) {

            ByteBuffer buffer = ByteBuffer.allocate(BLOCK_SIZE);

            int bytesRead = channel.read(buffer);
            LOG.info("Read " + bytesRead + " From File");

            if (bytesRead == -1) {
                finished();
                break;
            } else {
                buffer.flip();
                available(buffer);
            }
        }
    } finally {
        channel.close();
    }

    // now wait for thread to die ...
    LOG.info("Done Reading File.... Waiting for ArcFileThread to DIE");
    thread.join();
    LOG.info("Done Reading File.... ArcFileThread to DIED");
}

From source file:net.technicpack.launchercore.mirror.download.Download.java

@Override
@SuppressWarnings("unused")
public void run() {
    ReadableByteChannel rbc = null;
    FileOutputStream fos = null;//  www  .  j  a  va 2s  .  co m
    try {
        HttpURLConnection conn = Utils.openHttpConnection(url);
        int response = conn.getResponseCode();
        int responseFamily = response / 100;

        if (responseFamily == 3) {
            String redirUrlText = conn.getHeaderField("Location");
            if (redirUrlText != null && !redirUrlText.isEmpty()) {
                URL redirectUrl = null;
                try {
                    redirectUrl = new URL(redirUrlText);
                } catch (MalformedURLException ex) {
                    throw new DownloadException("Invalid Redirect URL: " + url, ex);
                }

                conn = Utils.openHttpConnection(redirectUrl);
                response = conn.getResponseCode();
                responseFamily = response / 100;
            }
        }

        if (responseFamily != 2) {
            throw new DownloadException("The server issued a " + response + " response code.");
        }

        InputStream in = getConnectionInputStream(conn);

        size = conn.getContentLength();
        outFile = new File(outPath);
        outFile.delete();

        rbc = Channels.newChannel(in);
        fos = new FileOutputStream(outFile);

        stateChanged();

        Thread progress = new MonitorThread(Thread.currentThread(), rbc);
        progress.start();

        fos.getChannel().transferFrom(rbc, 0, size > 0 ? size : Integer.MAX_VALUE);
        in.close();
        rbc.close();
        progress.interrupt();

        synchronized (timeoutLock) {
            if (isTimedOut) {
                return;
            }
        }

        if (size > 0) {
            if (size == outFile.length()) {
                result = Result.SUCCESS;
            }
        } else {
            result = Result.SUCCESS;
        }
    } catch (ClosedByInterruptException ex) {
        result = Result.FAILURE;
        return;
    } catch (PermissionDeniedException e) {
        exception = e;
        result = Result.PERMISSION_DENIED;
    } catch (DownloadException e) {
        exception = e;
        result = Result.FAILURE;
    } catch (Exception e) {
        exception = e;
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(fos);
        IOUtils.closeQuietly(rbc);
    }
}

From source file:org.codelabor.system.file.util.UploadUtils.java

/**
 * ?? .</br> ?  ?? FILE_SYSTEM? , ??  DATABASE?  byte[]
 *  DTO? ./*ww w. j a va 2s. c  o m*/
 * 
 * @param repositoryType
 *            ?  ?
 * @param inputStream
 *             
 * @param fileDTO
 *            ? DTO
 * @throws Exception
 *             
 */
static public void processFile(RepositoryType repositoryType, InputStream inputStream, FileDTO fileDTO)
        throws Exception {
    // prepare io
    OutputStream outputStream = null;
    ReadableByteChannel inputChannel = null;
    WritableByteChannel outputChannel = null;

    int fileSize = 0;

    switch (repositoryType) {
    case FILE_SYSTEM:
        // prepare repository
        File repository = new File(fileDTO.getRepositoryPath());
        String repositoryPath = fileDTO.getRepositoryPath();
        StringBuilder sb = new StringBuilder();
        if (logger.isDebugEnabled()) {
            sb.append("repositoryPath: ").append(repositoryPath);
            sb.append(", repositoryType: ").append(repositoryType);
            sb.append(", repository.exists(): ").append(repository.exists());
            sb.append(", repository.isDirectory(): ").append(repository.isDirectory());
            logger.debug(sb.toString());
        }

        // prepare directory
        File file = new File(repositoryPath);
        if (!file.exists()) {
            try {
                FileUtils.forceMkdir(file);
            } catch (IOException e) {
                StringBuilder sb = new StringBuilder();
                sb.append("Cannot make directory: ");
                sb.append(file.toString());
                logger.error(sb.toString());
                throw new ServletException(sb.toString());
            }
        }

        // prepare stream
        sb.setLength(0);
        sb.append(fileDTO.getRepositoryPath());
        if (!fileDTO.getRepositoryPath().endsWith(File.separator)) {
            sb.append(File.separator);
        }
        sb.append(fileDTO.getUniqueFilename());
        String filename = sb.toString();
        outputStream = new FileOutputStream(filename);
        logger.debug("filename: {}", filename);

        // copy channel
        inputChannel = Channels.newChannel(inputStream);
        outputChannel = Channels.newChannel(outputStream);
        fileSize = ChannelUtils.copy(inputChannel, outputChannel);

        // set vo
        if (StringUtils.isEmpty(fileDTO.getContentType())) {
            fileDTO.setContentType(TikaMimeDetectUtils.getMimeType(filename));
        }
        break;
    case DATABASE:
        // prepare steam
        outputStream = new ByteArrayOutputStream();

        // copy channel
        inputChannel = Channels.newChannel(inputStream);
        outputChannel = Channels.newChannel(outputStream);
        fileSize = ChannelUtils.copy(inputChannel, outputChannel);

        // set vo
        byte[] bytes = ((ByteArrayOutputStream) outputStream).toByteArray();
        fileDTO.setBytes(bytes);
        fileDTO.setRepositoryPath(null);
        if (StringUtils.isEmpty(fileDTO.getContentType())) {
            fileDTO.setContentType(TikaMimeDetectUtils.getMimeType(bytes));
        }
        break;
    }
    fileDTO.setFileSize(fileSize);

    // close io
    inputChannel.close();
    outputChannel.close();
    inputStream.close();
    outputStream.close();
}