Example usage for java.nio.channels Channels newInputStream

List of usage examples for java.nio.channels Channels newInputStream

Introduction

In this page you can find the example usage for java.nio.channels Channels newInputStream.

Prototype

public static InputStream newInputStream(AsynchronousByteChannel ch) 

Source Link

Document

Constructs a stream that reads bytes from the given channel.

Usage

From source file:com.rogiel.httpchannel.service.impl.MegaUploadServiceTest.java

@Test
public void testPremiumDownloader() throws IOException {
    service.getAuthenticator(new Credential(VALID_USERNAME, VALID_PASSWORD)).login();

    final DownloadChannel channel = service.getDownloader(URI.create("http://www.megaupload.com/?d=CVQKJ1KM"))
            .openChannel(new DownloadListener() {
                @Override/* ww w . j a  v a  2  s.c  om*/
                public boolean timer(long time) {
                    System.out.println("Waiting " + time);
                    return true;
                }
            }, 0);
    final ByteArrayOutputStream bout = new ByteArrayOutputStream();
    IOUtils.copy(Channels.newInputStream(channel), bout);
    System.out.println(bout.size());
}

From source file:fr.gael.dhus.util.http.DownloadableProduct.java

@Override
public <T> T getImpl(Class<? extends T> cl) {
    if (InputStream.class.isAssignableFrom(cl)) {
        try {/*  w w  w  . j a  v a  2s.c om*/
            Pipe pipe = Pipe.open();
            DownloadTask dltask = new DownloadTask(pipe);
            downloadThread = new Thread(dltask, "Product Download");
            downloadThread.start();

            InputStream is = Channels.newInputStream(pipe.source());
            return cl.cast(is);
        } catch (IOException ex) {
            LOGGER.error("could not create pipe", ex);
        }
    }
    return null;
}

From source file:com.bennavetta.appsite.processing.ProcessingController.java

private void runProcessor(Processor processor, Resource resource, OutputStream out, Request request,
        Response response) throws Exception {
    try (InputStream in = Channels.newInputStream(resources.read(resource))) {
        processor.process(resource.getPath(), resource.getMimeType(), in, out, request, response);
    }//from   w  ww  .  j  av a  2  s .co m
}

From source file:com.aliyun.odps.volume.VolumeFSInputStream.java

private synchronized InputStream getLocalInputStream(long index) throws FileNotFoundException, IOException {
    if (!seekOptimization) {
        return null;
    }// w  w  w. j av  a  2  s  .co  m
    isCache = true;
    File blockFile = new File(buffer_block_dir, getBlockFileName(index));
    raf = new RandomAccessFile(blockFile, "r");
    return Channels.newInputStream(raf.getChannel().position(pos - curIndex * blockSize));
}

From source file:org.pentaho.googlecloudstorage.vfs.GoogleCloudStorageFileObject.java

/**
 * Gets the input stream of the file/*from   w w  w  .  j  av  a2  s . c  om*/
 *
 * @return The InputStream of the file
 * @throws Exception
 */
@Override
protected InputStream doGetInputStream() throws Exception {
    return Channels.newInputStream(getBlob().reader());
}

From source file:com.github.jinahya.simple.file.back.LocalFileBackTest.java

@Test(enabled = true, invocationCount = 1)
public void read() throws IOException, FileBackException {

    fileContext.fileOperationSupplier(() -> FileOperation.READ);

    final ByteBuffer fileKey = randomFileKey();
    fileContext.sourceKeySupplier(() -> fileKey);

    final Path leafPath = LocalFileBack.leafPath(rootPath, fileKey, true);

    final byte[] fileBytes = randomFileBytes();
    final boolean fileWritten = Files.isRegularFile(leafPath) || current().nextBoolean();
    if (fileWritten) {
        Files.write(leafPath, fileBytes, StandardOpenOption.CREATE, StandardOpenOption.WRITE);
        logger.trace("file written");
    }/*from   ww  w.j  a va2 s  .co  m*/

    fileContext.sourceChannelConsumer(v -> {
        final byte[] actual = new byte[fileBytes.length];
        try {
            IOUtils.readFully(Channels.newInputStream(v), actual);
            if (fileWritten) {
                assertEquals(actual, fileBytes);
            }
        } catch (final IOException ioe) {
            fail("failed to read from source channel", ioe);
        }
    });

    final ByteArrayOutputStream targetStream = new ByteArrayOutputStream();
    fileContext.targetChannelSupplier(() -> {
        return Channels.newChannel(targetStream);
    });

    fileBack.operate(fileContext);

    if (fileWritten) {
        assertEquals(targetStream.toByteArray(), fileBytes);
    }
}

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();/*from ww w.j  av a  2  s  . com*/
            rbc.close();
        } else {
            objectInputStream.close();
        }
        contentCached = true;
    }

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

From source file:com.clustercontrol.agent.log.LogfileMonitor.java

/**
 * ?<BR>/*from   w  ww  . ja  v a  2 s.  c o  m*/
 * 
 */
public void run() {
    m_log.debug("monitor start.  logfile : " + getFilePath() + "  file encoding : "
            + System.getProperty("file.encoding"));

    if (!status.isInitialized())
        return;

    // 
    if (fileChannel == null) {
        // ??????
        if (!openFile()) {
            return;
        }
    }

    boolean readSuccessFlg = true; // ???
    long currentFilesize = 0;
    try {
        currentFilesize = fileChannel.size(); // ??????

        if (status.prevSize == currentFilesize) {
            /** ? */
            boolean logrotateFlag = false; // 

            int runInterval = LogfileMonitorManager.getRunInterval();
            // ?m_unchangedStatsPeriod????????
            if ((++m_unchanged_stats * runInterval) < LogfileMonitorConfig.unchangedStatsPeriod) {
                return;
            }
            m_log.debug("run() : " + getFilePath() + " check log rotation");

            // ??????
            // ??
            // ???????????????????
            // mv??????
            File file = new File(getFilePath());
            if (fileChannel.size() != file.length()) { // 
                m_log.debug("run() : " + getFilePath() + " file size not match");
                logrotateFlag = true;

                m_log.debug("run() : m_logrotate set true .1");// rm?????????rotate??????
                m_log.debug("run() : m_fr.length()=" + fileChannel.size());
                m_log.debug("run() : file.length()=" + file.length());
            } else if (currentFilesize > 0 && LogfileMonitorConfig.firstPartDataCheckPeriod > 0
                    && (HinemosTime.currentTimeMillis()
                            - m_lastDataCheck) > LogfileMonitorConfig.firstPartDataCheckPeriod) {
                m_log.debug("run() : " + getFilePath() + " check first part of file");

                // ??
                // ????????????????????
                // mv???????
                // ?????????????
                logrotateFlag = checkPrefix();

                // ?
                m_lastDataCheck = HinemosTime.currentTimeMillis();
            }

            // ??????
            if (logrotateFlag) {
                // ????
                // ??????????????
                //  ??????????????filesize????????
                //  ????????????????
                // ??
                if (currentFilesize == fileChannel.size()) {
                    m_log.info(getFilePath() + " : file changed");
                    closeFile();

                    status.rotate();

                    // 
                    if (!openFile()) {
                        return;
                    }

                    currentFilesize = fileChannel.size();
                }

            }
            m_unchanged_stats = 0;
        }

        m_unchanged_stats = 0;

        if (status.prevSize < currentFilesize) {
            // ?
            m_log.debug("run() : " + getFilePath() + " filesize " + status.prevSize + " tmp_filesize "
                    + currentFilesize);

            char[] cbuf = new char[1024];
            try (Reader fr = new InputStreamReader(Channels.newInputStream(fileChannel),
                    m_wrapper.monitorInfo.getLogfileCheckInfo().getFileEncoding()) {
                @Override
                public void close() {
                    // ????????
                }
            }) {
                LineSeparator separator = new LineSeparator(m_wrapper.monitorInfo.getLogfileCheckInfo());

                int maxBytes = m_wrapper.monitorInfo.getLogfileCheckInfo().getMaxBytes() != null
                        ? m_wrapper.monitorInfo.getLogfileCheckInfo().getMaxBytes()
                        : LogfileMonitorConfig.logfilMessageLength;
                int maxLines = LogfileMonitorConfig.logfilMessageLine;

                long start = System.currentTimeMillis();
                boolean logFlag = true;
                while (true) {
                    readSuccessFlg = false;

                    int read = fr.read(cbuf);
                    readSuccessFlg = true;
                    if (read == -1)
                        break;

                    // ?????
                    // ??????????
                    // ?????
                    String appendedBuf = new StringBuilder(status.carryover.length() + read)
                            .append(status.carryover).append(cbuf, 0, read).toString();
                    List<String> lines = new LinkedList<String>();
                    while (lines.size() <= maxLines) {
                        int pos = separator.search(appendedBuf);
                        if (pos != -1) {
                            lines.add(appendedBuf.substring(0, pos));
                            appendedBuf = appendedBuf.substring(pos, appendedBuf.length());
                        } else {
                            break;
                        }
                    }

                    if (!appendedBuf.isEmpty()) {
                        status.carryover = appendedBuf;

                        // ????????
                        if (status.carryover.length() > maxBytes) {
                            String message = "run() : " + getFilePath() + " carryOverBuf size = "
                                    + status.carryover.length()
                                    + ". carryOverBuf is too long. it cut down .(see monitor.logfile.message.length)";
                            if (logFlag) {
                                m_log.info(message);
                                logFlag = false;
                            } else {
                                m_log.debug(message);
                            }

                            status.carryover = status.carryover.substring(0, maxBytes);
                        }

                        // ?
                        if (m_log.isDebugEnabled()) {
                            m_log.debug("run() : " + getFilePath() + " carryOverBuf size "
                                    + status.carryover.length());
                        }
                    } else {
                        status.carryover = "";
                        logFlag = true;
                    }

                    for (String line : lines) {
                        // ????????syslog??????
                        if (m_syslog.isValid()) {
                            // v3.2 mode
                            String logPrefix = LogfileMonitorConfig.program + "(" + getFilePath() + "):";
                            m_syslog.log(logPrefix + line);
                        } else {
                            // v4.0 mode
                            MonitorStringUtil.patternMatch(
                                    LogfileMonitorConfig.formatLine(line, m_wrapper.monitorInfo),
                                    m_wrapper.monitorInfo, m_wrapper.runInstructionInfo, getFilePath());
                        }
                    }
                    //Copytruncate???????Position?
                    //?????Position??????????????
                    //?????????????????
                    //???????????????
                    currentFilesize = fileChannel.size();
                    if (status.prevSize <= currentFilesize) {
                        // ??
                        status.position = fileChannel.position();
                        status.prevSize = currentFilesize;
                        if (status.prefix == null || status.prefix.isEmpty()) {
                            status.prefix = new String(cbuf, 0,
                                    Math.min(read, LogfileMonitorConfig.firstPartDataCheckSize));
                        } else if (status.prefix.length() < LogfileMonitorConfig.firstPartDataCheckSize) {
                            status.prefix = status.prefix + new String(cbuf, 0, Math.min(read,
                                    LogfileMonitorConfig.firstPartDataCheckSize - status.prefix.length()));
                        }
                        status.store();
                    } else {
                        fileChannel.position(0);
                        status.rotate();
                    }
                }

                m_log.info(String.format("run() : elapsed=%d ms.", System.currentTimeMillis() - start));
            }

            if (m_log.isDebugEnabled()) {
                m_log.debug("run() : " + getFilePath() + " filesize = " + status.prevSize + ", FilePointer = "
                        + fileChannel.position());
            }
        } else if (status.prevSize > currentFilesize) {
            // ????
            m_log.info("run() : " + getFilePath() + " : file size becomes small");
            fileChannel.position(0);
            status.rotate();
        }
    } catch (IOException e) {
        if (readSuccessFlg) {
            m_log.error("run() : " + e.getMessage());
            String[] args = { getFilePath() };
            // message.log.agent.1={0}?
            // message.log.agent.4=????????
            sendMessage(PriorityConstant.TYPE_WARNING, MessageConstant.AGENT.getMessage(),
                    MessageConstant.MESSAGE_LOG_FAILED_TO_READ_FILE.getMessage(),
                    MessageConstant.MESSAGE_LOG_FILE.getMessage(args) + "\n" + e.getMessage());

            // ?????
            closeFile();
        } else {
            m_log.warn("run() : " + e.getMessage());
            // ?????????????
            try {
                fileChannel.position(status.prevSize);
            } catch (IOException e1) {
                m_log.error("run() set file-pointer error : " + e1.getMessage());
            }
        }
    }
}

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

protected OutputStream doGetOutputStream(boolean bAppend) throws Exception {
    return new MonitorOutputStream(Channels.newOutputStream(getCacheFile().getChannel())) {
        protected void onClose() throws IOException {
            try {
                LOG.debug(String.format("sending '%s' to storage (cached=%b)", object.getKey(), cacheFile));
                if (cacheFile != null) {
                    FileChannel cacheFc = getCacheFile().getChannel();
                    object.setContentLength(cacheFc.size());
                    object.setDataInputStream(Channels.newInputStream(cacheFc));
                }/*from   w  w w .  j a va 2  s.  com*/
                service.putObject(bucket, object);
            } catch (S3ServiceException e) {
                LOG.error(String.format("can't send object '%s' to storage", object), e);
            }
        }
    };
}