Search in sources :

Example 16 with FileInputStream

use of java.io.FileInputStream in project jetty.project by eclipse.

the class ResourceHandlerTest method setUp.

@BeforeClass
public static void setUp() throws Exception {
    File dir = MavenTestingUtils.getTargetFile("test-classes/simple");
    File bigger = new File(dir, "bigger.txt");
    File big = new File(dir, "big.txt");
    try (OutputStream out = new FileOutputStream(bigger)) {
        for (int i = 0; i < 100; i++) {
            try (InputStream in = new FileInputStream(big)) {
                IO.copy(in, out);
            }
        }
    }
    bigger.deleteOnExit();
    // determine how the SCM of choice checked out the big.txt EOL
    // we can't just use whatever is the OS default.
    // because, for example, a windows system using git can be configured for EOL handling using
    // local, remote, file lists, patterns, etc, rendering assumptions about the OS EOL choice
    // wrong for unit tests.
    LN = System.getProperty("line.separator");
    try (BufferedReader reader = Files.newBufferedReader(big.toPath(), StandardCharsets.UTF_8)) {
        // a buffer large enough to capture at least 1 EOL
        char[] cbuf = new char[128];
        reader.read(cbuf);
        String sample = new String(cbuf);
        if (sample.contains("\r\n")) {
            LN = "\r\n";
        } else if (sample.contains("\n\r")) {
            LN = "\n\r";
        } else {
            LN = "\n";
        }
    }
    _server = new Server();
    _config = new HttpConfiguration();
    _config.setOutputBufferSize(2048);
    _connector = new ServerConnector(_server, new HttpConnectionFactory(_config));
    _local = new LocalConnector(_server);
    _server.setConnectors(new Connector[] { _connector, _local });
    _resourceHandler = new ResourceHandler();
    _resourceHandler.setResourceBase(MavenTestingUtils.getTargetFile("test-classes/simple").getAbsolutePath());
    _resourceHandler.setWelcomeFiles(new String[] { "welcome.txt" });
    _contextHandler = new ContextHandler("/resource");
    _contextHandler.setHandler(_resourceHandler);
    _server.setHandler(_contextHandler);
    _server.start();
}
Also used : Server(org.eclipse.jetty.server.Server) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) LocalConnector(org.eclipse.jetty.server.LocalConnector) Matchers.containsString(org.hamcrest.Matchers.containsString) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) FileInputStream(java.io.FileInputStream) ServerConnector(org.eclipse.jetty.server.ServerConnector) FileOutputStream(java.io.FileOutputStream) BufferedReader(java.io.BufferedReader) File(java.io.File) BeforeClass(org.junit.BeforeClass)

Example 17 with FileInputStream

use of java.io.FileInputStream in project jetty.project by eclipse.

the class ResourceHandlerTest method testSlowBiggest.

@Test
@Slow
public void testSlowBiggest() throws Exception {
    _connector.setIdleTimeout(10000);
    File dir = MavenTestingUtils.getTargetFile("test-classes/simple");
    File biggest = new File(dir, "biggest.txt");
    try (OutputStream out = new FileOutputStream(biggest)) {
        for (int i = 0; i < 10; i++) {
            try (InputStream in = new FileInputStream(new File(dir, "bigger.txt"))) {
                IO.copy(in, out);
            }
        }
        out.write("\nTHE END\n".getBytes(StandardCharsets.ISO_8859_1));
    }
    biggest.deleteOnExit();
    try (Socket socket = new Socket("localhost", _connector.getLocalPort());
        OutputStream out = socket.getOutputStream();
        InputStream in = socket.getInputStream()) {
        socket.getOutputStream().write("GET /resource/biggest.txt HTTP/1.0\n\n".getBytes());
        byte[] array = new byte[102400];
        ByteBuffer buffer = null;
        while (true) {
            Thread.sleep(100);
            int len = in.read(array);
            if (len < 0)
                break;
            buffer = BufferUtil.toBuffer(array, 0, len);
        // System.err.println(++i+": "+BufferUtil.toDetailString(buffer));
        }
        Assert.assertEquals('E', buffer.get(buffer.limit() - 4));
        Assert.assertEquals('N', buffer.get(buffer.limit() - 3));
        Assert.assertEquals('D', buffer.get(buffer.limit() - 2));
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) ByteBuffer(java.nio.ByteBuffer) FileInputStream(java.io.FileInputStream) Socket(java.net.Socket) Test(org.junit.Test) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

Example 18 with FileInputStream

use of java.io.FileInputStream in project che by eclipse.

the class RecipeLoader method getResource.

/**
     * Searches for resource by given path.
     *
     * @param resource
     *         path to resource
     * @return resource InputStream
     * @throws IOException
     *         when problem occurs during resource getting
     */
private InputStream getResource(String resource) throws IOException {
    File resourceFile = new File(resource);
    if (resourceFile.exists() && !resourceFile.isFile()) {
        throw new IOException(String.format("%s is not a file. ", resourceFile.getAbsolutePath()));
    }
    InputStream is = resourceFile.exists() ? new FileInputStream(resourceFile) : Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
    if (is == null) {
        throw new IOException(String.format("Not found resource: %s", resource));
    }
    return is;
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) JsonIOException(com.google.gson.JsonIOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 19 with FileInputStream

use of java.io.FileInputStream in project druid by druid-io.

the class SmooshedFileMapper method load.

public static SmooshedFileMapper load(File baseDir) throws IOException {
    File metaFile = FileSmoosher.metaFile(baseDir);
    BufferedReader in = null;
    try {
        in = new BufferedReader(new InputStreamReader(new FileInputStream(metaFile), Charsets.UTF_8));
        String line = in.readLine();
        if (line == null) {
            throw new ISE("First line should be version,maxChunkSize,numChunks, got null.");
        }
        String[] splits = line.split(",");
        if (!"v1".equals(splits[0])) {
            throw new ISE("Unknown version[%s], v1 is all I know.", splits[0]);
        }
        if (splits.length != 3) {
            throw new ISE("Wrong number of splits[%d] in line[%s]", splits.length, line);
        }
        final Integer numFiles = Integer.valueOf(splits[2]);
        List<File> outFiles = Lists.newArrayListWithExpectedSize(numFiles);
        for (int i = 0; i < numFiles; ++i) {
            outFiles.add(FileSmoosher.makeChunkFile(baseDir, i));
        }
        Map<String, Metadata> internalFiles = Maps.newTreeMap();
        while ((line = in.readLine()) != null) {
            splits = line.split(",");
            if (splits.length != 4) {
                throw new ISE("Wrong number of splits[%d] in line[%s]", splits.length, line);
            }
            internalFiles.put(splits[0], new Metadata(Integer.parseInt(splits[1]), Integer.parseInt(splits[2]), Integer.parseInt(splits[3])));
        }
        return new SmooshedFileMapper(outFiles, internalFiles);
    } finally {
        Closeables.close(in, false);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) FileInputStream(java.io.FileInputStream) BufferedReader(java.io.BufferedReader) ISE(io.druid.java.util.common.ISE) File(java.io.File)

Example 20 with FileInputStream

use of java.io.FileInputStream in project druid by druid-io.

the class CompressionUtilsTest method testGoodGzipByteSource.

@Test
public void testGoodGzipByteSource() throws IOException {
    final File tmpDir = temporaryFolder.newFolder("testGoodGzipByteSource");
    final File gzFile = new File(tmpDir, testFile.getName() + ".gz");
    Assert.assertFalse(gzFile.exists());
    CompressionUtils.gzip(Files.asByteSource(testFile), Files.asByteSink(gzFile), Predicates.<Throwable>alwaysTrue());
    Assert.assertTrue(gzFile.exists());
    try (final InputStream inputStream = CompressionUtils.gzipInputStream(new FileInputStream(gzFile))) {
        assertGoodDataStream(inputStream);
    }
    if (!testFile.delete()) {
        throw new IOException(String.format("Unable to delete file [%s]", testFile.getAbsolutePath()));
    }
    Assert.assertFalse(testFile.exists());
    CompressionUtils.gunzip(Files.asByteSource(gzFile), testFile);
    Assert.assertTrue(testFile.exists());
    try (final InputStream inputStream = new FileInputStream(testFile)) {
        assertGoodDataStream(inputStream);
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) FilterInputStream(java.io.FilterInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Aggregations

FileInputStream (java.io.FileInputStream)14180 File (java.io.File)6801 IOException (java.io.IOException)6283 InputStream (java.io.InputStream)4121 FileOutputStream (java.io.FileOutputStream)2210 FileNotFoundException (java.io.FileNotFoundException)2002 InputStreamReader (java.io.InputStreamReader)1733 BufferedInputStream (java.io.BufferedInputStream)1593 Test (org.junit.Test)1548 BufferedReader (java.io.BufferedReader)1414 Properties (java.util.Properties)1410 ArrayList (java.util.ArrayList)960 OutputStream (java.io.OutputStream)753 ByteArrayInputStream (java.io.ByteArrayInputStream)599 HashMap (java.util.HashMap)597 ZipEntry (java.util.zip.ZipEntry)575 DataInputStream (java.io.DataInputStream)549 GZIPInputStream (java.util.zip.GZIPInputStream)435 KeyStore (java.security.KeyStore)430 ByteArrayOutputStream (java.io.ByteArrayOutputStream)407