use of java.io.ByteArrayOutputStream in project jetty.project by eclipse.
the class HttpServerTestBase method testRecycledWriters.
@Test
public void testRecycledWriters() throws Exception {
configureServer(new EchoHandler());
try (Socket client = newSocket(_serverURI.getHost(), _serverURI.getPort())) {
OutputStream os = client.getOutputStream();
InputStream is = client.getInputStream();
os.write(("POST /echo?charset=utf-8 HTTP/1.1\r\n" + "host: " + _serverURI.getHost() + ":" + _serverURI.getPort() + "\r\n" + "content-type: text/plain; charset=utf-8\r\n" + "content-length: 10\r\n" + "\r\n").getBytes(StandardCharsets.ISO_8859_1));
os.write(("123456789\n").getBytes("utf-8"));
os.write(("POST /echo?charset=utf-8 HTTP/1.1\r\n" + "host: " + _serverURI.getHost() + ":" + _serverURI.getPort() + "\r\n" + "content-type: text/plain; charset=utf-8\r\n" + "content-length: 10\r\n" + "\r\n").getBytes(StandardCharsets.ISO_8859_1));
os.write(("abcdefghZ\n").getBytes("utf-8"));
String content = "Wibble";
byte[] contentB = content.getBytes("utf-8");
os.write(("POST /echo?charset=utf-16 HTTP/1.1\r\n" + "host: " + _serverURI.getHost() + ":" + _serverURI.getPort() + "\r\n" + "content-type: text/plain; charset=utf-8\r\n" + "content-length: " + contentB.length + "\r\n" + "connection: close\r\n" + "\r\n").getBytes(StandardCharsets.ISO_8859_1));
os.write(contentB);
os.flush();
ByteArrayOutputStream bout = new ByteArrayOutputStream();
IO.copy(is, bout);
byte[] b = bout.toByteArray();
//System.err.println("OUTPUT: "+new String(b));
int i = 0;
while (b[i] != 'Z') i++;
int state = 0;
while (state != 4) {
switch(b[i++]) {
case '\r':
if (state == 0 || state == 2)
state++;
continue;
case '\n':
if (state == 1 || state == 3)
state++;
continue;
default:
state = 0;
}
}
String in = new String(b, 0, i, StandardCharsets.UTF_8);
assertThat(in, containsString("123456789"));
assertThat(in, containsString("abcdefghZ"));
assertFalse(in.contains("Wibble"));
in = new String(b, i, b.length - i, StandardCharsets.UTF_16);
assertEquals("Wibble\n", in);
}
}
use of java.io.ByteArrayOutputStream in project jetty.project by eclipse.
the class DatabaseLoginServiceTestServer method runscript.
public static int runscript(File scriptFile) throws Exception {
//System.err.println("Running script:"+scriptFile.getAbsolutePath());
try (FileInputStream fileStream = new FileInputStream(scriptFile)) {
Loader.loadClass("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
Connection connection = DriverManager.getConnection(__dbURL, "", "");
ByteArrayOutputStream out = new ByteArrayOutputStream();
return ij.runScript(connection, fileStream, "UTF-8", out, "UTF-8");
}
}
use of java.io.ByteArrayOutputStream in project vert.x by eclipse.
the class Http2ClientTest method testResponseCompression.
private void testResponseCompression(boolean enabled) throws Exception {
byte[] expected = TestUtils.randomAlphaString(1000).getBytes();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
GZIPOutputStream in = new GZIPOutputStream(baos);
in.write(expected);
in.close();
byte[] compressed = baos.toByteArray();
server.close();
server = vertx.createHttpServer(serverOptions);
server.requestHandler(req -> {
assertEquals(enabled ? "deflate, gzip" : null, req.getHeader(HttpHeaderNames.ACCEPT_ENCODING));
req.response().putHeader(HttpHeaderNames.CONTENT_ENCODING.toLowerCase(), "gzip").end(Buffer.buffer(compressed));
});
startServer();
client.close();
client = vertx.createHttpClient(clientOptions.setTryUseCompression(enabled));
client.get(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, "/somepath", resp -> {
String encoding = resp.getHeader(HttpHeaderNames.CONTENT_ENCODING);
assertEquals(enabled ? null : "gzip", encoding);
resp.bodyHandler(buff -> {
assertEquals(Buffer.buffer(enabled ? expected : compressed), buff);
testComplete();
});
}).end();
await();
}
use of java.io.ByteArrayOutputStream in project buck by facebook.
the class SchemeGenerator method writeScheme.
public Path writeScheme() throws IOException {
Map<PBXTarget, XCScheme.BuildableReference> buildTargetToBuildableReferenceMap = Maps.newHashMap();
for (PBXTarget target : Iterables.concat(orderedBuildTargets, orderedBuildTestTargets)) {
String blueprintName = target.getProductName();
if (blueprintName == null) {
blueprintName = target.getName();
}
Path outputPath = outputDirectory.getParent();
String buildableReferencePath;
Path projectPath = Preconditions.checkNotNull(targetToProjectPathMap.get(target));
if (outputPath == null) {
//Root directory project
buildableReferencePath = projectPath.toString();
} else {
buildableReferencePath = outputPath.relativize(projectPath).toString();
}
XCScheme.BuildableReference buildableReference = new XCScheme.BuildableReference(buildableReferencePath, Preconditions.checkNotNull(target.getGlobalID()), target.getProductReference() != null ? target.getProductReference().getName() : Preconditions.checkNotNull(target.getProductName()), blueprintName);
buildTargetToBuildableReferenceMap.put(target, buildableReference);
}
XCScheme.BuildAction buildAction = new XCScheme.BuildAction(parallelizeBuild);
// For aesthetic reasons put all non-test build actions before all test build actions.
for (PBXTarget target : orderedBuildTargets) {
addBuildActionForBuildTarget(buildTargetToBuildableReferenceMap.get(target), !primaryTargetIsBuildWithBuck || !primaryTarget.isPresent() || target.equals(primaryTarget.get()) ? XCScheme.BuildActionEntry.BuildFor.DEFAULT : XCScheme.BuildActionEntry.BuildFor.INDEXING, buildAction);
}
for (PBXTarget target : orderedBuildTestTargets) {
addBuildActionForBuildTarget(buildTargetToBuildableReferenceMap.get(target), XCScheme.BuildActionEntry.BuildFor.TEST_ONLY, buildAction);
}
XCScheme.TestAction testAction = new XCScheme.TestAction(Preconditions.checkNotNull(actionConfigNames.get(SchemeActionType.TEST)));
for (PBXTarget target : orderedRunTestTargets) {
XCScheme.BuildableReference buildableReference = buildTargetToBuildableReferenceMap.get(target);
XCScheme.TestableReference testableReference = new XCScheme.TestableReference(buildableReference);
testAction.addTestableReference(testableReference);
}
Optional<XCScheme.LaunchAction> launchAction = Optional.empty();
Optional<XCScheme.ProfileAction> profileAction = Optional.empty();
if (primaryTarget.isPresent()) {
XCScheme.BuildableReference primaryBuildableReference = buildTargetToBuildableReferenceMap.get(primaryTarget.get());
if (primaryBuildableReference != null) {
launchAction = Optional.of(new XCScheme.LaunchAction(primaryBuildableReference, Preconditions.checkNotNull(actionConfigNames.get(SchemeActionType.LAUNCH)), runnablePath, remoteRunnablePath, launchStyle));
profileAction = Optional.of(new XCScheme.ProfileAction(primaryBuildableReference, Preconditions.checkNotNull(actionConfigNames.get(SchemeActionType.PROFILE))));
}
}
XCScheme.AnalyzeAction analyzeAction = new XCScheme.AnalyzeAction(Preconditions.checkNotNull(actionConfigNames.get(SchemeActionType.ANALYZE)));
XCScheme.ArchiveAction archiveAction = new XCScheme.ArchiveAction(Preconditions.checkNotNull(actionConfigNames.get(SchemeActionType.ARCHIVE)));
XCScheme scheme = new XCScheme(schemeName, Optional.of(buildAction), Optional.of(testAction), launchAction, profileAction, Optional.of(analyzeAction), Optional.of(archiveAction));
outputScheme = Optional.of(scheme);
Path schemeDirectory = outputDirectory.resolve("xcshareddata/xcschemes");
projectFilesystem.mkdirs(schemeDirectory);
Path schemePath = schemeDirectory.resolve(schemeName + ".xcscheme");
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
serializeScheme(scheme, outputStream);
String contentsToWrite = outputStream.toString();
if (MoreProjectFilesystems.fileContentsDiffer(new ByteArrayInputStream(contentsToWrite.getBytes(Charsets.UTF_8)), schemePath, projectFilesystem)) {
projectFilesystem.writeContentsToPath(outputStream.toString(), schemePath);
}
}
return schemePath;
}
use of java.io.ByteArrayOutputStream in project buck by facebook.
the class WorkspaceGenerator method writeWorkspace.
public Path writeWorkspace() throws IOException {
DocumentBuilder docBuilder;
Transformer transformer;
try {
docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
transformer = TransformerFactory.newInstance().newTransformer();
} catch (ParserConfigurationException | TransformerConfigurationException e) {
throw new RuntimeException(e);
}
DOMImplementation domImplementation = docBuilder.getDOMImplementation();
final Document doc = domImplementation.createDocument(/* namespaceURI */
null, "Workspace", /* docType */
null);
doc.setXmlVersion("1.0");
Element rootElem = doc.getDocumentElement();
rootElem.setAttribute("version", "1.0");
final Stack<Element> groups = new Stack<>();
groups.push(rootElem);
FileVisitor<Map.Entry<String, WorkspaceNode>> visitor = new FileVisitor<Map.Entry<String, WorkspaceNode>>() {
@Override
public FileVisitResult preVisitDirectory(Map.Entry<String, WorkspaceNode> dir, BasicFileAttributes attrs) throws IOException {
Preconditions.checkArgument(dir.getValue() instanceof WorkspaceGroup);
Element element = doc.createElement("Group");
element.setAttribute("location", "container:");
element.setAttribute("name", dir.getKey());
groups.peek().appendChild(element);
groups.push(element);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Map.Entry<String, WorkspaceNode> file, BasicFileAttributes attrs) throws IOException {
Preconditions.checkArgument(file.getValue() instanceof WorkspaceFileRef);
WorkspaceFileRef fileRef = (WorkspaceFileRef) file.getValue();
Element element = doc.createElement("FileRef");
element.setAttribute("location", "container:" + MorePaths.relativize(MorePaths.normalize(outputDirectory), fileRef.getPath()).toString());
groups.peek().appendChild(element);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Map.Entry<String, WorkspaceNode> file, IOException exc) throws IOException {
return FileVisitResult.TERMINATE;
}
@Override
public FileVisitResult postVisitDirectory(Map.Entry<String, WorkspaceNode> dir, IOException exc) throws IOException {
groups.pop();
return FileVisitResult.CONTINUE;
}
};
walkNodeTree(visitor);
Path projectWorkspaceDir = getWorkspaceDir();
projectFilesystem.mkdirs(projectWorkspaceDir);
Path serializedWorkspace = projectWorkspaceDir.resolve("contents.xcworkspacedata");
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(outputStream);
transformer.transform(source, result);
String contentsToWrite = outputStream.toString();
if (MoreProjectFilesystems.fileContentsDiffer(new ByteArrayInputStream(contentsToWrite.getBytes(Charsets.UTF_8)), serializedWorkspace, projectFilesystem)) {
projectFilesystem.writeContentsToPath(contentsToWrite, serializedWorkspace);
}
} catch (TransformerException e) {
throw new RuntimeException(e);
}
Path xcshareddata = projectWorkspaceDir.resolve("xcshareddata");
projectFilesystem.mkdirs(xcshareddata);
Path workspaceSettingsPath = xcshareddata.resolve("WorkspaceSettings.xcsettings");
String workspaceSettings = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\"" + " \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n" + "<plist version=\"1.0\">\n" + "<dict>\n" + "\t<key>IDEWorkspaceSharedSettings_AutocreateContextsIfNeeded</key>\n" + "\t<false/>\n" + "</dict>\n" + "</plist>";
projectFilesystem.writeContentsToPath(workspaceSettings, workspaceSettingsPath);
return projectWorkspaceDir;
}
Aggregations