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();
}
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();
}
}
}
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();
}
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());
}
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);
}
}
Aggregations