xref: /OpenGrok/opengrok-indexer/src/main/java/org/opengrok/indexer/analysis/StreamSource.java (revision 5d9f3aa0ca3da3a714233f987fa732f62c0965f6)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * See LICENSE.txt included in this distribution for the specific
9  * language governing permissions and limitations under the License.
10  *
11  * When distributing Covered Code, include this CDDL HEADER in each
12  * file and include the License file at LICENSE.txt.
13  * If applicable, add the following below this CDDL HEADER, with the
14  * fields enclosed by brackets "[]" replaced with your own identifying
15  * information: Portions Copyright [yyyy] [name of copyright owner]
16  *
17  * CDDL HEADER END
18  */
19 
20 /*
21  * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
22  * Portions Copyright (c) 2018, Chris Fraire <cfraire@me.com>.
23  */
24 package org.opengrok.indexer.analysis;
25 
26 import java.io.BufferedInputStream;
27 import java.io.ByteArrayInputStream;
28 import java.io.File;
29 import java.io.FileInputStream;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.nio.charset.StandardCharsets;
33 
34 /**
35  * This class lets you create {@code InputStream}s that read data from a
36  * specific source. It could be used if you need to pass a stream as an
37  * argument to a method where the stream may need to be read multiple times.
38  * Instead of passing the stream directly, you pass a {@code StreamSource}
39  * instance that generates the stream. The receiver may call
40  * {@link #getStream()} multiple times, getting a fresh stream each time,
41  * so that there may be multiple, concurrent readers that don't interfere
42  * with each other.
43  */
44 public abstract class StreamSource {
45     /**
46      * Get a stream that reads data from the input source. Every call should
47      * return a new instance so that multiple readers can read from the source
48      * without interfering with each other.
49      *
50      * @return an {@code InputStream}
51      * @throws IOException if an error occurs when opening the stream
52      */
getStream()53     public abstract InputStream getStream() throws IOException;
54 
55     /**
56      * Helper method that creates a {@code StreamSource} instance that
57      * reads data from a file.
58      *
59      * @param file the data file
60      * @return a stream source that reads from {@code file}
61      */
fromFile(final File file)62     public static StreamSource fromFile(final File file) {
63         return new StreamSource() {
64             @Override
65             public InputStream getStream() throws IOException {
66                 return new BufferedInputStream(new FileInputStream(file));
67             }
68         };
69     }
70 
71     /**
72      * Helper method that creates a {@code StreamSource} instance that
73      * reads data from a String.
74      * @param str the source string
75      * @return a stream source that reads from {@code str}
76      */
77     public static StreamSource fromString(final String str) {
78         return new StreamSource() {
79             private final byte[] sbuf = str.getBytes(StandardCharsets.UTF_8);
80 
81             @Override
82             public InputStream getStream() throws IOException {
83                 return new ByteArrayInputStream(sbuf);
84             }
85         };
86     }
87 }
88