Example usage for java.io DataOutputStream DataOutputStream

List of usage examples for java.io DataOutputStream DataOutputStream

Introduction

In this page you can find the example usage for java.io DataOutputStream DataOutputStream.

Prototype

public DataOutputStream(OutputStream out) 

Source Link

Document

Creates a new data output stream to write data to the specified underlying output stream.

Usage

From source file:CounterServer.java

public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    HttpSession session = req.getSession(true);
    int count = 1;
    Integer i = (Integer) session.getAttribute(COUNTER_KEY);
    if (i != null) {
        count = i.intValue() + 5;/*from   w ww .j a va2  s.  c o m*/
    }
    session.setAttribute(COUNTER_KEY, new Integer(count));
    DataInputStream in = new DataInputStream(req.getInputStream());
    resp.setContentType("application/octet-stream");
    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    DataOutputStream out = new DataOutputStream(byteOut);
    out.writeInt(count);
    out.flush();
    byte[] buf = byteOut.toByteArray();
    resp.setContentLength(buf.length);
    ServletOutputStream servletOut = resp.getOutputStream();
    servletOut.write(buf);
    servletOut.close();
}

From source file:com.lightboxtechnologies.spectrum.FolderCount.java

static String convertScanToString(Scan scan) throws IOException {
    final ByteArrayOutputStream out = new ByteArrayOutputStream();

    DataOutputStream dos = null;// w  w w.j  ava  2 s.c  om
    try {
        dos = new DataOutputStream(out);
        scan.write(dos);
        dos.close();
    } finally {
        IOUtils.closeQuietly(dos);
    }

    return Base64.encodeBytes(out.toByteArray());
}

From source file:com.fullhousedev.globalchat.bukkit.PluginMessageManager.java

public static void getServerName(Plugin pl) {
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    DataOutputStream out = new DataOutputStream(b);

    try {/*from   ww w .j ava  2s .c  o m*/
        out.writeUTF("GetServer");
    } catch (IOException ex) {
        Logger.getLogger(GlobalChat.class.getName()).log(Level.SEVERE, null, ex);
    }

    if (Bukkit.getOnlinePlayers().length == 0) {
        EventListeners.waitingOnJoin = true;
        return;
    }
    Player p = Bukkit.getOnlinePlayers()[0];

    p.sendPluginMessage(pl, "BungeeCord", b.toByteArray());
}

From source file:ro.fortsoft.matilda.util.RecaptchaUtils.java

public static boolean verify(String recaptchaResponse, String secret) {
    if (StringUtils.isNullOrEmpty(recaptchaResponse)) {
        return false;
    }//from w w  w  .j  a  v a 2 s.co m

    boolean result = false;
    try {
        URL url = new URL("https://www.google.com/recaptcha/api/siteverify");
        HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();

        // add request header
        connection.setRequestMethod("POST");
        connection.setRequestProperty("User-Agent", "Mozilla/5.0");
        connection.setRequestProperty("Accept-Language", "en-US,en;q=0.5");

        String postParams = "secret=" + secret + "&response=" + recaptchaResponse;
        //            log.debug("Post parameters '{}'", postParams);

        // send post request
        connection.setDoOutput(true);
        DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
        outputStream.writeBytes(postParams);
        outputStream.flush();
        outputStream.close();

        int responseCode = connection.getResponseCode();
        log.debug("Response code '{}'", responseCode);

        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        StringBuilder response = new StringBuilder();

        String line;
        while ((line = reader.readLine()) != null) {
            response.append(line);
        }
        reader.close();

        // print result
        log.debug("Response '{}'", response.toString());

        // parse JSON response and return 'success' value
        ObjectMapper mapper = new ObjectMapper();
        JsonNode rootNode = mapper.readTree(response.toString());
        JsonNode nameNode = rootNode.path("success");

        result = nameNode.asBoolean();
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }

    return result;
}

From source file:Main.IrcBot.java

public static void postGit(String pasteBinSnippet, PrintWriter out) {
    try {/*from w  w w  .  j  av a2s . com*/
        String url = "https://api.github.com/gists";
        URL obj = new URL(url);
        HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

        //add reuqest header
        JSONObject x = new JSONObject();
        JSONObject y = new JSONObject();
        JSONObject z = new JSONObject();
        z.put("content", pasteBinSnippet);
        y.put("index.txt", z);
        x.put("public", true);
        x.put("description", "LPBOT");
        x.put("files", y);
        con.setRequestMethod("POST");
        con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");

        String urlParameters = "public=true&description=LPBOT";

        // Send post request
        con.setDoOutput(true);
        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
        wr.writeBytes(x.toString());
        wr.flush();
        wr.close();

        int responseCode = con.getResponseCode();
        System.out.println("\nSending 'POST' request to URL : " + url);
        System.out.println("Post parameters : " + urlParameters);
        System.out.println("Response Code : " + responseCode);

        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        //print result
        JSONObject gitResponse = new JSONObject(response.toString());
        String newGitUrl = gitResponse.getString("html_url");

        if (newGitUrl.length() > 0) {
            out.println("PRIVMSG #learnprogramming :The paste on Git: " + newGitUrl);
        }
        System.out.println(newGitUrl);
    } catch (Exception p) {

    }
}

From source file:nl.dreamkernel.s4.tweaker.util.RootProcess.java

public boolean init() {
    try {/*w ww  . ja va  2 s  .  c om*/
        mProcess = Runtime.getRuntime().exec("su");
        mOutputStream = new DataOutputStream(mProcess.getOutputStream());
        mInputStream = new DataInputStream(mProcess.getInputStream());
        if (write("su -v\n")) {
            String[] results = read();
            for (String line : results) {
                if (line.length() > 0) {
                    return true;
                }
            }
        }
    } catch (IOException e) {
    }
    return false;
}

From source file:com.mbrlabs.mundus.utils.TerrainIO.java

/**
 * Binary gziped format./*from  w  ww.j a v  a2  s. com*/
 *
 * @param terrain
 */
public static void exportTerrain(ProjectContext projectContext, Terrain terrain) {
    float[] data = terrain.heightData;
    long start = System.currentTimeMillis();

    // create file
    File file = new File(FilenameUtils.concat(projectContext.path, terrain.terraPath));
    try {
        FileUtils.touch(file);
    } catch (IOException e) {
        e.printStackTrace();
    }

    // write .terra
    try (DataOutputStream outputStream = new DataOutputStream(
            new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(file))))) {

        for (float f : data) {
            outputStream.writeFloat(f);
        }
        outputStream.flush();
    } catch (IOException e) {
        e.printStackTrace();
        return;
    }

    // write splatmap
    SplatMap splatmap = terrain.getTerrainTexture().getSplatmap();
    if (splatmap != null) {
        splatmap.savePNG(Gdx.files.absolute(FilenameUtils.concat(projectContext.path, splatmap.getPath())));
    }

    //Log.debug("Terrain export execution time (" + data.length + " floats): "
    //        + (System.currentTimeMillis() - start) + " ms");
}

From source file:net.portalblockz.portalbot.urlshorteners.GooGl.java

@Override
public String shorten(String url) {
    StringBuilder response = new StringBuilder();
    try {//from  w w  w . java  2 s  .  c  o  m
        URL req = new URL("https://www.googleapis.com/urlshortener/v1/url");

        HttpsURLConnection con = (HttpsURLConnection) req.openConnection();
        con.setRequestMethod("POST");
        con.setRequestProperty("Content-Type", "application/json");
        con.setDoOutput(true);

        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
        wr.writeBytes("{\"longUrl\": \"" + url + "\"}");
        wr.flush();
        wr.close();

        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        JSONObject res = new JSONObject(response.toString());
        if (res.optString("id") != null)
            return res.getString("id");

    } catch (Exception ignored) {
        ignored.printStackTrace();
        System.out.print(response.toString());
    }
    return null;
}

From source file:cfa.vo.interop.EncodeDoubleArray.java

private static byte[] doubleToByte(double[] data) throws IOException {

    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    DataOutputStream outputStream = new DataOutputStream(byteStream);

    try {//from w w w  . j  a  v a2s.  com

        for (int ii = 0; ii < data.length; ii++)
            outputStream.writeDouble(data[ii]);

    } catch (EOFException e) {
        throw new IOException("Unable to read from dataInputStream, found EOF");

    } catch (IOException e) {
        throw new IOException("Unable to read from dataInputStream, IO error");
    }

    byte[] result = byteStream.toByteArray();

    return result;
}

From source file:com.facebook.infrastructure.db.ReadResponse.java

public static Message makeReadResponseMessage(ReadResponse readResponse) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(bos);
    ReadResponse.serializer().serialize(readResponse, dos);
    Message message = new Message(StorageService.getLocalStorageEndPoint(), MessagingService.responseStage_,
            MessagingService.responseVerbHandler_, bos.toByteArray());
    return message;
}