Example usage for java.io SequenceInputStream SequenceInputStream

List of usage examples for java.io SequenceInputStream SequenceInputStream

Introduction

In this page you can find the example usage for java.io SequenceInputStream SequenceInputStream.

Prototype

public SequenceInputStream(Enumeration<? extends InputStream> e) 

Source Link

Document

Initializes a newly created SequenceInputStream by remembering the argument, which must be an Enumeration that produces objects whose run-time type is InputStream.

Usage

From source file:cn.dreampie.LessCompiler.java

/**
 * Initializes this <code>LessCompiler</code>.
 * <p>// ww w.j a  v a2  s  .  c om
 * It is not needed to call this method manually, as it is called implicitly by the compile methods if needed.
 * </p>
 */
public synchronized void init() {
    long start = System.currentTimeMillis();

    try {
        Context cx = Context.enter();
        //cx.setOptimizationLevel(-1);
        cx.setLanguageVersion(Context.VERSION_1_7);

        Global global = new Global();
        global.init(cx);
        scope = cx.initStandardObjects(global);
        scope.put("logger", scope, Context.toObject(logger, scope));

        out = new ByteArrayOutputStream();
        global.setOut(new PrintStream(out));

        // Combine all of the streams (less, custom, lessc) into one big stream
        List<InputStream> streams = new ArrayList<InputStream>();

        // less should be first
        streams.add(lessJs.openConnection().getInputStream());

        // then the custom js so it has a chance to add any hooks
        for (URL url : customJs) {
            streams.add(url.openConnection().getInputStream());
        }

        // then the lessc so we can do the compile
        streams.add(lesscJs.openConnection().getInputStream());

        InputStreamReader reader = new InputStreamReader(
                new SequenceInputStream(Collections.enumeration(streams)));

        // Load the streams into a function we can run
        compiler = (Function) cx.compileReader(reader, lessJs.toString(), 1, null);

    } catch (Exception e) {
        String message = "Failed to initialize LESS compiler.";
        logger.error(message, e);
        throw new IllegalStateException(message, e);
    } finally {
        Context.exit();
    }

    if (logger.isDebugEnabled()) {
        logger.debug("Finished initialization of LESS compiler in %,d ms.%n",
                System.currentTimeMillis() - start);
    }
}

From source file:org.treeingwalker.LessCompiler.java

/**
 * Initializes this <code>LessCompiler</code>.
 * <p>/* ww w.j av  a  2s  . c  o m*/
 * It is not needed to call this method manually, as it is called implicitly by the compile methods if needed.
 * </p>
 */
public synchronized void init() {
    long start = System.currentTimeMillis();

    try {
        Context cx = Context.enter();
        //cx.setOptimizationLevel(-1);
        cx.setLanguageVersion(Context.VERSION_1_7);

        Global global = new Global();
        global.init(cx);
        scope = cx.initStandardObjects(global);
        scope.put("logger", scope, Context.toObject(logger, scope));

        out = new ByteArrayOutputStream();
        global.setOut(new PrintStream(out));

        // Combine all of the streams (less, custom, lessc) into one big stream
        List<InputStream> streams = new ArrayList<>();

        // less should be first
        streams.add(lessJs.openConnection().getInputStream());

        // then the custom js so it has a chance to add any hooks
        for (URL url : customJs) {
            streams.add(url.openConnection().getInputStream());
        }

        // then the lessc so we can do the compile
        streams.add(lesscJs.openConnection().getInputStream());

        InputStreamReader reader = new InputStreamReader(
                new SequenceInputStream(Collections.enumeration(streams)));

        // Load the streams into a function we can run 
        compiler = (Function) cx.compileReader(reader, lessJs.toString(), 1, null);

    } catch (Exception e) {
        String message = "Failed to initialize LESS compiler.";
        logger.error(message, e);
        throw new IllegalStateException(message, e);
    } finally {
        Context.exit();
    }

    if (logger.isDebugEnabled()) {
        logger.debug("Finished initialization of LESS compiler in %,d ms.%n",
                System.currentTimeMillis() - start);
    }
}

From source file:com.nmote.io.ByteArrayOutputStream.java

/**
 * Gets the current contents of this byte stream as a Input Stream. The
 * returned stream is backed by buffers of <code>this</code> stream,
 * avoiding memory allocation and copy, thus saving space and time.<br>
 * /*from   w  ww  . j  a v  a 2 s . c  om*/
 * @return the current contents of this output stream.
 * @see java.io.ByteArrayOutputStream#toByteArray()
 * @see #reset()
 * @since Commons IO 2.0
 */
public InputStream toBufferedInputStream() {
    int remaining = count;
    if (remaining == 0) {
        return new ClosedInputStream();
    }
    List<ByteArrayInputStream> list = new ArrayList<ByteArrayInputStream>(buffers.size());
    for (byte[] buf : buffers) {
        int c = Math.min(buf.length, remaining);
        list.add(new ByteArrayInputStream(buf, 0, c));
        remaining -= c;
        if (remaining == 0) {
            break;
        }
    }
    return new SequenceInputStream(Collections.enumeration(list));
}

From source file:com.norconex.commons.lang.io.ByteArrayOutputStream.java

/**
 * Gets the current contents of this byte stream as a Input Stream. The
 * returned stream is backed by buffers of <code>this</code> stream,
 * avoiding memory allocation and copy, thus saving space and time.<br>
 * /*from  www . ja  v a  2s  .c o m*/
 * @return the current contents of this output stream.
 * @see java.io.ByteArrayOutputStream#toByteArray()
 * @see #reset()
 */
private InputStream toBufferedInputStream() {
    int remaining = totalCount;
    if (remaining == 0) {
        return new ClosedInputStream();
    }
    List<ByteArrayInputStream> list = new ArrayList<ByteArrayInputStream>(buffers.size());
    for (byte[] buf : buffers) {
        int c = Math.min(buf.length, remaining);
        list.add(new ByteArrayInputStream(buf, 0, c));
        remaining -= c;
        if (remaining == 0) {
            break;
        }
    }
    return new SequenceInputStream(Collections.enumeration(list));
}

From source file:org.codehaus.plexus.archiver.zip.ByteArrayOutputStream.java

/**
 * Gets the current contents of this byte stream as a Input Stream. The
 * returned stream is backed by buffers of <code>this</code> stream,
 * avoiding memory allocation and copy, thus saving space and time.<br>
 *
 * @return the current contents of this output stream.
 * @see java.io.ByteArrayOutputStream#toByteArray()
 * @see #reset()/*  w w  w .jav  a 2 s .  c  o m*/
 * @since 2.5
 */
public synchronized InputStream toInputStream() {
    int remaining = count;
    if (remaining == 0) {
        return new ClosedInputStream();
    }
    final List<ByteArrayInputStream> list = new ArrayList<ByteArrayInputStream>(buffers.size());
    for (final byte[] buf : buffers) {
        final int c = Math.min(buf.length, remaining);
        list.add(new ByteArrayInputStream(buf, 0, c));
        remaining -= c;
        if (remaining == 0) {
            break;
        }
    }
    reuseBuffers = false;
    return new SequenceInputStream(Collections.enumeration(list));
}

From source file:eu.crowdrec.contest.sender.RequestSender.java

/**
 * read logFile then sends line by line to server.
 * /*from  ww w  .j  a v a2 s  . c  o m*/
 * @param inLogFileName
 *            path to log file. That can be a zip file or text file.
 * @param outLogFile
 *            path to outLog file. The outLog file should be analyzed by the evaluator.
 *            if the filename is null; no output will be generated
 * @param serverURL
 *            URL of the server
 */
public static void sender(final String inLogFileName, final String outLogFile, final String serverURL) {

    // handle the log file
    // check type of file

    // try to read the defined logFile
    BufferedReader br = null;
    BufferedWriter bw = null;
    try {
        // if outLogFile name is not null, create an output file
        if (outLogFile != null && outLogFile.length() > 0) {
            bw = new BufferedWriter(new FileWriter(new File(outLogFile), false));
        }

        // support a list of files in a directory
        File inLogFile = new File(inLogFileName);
        InputStream is;
        if (inLogFile.isFile()) {
            is = new FileInputStream(inLogFileName);
            // support gZip files
            if (inLogFile.getName().toLowerCase().endsWith(".gz")) {
                is = new GZIPInputStream(is);
            }
        } else {
            // if the input is a directory, consider all files based on a pattern
            File[] childs = inLogFile.listFiles(new FilenameFilter() {

                @Override
                public boolean accept(File dir, String name) {
                    final String fileName = name.toLowerCase();
                    return fileName.endsWith("data.idomaar.txt.gz") || fileName.endsWith("data.idomaar.txt")
                            || fileName.endsWith(".data.gz");
                }
            });
            if (childs == null || childs.length == 0) {
                throw new IOException("invalid inLogFileName or empty directory");
            }
            Arrays.sort(childs, new Comparator<File>() {

                @Override
                public int compare(File o1, File o2) {
                    return o1.getName().compareTo(o2.getName());
                }
            });
            Vector<InputStream> isChilds = new Vector<InputStream>();
            for (int i = 0; i < childs.length; i++) {
                InputStream tmpIS = new FileInputStream(childs[i]);
                // support gZip files
                if (childs[i].getName().toLowerCase().endsWith(".gz")) {
                    tmpIS = new GZIPInputStream(tmpIS);
                }
                isChilds.add(tmpIS);
            }
            is = new SequenceInputStream(isChilds.elements());
        }

        // read the log file line by line
        br = new BufferedReader(new InputStreamReader(is));
        try {
            for (String line = br.readLine(); line != null; line = br.readLine()) {

                // ignore invalid lines and header
                if (line.startsWith("null") || line.startsWith("#")) {
                    continue;
                }

                if (useThreadPool) {
                    RequestSenderThread t = new RequestSenderThread(line, serverURL, bw);
                    try {
                        // try to limit the speed of sending requests
                        if (Thread.activeCount() > 1000) {
                            if (logger.isDebugEnabled()) {
                                logger.debug("Thread.activeCount() = " + Thread.activeCount());
                            }
                            Thread.sleep(200);
                        }
                    } catch (Exception e) {
                        logger.info(e.toString());
                    }
                    t.start();
                } else {
                    // send parameters to http server and get response.
                    final long startTime = System.currentTimeMillis();
                    String result = HttpLib.HTTPCLIENT.equals(httpLib)
                            ? excutePostWithHttpClient(line, serverURL)
                            : excutePost(line, serverURL);

                    // analyze whether an answer is expected
                    boolean answerExpected = false;
                    if (line.contains("\"event_type\": \"recommendation_request\"")) {
                        answerExpected = true;
                    }
                    if (answerExpected) {
                        if (logger.isInfoEnabled()) {
                            logger.info("serverResponse: " + result);
                        }

                        // if the output file is not null, write the output in a synchronized way
                        if (bw != null) {
                            String[] data = LogFileUtils.extractEvaluationRelevantDataFromInputLine(line);
                            String requestId = data[0];
                            String userId = data[1];
                            String itemId = data[2];
                            String domainId = data[3];
                            String timeStamp = data[4];
                            long responseTime = System.currentTimeMillis() - startTime;
                            synchronized (bw) {
                                try {
                                    bw.write("prediction\t" + requestId + "\t" + timeStamp + "\t" + responseTime
                                            + "\t" + itemId + "\t" + userId + "\t" + domainId + "\t" + result);
                                    bw.newLine();
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    }
                }
            }
        } catch (IOException e) {
            logger.warn(e.toString(), e);
        }
    } catch (FileNotFoundException e) {
        logger.error("logFile not found e:" + e.toString());
    } catch (IOException e) {
        logger.error("reading the logFile failed e:" + e.toString());
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                logger.debug("close read-log file failed");
            }
        }
        if (bw != null) {
            try {
                // wait for ensuring that all request are finished
                // this simplifies the management of thread and worked fine for all test machines
                Thread.sleep(5000);
                bw.flush();
            } catch (Exception e) {
                logger.debug("close write-log file failed");
            }
        }
    }
}

From source file:org.exist.util.io.FastByteArrayOutputStream.java

/**
 * Gets the current contents of this byte stream as a Input Stream. The
 * returned stream is backed by buffers of <code>this</code> stream,
 * avoiding memory allocation and copy, thus saving space and time.<br>
 *
 * @return the current contents of this output stream.
 * @see java.io.ByteArrayOutputStream#toByteArray()
 * @see #reset()/*from  ww  w .  j av  a  2 s  .  c  o  m*/
 * @since 2.5
 */
public /*synchronized*/ InputStream toInputStream() {
    int remaining = count;
    if (remaining == 0) {
        return new ClosedInputStream();
    }
    final List<ByteArrayInputStream> list = new ArrayList<>(buffers.size());
    for (final byte[] buf : buffers) {
        final int c = Math.min(buf.length, remaining);
        list.add(new ByteArrayInputStream(buf, 0, c));
        remaining -= c;
        if (remaining == 0) {
            break;
        }
    }
    reuseBuffers = false;
    return new SequenceInputStream(Collections.enumeration(list));
}

From source file:org.exist.util.io.FastByteArrayOutputStream.java

/**
 * Similar to {@link #toInputStream()}//from www . j ava  2s.  co m
 * but utilises {@link FastByteArrayInputStream}
 * as opposed to {@link java.io.ByteArrayInputStream}.
 */
public /*synchronized*/ InputStream toFastByteInputStream() {
    int remaining = count;
    if (remaining == 0) {
        return new ClosedInputStream();
    }
    final List<FastByteArrayInputStream> list = new ArrayList<>(buffers.size());
    for (final byte[] buf : buffers) {
        final int c = Math.min(buf.length, remaining);
        list.add(new FastByteArrayInputStream(buf, 0, c));
        remaining -= c;
        if (remaining == 0) {
            break;
        }
    }
    reuseBuffers = false;
    return new SequenceInputStream(Collections.enumeration(list));
}

From source file:com.ettrema.zsync.Upload.java

/**
 * Returns an InputStream containing a complete ZSync upload (Params, Relocate stream, and ByteRange stream), 
 * ready to be sent as the body of a PUT request. <p/>
 * //from   ww  w  .  j  a v  a 2s. co m
 * Note: In this implementation, any temporary file used to store the RelocateRanges will be automatically deleted when this stream
 * is closed, so a second invocation of this method on the same Upload object is likely to throw an exception.
 * Therefore, this method should be used only once per Upload object.
 * 
 * @return The complete ZSync upload
 * @throws UnsupportedEncodingException
 * @throws IOException
 */
public InputStream getInputStream() throws UnsupportedEncodingException, IOException {

    List<InputStream> streamList = new ArrayList<InputStream>();

    /*
     * The getParams and getRelocStream must be terminated by a single LF character.
     */
    streamList.add(IOUtils.toInputStream(getParams(), CHARSET));
    streamList.add(IOUtils.toInputStream(RELOCATE + ": ", CHARSET));
    streamList.add(getRelocStream());
    /* Prepend the data portion with a blank line. */
    streamList.add(IOUtils.toInputStream(Character.toString(LF), CHARSET));
    streamList.add(getDataStream());

    return new SequenceInputStream(new IteratorEnum<InputStream>(streamList));
}

From source file:com.adobe.aem.demomachine.gui.AemDemoUtils.java

public static String calcMD5HashForDir(File dirToHash, boolean includeSubFolders, boolean includeHiddenFiles) {

    assert (dirToHash.isDirectory());
    Vector<FileInputStream> fileStreams = new Vector<FileInputStream>();

    logger.debug("Found files for hashing:");
    collectInputStreams(dirToHash, fileStreams, includeSubFolders, includeHiddenFiles);

    SequenceInputStream seqStream = new SequenceInputStream(fileStreams.elements());

    try {//  w  ww .j a  va 2  s  . com
        String md5Hash = DigestUtils.md5Hex(seqStream);
        seqStream.close();
        return md5Hash;
    } catch (IOException e) {
        throw new RuntimeException("Error reading files to hash in " + dirToHash.getAbsolutePath(), e);
    }

}