Search in sources :

Example 11 with FileInputStream

use of java.io.FileInputStream in project buck by facebook.

the class ConcatStepTest method testConcatFiles.

@Test
public void testConcatFiles() throws IOException {
    // Create three files containing "foo", "bar", and "baz"
    // and see if they are correctly concatenated.
    File dest = temp.newFile();
    ImmutableList.Builder<Path> inputsBuilder = ImmutableList.builder();
    String[] fileContents = { "foo", "bar", "baz" };
    for (int i = 0; i < fileContents.length; i++) {
        File src = temp.newFile();
        PrintStream out = new PrintStream(src);
        out.print(fileContents[i]);
        inputsBuilder.add(src.toPath());
        out.close();
    }
    ProjectFilesystem filesystem = new ProjectFilesystem(temp.getRoot().toPath());
    ExecutionContext context = TestExecutionContext.newInstance();
    ConcatStep step = new ConcatStep(filesystem, inputsBuilder.build(), dest.toPath());
    step.execute(context);
    BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(dest)));
    assertEquals(reader.readLine(), "foobarbaz");
    reader.close();
}
Also used : Path(java.nio.file.Path) PrintStream(java.io.PrintStream) InputStreamReader(java.io.InputStreamReader) ImmutableList(com.google.common.collect.ImmutableList) FileInputStream(java.io.FileInputStream) ExecutionContext(com.facebook.buck.step.ExecutionContext) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) BufferedReader(java.io.BufferedReader) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) File(java.io.File) Test(org.junit.Test)

Example 12 with FileInputStream

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

the class Starter method getConfiguration.

public void getConfiguration(String[] args) throws Exception {
    for (int i = 0; i < args.length; i++) {
        //--stop-port
        if ("--stop-port".equals(args[i]))
            stopPort = Integer.parseInt(args[++i]);
        //--stop-key
        if ("--stop-key".equals(args[i]))
            stopKey = args[++i];
        //--jettyXml
        if ("--jetty-xml".equals(args[i])) {
            jettyXmls = new ArrayList<File>();
            String[] names = StringUtil.csvSplit(args[++i]);
            for (int j = 0; names != null && j < names.length; j++) {
                jettyXmls.add(new File(names[j].trim()));
            }
        }
        //--context-xml
        if ("--context-xml".equals(args[i])) {
            contextXml = new File(args[++i]);
        }
        //--props
        if ("--props".equals(args[i])) {
            File f = new File(args[++i].trim());
            props = new Properties();
            try (InputStream in = new FileInputStream(f)) {
                props.load(in);
            }
        }
        //--token
        if ("--token".equals(args[i])) {
            token = args[++i].trim();
        }
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Properties(java.util.Properties) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 13 with FileInputStream

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

the class FileSessionDataStore method load.

/** 
     * @see org.eclipse.jetty.server.session.SessionDataStore#load(java.lang.String)
     */
@Override
public SessionData load(String id) throws Exception {
    final AtomicReference<SessionData> reference = new AtomicReference<SessionData>();
    final AtomicReference<Exception> exception = new AtomicReference<Exception>();
    Runnable r = new Runnable() {

        public void run() {
            File file = getFile(_storeDir, id);
            if (file == null || !file.exists()) {
                if (LOG.isDebugEnabled())
                    LOG.debug("No file: {}", file);
                return;
            }
            try (FileInputStream in = new FileInputStream(file)) {
                SessionData data = load(in);
                data.setLastSaved(file.lastModified());
                //delete restored file
                file.delete();
                reference.set(data);
            } catch (UnreadableSessionDataException e) {
                if (isDeleteUnrestorableFiles() && file.exists() && file.getParentFile().equals(_storeDir))
                    ;
                {
                    file.delete();
                    LOG.warn("Deleted unrestorable file for session {}", id);
                }
                exception.set(e);
            } catch (Exception e) {
                exception.set(e);
            }
        }
    };
    //ensure this runs with the context classloader set
    _context.run(r);
    if (exception.get() != null)
        throw exception.get();
    return reference.get();
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) File(java.io.File) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 14 with FileInputStream

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

the class PutFilterTest method testHandlePut.

@Test
public void testHandlePut() throws Exception {
    // generated and parsed test
    HttpTester.Request request = HttpTester.newRequest();
    HttpTester.Response response;
    // test GET
    request.setMethod("GET");
    request.setVersion("HTTP/1.0");
    request.setHeader("Host", "tester");
    request.setURI("/context/file.txt");
    response = HttpTester.parseResponse(tester.getResponses(request.generate()));
    assertEquals(HttpServletResponse.SC_NOT_FOUND, response.getStatus());
    // test PUT0
    request.setMethod("PUT");
    request.setURI("/context/file.txt");
    request.setHeader("Content-Type", "text/plain");
    String data0 = "Now is the time for all good men to come to the aid of the party";
    request.setContent(data0);
    response = HttpTester.parseResponse(tester.getResponses(request.generate()));
    assertEquals(HttpServletResponse.SC_CREATED, response.getStatus());
    File file = new File(_dir, "file.txt");
    assertTrue(file.exists());
    assertEquals(data0, IO.toString(new FileInputStream(file)));
    // test GET1
    request.setMethod("GET");
    request.setVersion("HTTP/1.0");
    request.setHeader("Host", "tester");
    request.setURI("/context/file.txt");
    response = HttpTester.parseResponse(tester.getResponses(request.generate()));
    assertEquals(HttpServletResponse.SC_OK, response.getStatus());
    assertEquals(data0, response.getContent());
    // test PUT1
    request.setMethod("PUT");
    request.setURI("/context/file.txt");
    request.setHeader("Content-Type", "text/plain");
    String data1 = "How Now BROWN COW!!!!";
    request.setContent(data1);
    response = HttpTester.parseResponse(tester.getResponses(request.generate()));
    assertEquals(HttpServletResponse.SC_OK, response.getStatus());
    file = new File(_dir, "file.txt");
    assertTrue(file.exists());
    assertEquals(data1, IO.toString(new FileInputStream(file)));
    // test PUT2
    request.setMethod("PUT");
    request.setURI("/context/file.txt");
    request.setHeader("Content-Type", "text/plain");
    String data2 = "Blah blah blah Blah blah";
    request.setContent(data2);
    String to_send = BufferUtil.toString(request.generate());
    URL url = new URL(tester.createConnector(true));
    Socket socket = new Socket(url.getHost(), url.getPort());
    OutputStream out = socket.getOutputStream();
    int l = to_send.length();
    out.write(to_send.substring(0, l - 10).getBytes());
    out.flush();
    Thread.sleep(100);
    out.write(to_send.substring(l - 10, l - 5).getBytes());
    out.flush();
    // read the file
    do {
        Thread.sleep(100);
        // test GET
        request.setMethod("GET");
        request.setVersion("HTTP/1.0");
        request.setHeader("Host", "tester");
        request.setURI("/context/file.txt");
        response = HttpTester.parseResponse(tester.getResponses(request.generate()));
    } while (response.getStatus() == 200);
    assertEquals(HttpServletResponse.SC_NOT_FOUND, response.getStatus());
    out.write(to_send.substring(l - 5).getBytes());
    out.flush();
    String in = IO.toString(socket.getInputStream());
    request.setMethod("GET");
    request.setVersion("HTTP/1.0");
    request.setHeader("Host", "tester");
    request.setURI("/context/file.txt");
    response = HttpTester.parseResponse(tester.getResponses(request.generate()));
    assertEquals(HttpServletResponse.SC_OK, response.getStatus());
    assertEquals(data2, response.getContent());
}
Also used : OutputStream(java.io.OutputStream) File(java.io.File) FileInputStream(java.io.FileInputStream) URL(java.net.URL) Socket(java.net.Socket) HttpTester(org.eclipse.jetty.http.HttpTester) Test(org.junit.Test)

Example 15 with FileInputStream

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

the class TestDirContentServlet method loadContentFileBytes.

protected byte[] loadContentFileBytes(final String fileName) throws IOException {
    String relPath = fileName;
    relPath = relPath.replaceFirst("^/context/", "");
    relPath = relPath.replaceFirst("^/", "");
    File contentFile = getTestFile(relPath);
    FileInputStream in = null;
    ByteArrayOutputStream out = null;
    try {
        in = new FileInputStream(contentFile);
        out = new ByteArrayOutputStream();
        IO.copy(in, out);
        return out.toByteArray();
    } finally {
        IO.close(out);
        IO.close(in);
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) File(java.io.File) FileInputStream(java.io.FileInputStream)

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