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