Example usage for java.io DataOutputStream write

List of usage examples for java.io DataOutputStream write

Introduction

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

Prototype

public synchronized void write(int b) throws IOException 

Source Link

Document

Writes the specified byte (the low eight bits of the argument b) to the underlying output stream.

Usage

From source file:org.mule.transport.tcp.protocols.LengthProtocol.java

@Override
protected void writeByteArray(OutputStream os, byte[] data) throws IOException {
    // Write the length and then the data.
    DataOutputStream dos = new DataOutputStream(os);
    dos.writeInt(data.length);/* w  w w. j a  v  a  2 s . c o m*/
    dos.write(data);
    // DataOutputStream size is SIZE_INT + the byte length, due to the writeInt call
    // this should fix EE-1494
    if (dos.size() != data.length + SIZE_INT) {
        // only flush if the sizes don't match up
        dos.flush();
    }
}

From source file:com.rpgsheet.xcom.dao.SaveGameDaoImpl.java

private void writeIt(int saveSlot, String fileName, byte[] data) {
    File saveFile = ufoGameFileService.getSaveFile(saveSlot, fileName);
    try {/*from  ww  w  .  j  a  va  2s .  c o m*/
        FileOutputStream fileOutputStream = new FileOutputStream(saveFile);
        DataOutputStream dataOutputStream = new DataOutputStream(fileOutputStream);
        dataOutputStream.write(data);
    } catch (FileNotFoundException e) {
        e.printStackTrace(System.err);
    } catch (IOException e) {
        e.printStackTrace(System.err);
    }
}

From source file:net.sourceforge.fenixedu.presentationTier.Action.commons.student.ViewStudentApplicationDA.java

public ActionForward downloadDocument(final ActionMapping mapping, final ActionForm form,
        final HttpServletRequest request, final HttpServletResponse response) throws IOException {
    final IndividualCandidacyDocumentFile document = getDomainObject(request, "documentOID");
    if (document != null && isAuthorized(document)) {
        response.setContentType(document.getContentType());
        response.addHeader("Content-Disposition", "attachment; filename=\"" + document.getFilename() + "\"");
        response.setContentLength(document.getSize().intValue());
        final DataOutputStream dos = new DataOutputStream(response.getOutputStream());
        dos.write(document.getContent());
        dos.close();/*  w  w  w .  j  ava 2s  .  c  o m*/
    } else {
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        response.getWriter().write(HttpStatus.getStatusText(HttpStatus.SC_BAD_REQUEST));
        response.getWriter().close();
    }
    return null;
}

From source file:net.mohatu.bloocoin.miner.RegisterClass.java

private void register() {
    try {//  w ww  .j  a  v  a2s .c o  m
        String result = new String();
        Socket sock = new Socket(this.url, this.port);
        String command = "{\"cmd\":\"register" + "\",\"addr\":\"" + addr + "\",\"pwd\":\"" + key + "\"}";
        DataInputStream is = new DataInputStream(sock.getInputStream());
        DataOutputStream os = new DataOutputStream(sock.getOutputStream());
        os.write(command.getBytes());
        os.flush();

        BufferedReader in = new BufferedReader(new InputStreamReader(is));
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            result += inputLine;
        }

        is.close();
        os.close();
        sock.close();
        System.out.println(result);
        if (result.contains("\"success\": true")) {
            System.out.println("Registration successful: " + addr);
            saveBloostamp();
        } else if (result.contains("\"success\": false")) {
            System.out.println("Result: Failed");
            MainView.updateStatusText("Registration failed. ");
            System.exit(0);
        }
    } catch (UnknownHostException e) {
        System.out.println("Error: Unknown host.");
    } catch (IOException e) {
        System.out.println("Error: Network error.");
    }
}

From source file:com.gagein.crawler.FileDownLoader.java

/**
 * ? filePath ???/*  w  ww  .  ja  va 2s  .  c  o  m*/
 */
private void saveToLocal(byte[] data, String filePath) {
    try {
        System.out.println("========SaveToLocal========filePath===" + filePath);
        DataOutputStream out = new DataOutputStream(new FileOutputStream(new File(filePath)));
        for (int i = 0; i < data.length; i++)
            out.write(data[i]);
        out.flush();
        out.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:org.apache.tajo.ws.rs.resources.outputs.CSVStreamingOutput.java

@Override
public void write(OutputStream outputStream) throws IOException, WebApplicationException {
    fetch();/*from  w w  w .  ja v  a2  s . c o m*/
    DataOutputStream streamingOutputStream = new DataOutputStream(new BufferedOutputStream(outputStream));
    streamingOutputStream.write(output.getBytes("utf-8"));
    streamingOutputStream.flush();
}

From source file:net.technicpack.minecraftcore.mojang.auth.AuthenticationService.java

private String postJson(String url, String data) throws IOException {
    byte[] rawData = data.getBytes("UTF-8");
    HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
    connection.setUseCaches(false);/*from w  w  w  .jav  a 2  s.  co  m*/
    connection.setDoOutput(true);
    connection.setDoInput(true);
    connection.setConnectTimeout(15000);
    connection.setReadTimeout(15000);
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type", "application/json; charset=utf-8");
    connection.setRequestProperty("Content-Length", rawData.length + "");
    connection.setRequestProperty("Content-Language", "en-US");

    DataOutputStream writer = new DataOutputStream(connection.getOutputStream());
    writer.write(rawData);
    writer.flush();
    writer.close();

    InputStream stream = null;
    String returnable = null;
    try {
        stream = connection.getInputStream();
        returnable = IOUtils.toString(stream, Charsets.UTF_8);
    } catch (IOException e) {
        stream = connection.getErrorStream();

        if (stream == null) {
            throw e;
        }
    } finally {
        try {
            if (stream != null)
                stream.close();
        } catch (IOException e) {
        }
    }

    return returnable;
}

From source file:net.mohatu.bloocoin.miner.RegCustom.java

private void register() {
    try {//from   w  w  w  .  j  a va  2 s . co  m
        String result = new String();
        Socket sock = new Socket(this.url, this.port);
        String command = "{\"cmd\":\"register" + "\",\"addr\":\"" + addr + "\",\"pwd\":\"" + key + "\"}";
        DataInputStream is = new DataInputStream(sock.getInputStream());
        DataOutputStream os = new DataOutputStream(sock.getOutputStream());
        os.write(command.getBytes());
        os.flush();

        BufferedReader in = new BufferedReader(new InputStreamReader(is));
        String inputLine;
        while ((inputLine = in.readLine()) != null) {
            result += inputLine;
        }

        is.close();
        os.close();
        sock.close();
        System.out.println(result);
        if (result.contains("\"success\": true")) {
            System.out.println("Registration successful: " + addr);
            saveBloostamp();
        } else if (result.contains("\"success\": false")) {
            System.out.println("Result: Failed");
            JOptionPane.showMessageDialog(Main.scrollPane,
                    "Registration failed.\nCheck your network connection", "Registration Failed",
                    JOptionPane.ERROR_MESSAGE);
            System.exit(0);
        }
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:org.apache.hadoop.streaming.TestUnconsumedInput.java

protected void createInput() throws IOException {
    DataOutputStream out = new DataOutputStream(new FileOutputStream(INPUT_FILE.getAbsoluteFile()));
    for (int i = 0; i < 10000; ++i) {
        out.write(input.getBytes("UTF-8"));
    }//from   w  ww .  j  ava2s .c  om
    out.close();
}

From source file:org.apache.maven.plugin.eclipse.TempEclipseWorkspace.java

/**
 * Given the relative path from the workspace to the project to link use the basename as the project name and link
 * this project to the fully qualified path anchored at workspaceLocation.
 * /* ww  w.j  a va 2 s .  c  o  m*/
 * @param projectToLink The project to link
 * @throws MalformedURLException
 * @throws FileNotFoundException
 * @throws IOException
 */
private void writeLocationFile(String projectToLink) throws IOException {
    File projectToLinkAsRelativeFile = new File(projectToLink);

    File projectWorkspaceDirectory = new File(workspaceLocation, projectToLinkAsRelativeFile.getPath())
            .getCanonicalFile();
    String uriToProjectWorkspaceDirectory = "URI//" + projectWorkspaceDirectory.toURI().toURL().toString();

    File metaDataPlugins = new File(workspaceLocation,
            ReadWorkspaceLocations.METADATA_PLUGINS_ORG_ECLIPSE_CORE_RESOURCES_PROJECTS);
    File projectMetaDataDirectory = new File(metaDataPlugins, projectToLinkAsRelativeFile.getName());
    File locationFile = new File(projectMetaDataDirectory, ReadWorkspaceLocations.BINARY_LOCATION_FILE);

    DataOutputStream dataOutputStream = new DataOutputStream(new FileOutputStream(locationFile));

    dataOutputStream.write(ILocalStoreConstants.BEGIN_CHUNK);
    dataOutputStream.writeUTF(uriToProjectWorkspaceDirectory);
    dataOutputStream.write(ILocalStoreConstants.END_CHUNK);
    IOUtil.close(dataOutputStream);
}