Search in sources :

Example 16 with FileOutputStream

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

the class DataRateLimitedServletTest method testStream.

@Test
public void testStream() throws Exception {
    File content = testdir.getPathFile("content.txt").toFile();
    String[] results = new String[10];
    try (OutputStream out = new FileOutputStream(content)) {
        byte[] b = new byte[1024];
        for (int i = 1024; i-- > 0; ) {
            int index = i % 10;
            Arrays.fill(b, (byte) ('0' + (index)));
            out.write(b);
            out.write('\n');
            if (results[index] == null)
                results[index] = new String(b, StandardCharsets.US_ASCII);
        }
    }
    long start = System.currentTimeMillis();
    String response = connector.getResponse("GET /context/stream/content.txt HTTP/1.0\r\n\r\n");
    long duration = System.currentTimeMillis() - start;
    assertThat("Response", response, containsString("200 OK"));
    assertThat("Response Length", response.length(), greaterThan(1024 * 1024));
    assertThat("Duration", duration, greaterThan(PAUSE * 1024L * 1024 / BUFFER));
    for (int i = 0; i < 10; i++) assertThat(response, containsString(results[i]));
}
Also used : OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) Matchers.containsString(org.hamcrest.Matchers.containsString) File(java.io.File) Test(org.junit.Test)

Example 17 with FileOutputStream

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

the class IncludedGzipTest method setUp.

@Before
public void setUp() throws Exception {
    testdir.ensureEmpty();
    File testFile = testdir.getPathFile("file.txt").toFile();
    try (OutputStream testOut = new BufferedOutputStream(new FileOutputStream(testFile))) {
        ByteArrayInputStream testIn = new ByteArrayInputStream(__content.getBytes("ISO8859_1"));
        IO.copy(testIn, testOut);
    }
    tester = new ServletTester("/context");
    tester.getContext().setResourceBase(testdir.getPath().toString());
    tester.getContext().addServlet(org.eclipse.jetty.servlet.DefaultServlet.class, "/");
    GzipHandler gzipHandler = new GzipHandler();
    tester.getContext().insertHandler(gzipHandler);
    tester.start();
}
Also used : ServletTester(org.eclipse.jetty.servlet.ServletTester) ByteArrayInputStream(java.io.ByteArrayInputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) Before(org.junit.Before)

Example 18 with FileOutputStream

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

the class PathWatcherTest method updateFileOverTime.

/**
     * Update (optionally create) a file over time.
     * <p>
     * The file will be created in a slowed down fashion, over the time specified.
     * 
     * @param path
     *            the file to update / create
     * @param fileSize
     *            the ultimate file size to create
     * @param timeDuration
     *            the time duration to take to create the file (approximate, not 100% accurate)
     * @param timeUnit
     *            the time unit to take to create the file
     * @throws IOException
     *             if unable to write file
     * @throws InterruptedException
     *             if sleep between writes was interrupted
     */
private void updateFileOverTime(Path path, int fileSize, int timeDuration, TimeUnit timeUnit) throws IOException, InterruptedException {
    // how long to sleep between writes
    int sleepMs = 100;
    // how many millis to spend writing entire file size
    long totalMs = timeUnit.toMillis(timeDuration);
    // how many write chunks to write
    int writeCount = (int) ((int) totalMs / (int) sleepMs);
    // average chunk buffer
    int chunkBufLen = fileSize / writeCount;
    byte[] chunkBuf = new byte[chunkBufLen];
    Arrays.fill(chunkBuf, (byte) 'x');
    try (FileOutputStream out = new FileOutputStream(path.toFile())) {
        int left = fileSize;
        while (left > 0) {
            int len = Math.min(left, chunkBufLen);
            out.write(chunkBuf, 0, len);
            left -= chunkBufLen;
            out.flush();
            out.getChannel().force(true);
            // Force file to actually write to disk.
            // Skipping any sort of filesystem caching of the write
            out.getFD().sync();
            TimeUnit.MILLISECONDS.sleep(sleepMs);
        }
    }
}
Also used : FileOutputStream(java.io.FileOutputStream)

Example 19 with FileOutputStream

use of java.io.FileOutputStream 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 20 with FileOutputStream

use of java.io.FileOutputStream 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)

Aggregations

FileOutputStream (java.io.FileOutputStream)13792 File (java.io.File)8295 IOException (java.io.IOException)6166 FileInputStream (java.io.FileInputStream)2644 OutputStream (java.io.OutputStream)2605 InputStream (java.io.InputStream)2077 BufferedOutputStream (java.io.BufferedOutputStream)1755 FileNotFoundException (java.io.FileNotFoundException)1531 OutputStreamWriter (java.io.OutputStreamWriter)1440 Test (org.junit.Test)1115 ZipEntry (java.util.zip.ZipEntry)734 BufferedWriter (java.io.BufferedWriter)668 ArrayList (java.util.ArrayList)654 ZipOutputStream (java.util.zip.ZipOutputStream)642 BufferedInputStream (java.io.BufferedInputStream)604 ByteArrayOutputStream (java.io.ByteArrayOutputStream)556 PrintWriter (java.io.PrintWriter)530 Properties (java.util.Properties)497 URL (java.net.URL)478 Writer (java.io.Writer)477