use of java.io.DataOutputStream in project buck by facebook.
the class HttpArtifactCacheBinaryProtocolTest method testReadFetchResponse.
@Test
public void testReadFetchResponse() throws IOException {
final String base64EncodedData = "AAAALgAAAAEAIDAwMDAwMDAwMDEwMDAwMDAwMDAwMDA4MDAwMDAwMDAwAAAAANcwdr5kYXRh";
final RuleKey ruleKey = new RuleKey("00000000010000000000008000000000");
final String data = "data";
byte[] expectedData;
try (ByteArrayOutputStream out = new ByteArrayOutputStream();
DataOutputStream dataOut = new DataOutputStream(out)) {
byte[] metadata = HttpArtifactCacheBinaryProtocol.createMetadataHeader(ImmutableSet.of(ruleKey), ImmutableMap.of(), ByteSource.wrap(data.getBytes(Charsets.UTF_8)));
dataOut.writeInt(metadata.length);
dataOut.write(metadata);
dataOut.write(data.getBytes(Charsets.UTF_8));
expectedData = out.toByteArray();
}
assertThat(expectedData, Matchers.equalTo(BaseEncoding.base64().decode(base64EncodedData)));
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(expectedData))) {
FetchResponseReadResult result = HttpArtifactCacheBinaryProtocol.readFetchResponse(inputStream, outputStream);
assertThat(result.getRuleKeys(), Matchers.contains(ruleKey));
assertThat(outputStream.toByteArray(), Matchers.equalTo(data.getBytes(Charsets.UTF_8)));
assertThat(result.getActualHashCode(), Matchers.equalTo(HashCode.fromString("d73076be")));
assertThat(result.getExpectedHashCode(), Matchers.equalTo(HashCode.fromString("d73076be")));
assertThat(result.getMetadata(), Matchers.anEmptyMap());
assertThat(result.getResponseSizeBytes(), Matchers.equalTo(4L));
}
}
use of java.io.DataOutputStream in project deeplearning4j by deeplearning4j.
the class RemoteUIStatsStorageRouter method tryPost.
private boolean tryPost(ToPost toPost) throws IOException {
HttpURLConnection connection = getConnection();
String className;
byte[] asBytes;
StorageType type;
if (toPost.getMeta() != null) {
StorageMetaData smd = toPost.getMeta();
className = smd.getClass().getName();
asBytes = smd.encode();
type = StorageType.MetaData;
} else if (toPost.getStaticInfo() != null) {
Persistable p = toPost.getStaticInfo();
className = p.getClass().getName();
asBytes = p.encode();
type = StorageType.StaticInfo;
} else {
Persistable p = toPost.getUpdate();
className = p.getClass().getName();
asBytes = p.encode();
type = StorageType.Update;
}
String base64 = DatatypeConverter.printBase64Binary(asBytes);
Map<String, String> jsonObj = new LinkedHashMap<>();
jsonObj.put("type", type.name());
jsonObj.put("class", className);
jsonObj.put("data", base64);
String str;
try {
str = objectMapper.writeValueAsString(jsonObj);
} catch (Exception e) {
//Should never get an exception from simple Map<String,String>
throw new RuntimeException(e);
}
DataOutputStream dos = new DataOutputStream(connection.getOutputStream());
dos.writeBytes(str);
dos.flush();
dos.close();
try {
int responseCode = connection.getResponseCode();
if (responseCode != 200) {
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
log.warn("Error posting to remote UI - received response code {}\tContent: {}", response, response.toString());
return false;
}
} catch (IOException e) {
String msg = e.getMessage();
if (msg.contains("403 for URL")) {
log.warn("Error posting to remote UI at {} (Response code: 403)." + " Remote listener support is not enabled? use UIServer.getInstance().enableRemoteListener()", url, e);
} else {
log.warn("Error posting to remote UI at {}", url, e);
}
return false;
}
return true;
}
use of java.io.DataOutputStream in project che by eclipse.
the class DeltaProcessingState method saveExternalLibTimeStamps.
public void saveExternalLibTimeStamps() throws CoreException {
if (this.externalTimeStamps == null)
return;
// cleanup to avoid any leak ( https://bugs.eclipse.org/bugs/show_bug.cgi?id=244849 )
HashSet toRemove = new HashSet();
if (this.roots != null) {
Enumeration keys = this.externalTimeStamps.keys();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
if (this.roots.get(key) == null) {
toRemove.add(key);
}
}
}
File timestamps = getTimeStampsFile();
DataOutputStream out = null;
try {
out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(timestamps)));
out.writeInt(this.externalTimeStamps.size() - toRemove.size());
Iterator entries = this.externalTimeStamps.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry entry = (Map.Entry) entries.next();
IPath key = (IPath) entry.getKey();
if (!toRemove.contains(key)) {
out.writeUTF(key.toPortableString());
Long timestamp = (Long) entry.getValue();
out.writeLong(timestamp.longValue());
}
}
} catch (IOException e) {
//$NON-NLS-1$
IStatus status = new Status(IStatus.ERROR, JavaCore.PLUGIN_ID, IStatus.ERROR, "Problems while saving timestamps", e);
throw new CoreException(status);
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
// nothing we can do: ignore
}
}
}
}
use of java.io.DataOutputStream in project hive by apache.
the class ReplicationSemanticAnalyzer method writeOutput.
private void writeOutput(List<String> values, Path outputFile) throws SemanticException {
FileSystem fs = null;
DataOutputStream outStream = null;
try {
fs = outputFile.getFileSystem(conf);
outStream = fs.create(outputFile);
outStream.writeBytes((values.get(0) == null ? Utilities.nullStringOutput : values.get(0)));
for (int i = 1; i < values.size(); i++) {
outStream.write(Utilities.tabCode);
outStream.writeBytes((values.get(i) == null ? Utilities.nullStringOutput : values.get(i)));
}
outStream.write(Utilities.newLineCode);
} catch (IOException e) {
throw new SemanticException(e);
} finally {
IOUtils.closeStream(outStream);
}
}
use of java.io.DataOutputStream in project zookeeper by apache.
the class CnxManagerTest method testInitialMessage.
@Test
public void testInitialMessage() throws Exception {
InitialMessage msg;
ByteArrayOutputStream bos;
DataInputStream din;
DataOutputStream dout;
String hostport;
// message with bad protocol version
try {
// the initial message (without the protocol version)
hostport = "10.0.0.2:3888";
bos = new ByteArrayOutputStream();
dout = new DataOutputStream(bos);
// sid
dout.writeLong(5L);
dout.writeInt(hostport.getBytes().length);
dout.writeBytes(hostport);
// now parse it
din = new DataInputStream(new ByteArrayInputStream(bos.toByteArray()));
msg = InitialMessage.parse(-65530L, din);
Assert.fail("bad protocol version accepted");
} catch (InitialMessage.InitialMessageException ex) {
}
// message too long
try {
hostport = createLongString(1048576);
bos = new ByteArrayOutputStream();
dout = new DataOutputStream(bos);
// sid
dout.writeLong(5L);
dout.writeInt(hostport.getBytes().length);
dout.writeBytes(hostport);
din = new DataInputStream(new ByteArrayInputStream(bos.toByteArray()));
msg = InitialMessage.parse(QuorumCnxManager.PROTOCOL_VERSION, din);
Assert.fail("long message accepted");
} catch (InitialMessage.InitialMessageException ex) {
}
// bad hostport string
try {
hostport = "what's going on here?";
bos = new ByteArrayOutputStream();
dout = new DataOutputStream(bos);
// sid
dout.writeLong(5L);
dout.writeInt(hostport.getBytes().length);
dout.writeBytes(hostport);
din = new DataInputStream(new ByteArrayInputStream(bos.toByteArray()));
msg = InitialMessage.parse(QuorumCnxManager.PROTOCOL_VERSION, din);
Assert.fail("bad hostport accepted");
} catch (InitialMessage.InitialMessageException ex) {
}
// good message
try {
hostport = "10.0.0.2:3888";
bos = new ByteArrayOutputStream();
dout = new DataOutputStream(bos);
// sid
dout.writeLong(5L);
dout.writeInt(hostport.getBytes().length);
dout.writeBytes(hostport);
// now parse it
din = new DataInputStream(new ByteArrayInputStream(bos.toByteArray()));
msg = InitialMessage.parse(QuorumCnxManager.PROTOCOL_VERSION, din);
} catch (InitialMessage.InitialMessageException ex) {
Assert.fail(ex.toString());
}
}
Aggregations