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