Search in sources :

Example 6 with DataOutputStream

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));
    }
}
Also used : RuleKey(com.facebook.buck.rules.RuleKey) ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream) Test(org.junit.Test)

Example 7 with DataOutputStream

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;
}
Also used : StorageType(org.deeplearning4j.api.storage.StorageType) Persistable(org.deeplearning4j.api.storage.Persistable) InputStreamReader(java.io.InputStreamReader) DataOutputStream(java.io.DataOutputStream) IOException(java.io.IOException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) StorageMetaData(org.deeplearning4j.api.storage.StorageMetaData) HttpURLConnection(java.net.HttpURLConnection) BufferedReader(java.io.BufferedReader)

Example 8 with DataOutputStream

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
            }
        }
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) Enumeration(java.util.Enumeration) IPath(org.eclipse.core.runtime.IPath) DataOutputStream(java.io.DataOutputStream) IOException(java.io.IOException) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) CoreException(org.eclipse.core.runtime.CoreException) FileOutputStream(java.io.FileOutputStream) Iterator(java.util.Iterator) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 9 with DataOutputStream

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);
    }
}
Also used : DataOutputStream(java.io.DataOutputStream) FileSystem(org.apache.hadoop.fs.FileSystem) IOException(java.io.IOException)

Example 10 with DataOutputStream

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());
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) InitialMessage(org.apache.zookeeper.server.quorum.QuorumCnxManager.InitialMessage) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream) Test(org.junit.Test)

Aggregations

DataOutputStream (java.io.DataOutputStream)2957 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1309 IOException (java.io.IOException)1019 Test (org.junit.Test)633 DataInputStream (java.io.DataInputStream)611 FileOutputStream (java.io.FileOutputStream)426 ByteArrayInputStream (java.io.ByteArrayInputStream)409 File (java.io.File)279 BufferedOutputStream (java.io.BufferedOutputStream)227 UnitTest (org.apache.geode.test.junit.categories.UnitTest)172 URL (java.net.URL)149 InputStreamReader (java.io.InputStreamReader)144 BufferedReader (java.io.BufferedReader)140 Path (org.apache.hadoop.fs.Path)137 DataInput (java.io.DataInput)124 ArrayList (java.util.ArrayList)122 HttpURLConnection (java.net.HttpURLConnection)121 FSDataOutputStream (org.apache.hadoop.fs.FSDataOutputStream)117 FileInputStream (java.io.FileInputStream)107 InputStream (java.io.InputStream)107