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(byte b[], int off, int len) throws IOException 

Source Link

Document

Writes len bytes from the specified byte array starting at offset off to the underlying output stream.

Usage

From source file:org.apache.hadoop.tools.TestIntegrationByChunk.java

private static void touchFile(String path, long totalFileSize, boolean preserveBlockSize,
        Options.ChecksumOpt checksumOpt, long seed) throws IOException {
    FileSystem fs;//  w  w w .ja  v  a2  s.c  o  m
    DataOutputStream outputStream = null;
    try {
        fs = cluster.getFileSystem();
        final Path qualifiedPath = new Path(path).makeQualified(fs.getUri(), fs.getWorkingDirectory());
        final long blockSize = preserveBlockSize ? NON_DEFAULT_BLOCK_SIZE
                : fs.getDefaultBlockSize(qualifiedPath) * 2;
        FsPermission permission = FsPermission.getFileDefault().applyUMask(FsPermission.getUMask(fs.getConf()));
        outputStream = fs.create(qualifiedPath, permission, EnumSet.of(CreateFlag.CREATE, CreateFlag.OVERWRITE),
                0, (short) (fs.getDefaultReplication(qualifiedPath) * 2), blockSize, null, checksumOpt);
        try {

            if (totalFileSize > 0) {
                int bufferLen = DEFAULT_BUFFER_SIZE;
                byte[] toWrite = new byte[bufferLen];
                Random rb = new Random(seed);
                long bytesToWrite = totalFileSize;
                while (bytesToWrite > 0) {
                    rb.nextBytes(toWrite);
                    int bytesToWriteNext = (bufferLen < bytesToWrite) ? bufferLen : (int) bytesToWrite;

                    outputStream.write(toWrite, 0, bytesToWriteNext);
                    bytesToWrite -= bytesToWriteNext;
                }
            }
        } finally {
            if (outputStream != null) {
                outputStream.close();
            }
        }

        FileStatus fileStatus = fs.getFileStatus(qualifiedPath);
        System.out.println(fileStatus.getBlockSize());
        System.out.println(fileStatus.getReplication());
    } finally {
        IOUtils.cleanup(null, outputStream);
    }
}

From source file:cn.ac.ncic.mastiff.io.coding.DeltaBinaryPackingStringReader.java

@Override
public byte[] ensureDecompressed() throws IOException {
    System.out.println("280    inBuf.length   " + inBuf.getLength());
    FlexibleEncoding.Parquet.DeltaByteArrayReader reader = new FlexibleEncoding.Parquet.DeltaByteArrayReader();
    DataOutputBuffer transfer = new DataOutputBuffer();
    transfer.write(inBuf.getData(), 12, inBuf.getLength() - 12);
    byte[] data = transfer.getData();
    System.out.println("286   byte [] data  " + data.length + "  numPairs  " + numPairs);
    inBuf.close();// ww  w  .  j a  v  a  2  s.c o m
    Binary[] bin = new Utils().readData(reader, data, numPairs);
    System.out.println("2998   Binary[] bin   " + bin.length);
    ByteArrayOutputStream bos1 = new ByteArrayOutputStream();
    DataOutputStream dos1 = new DataOutputStream(bos1);
    ByteArrayOutputStream bos2 = new ByteArrayOutputStream();
    DataOutputStream dos2 = new DataOutputStream(bos2);
    //    DataOutputBuffer   decoding = new DataOutputBuffer();
    //    DataOutputBuffer   offset = new DataOutputBuffer();
    dos1.writeInt(decompressedSize);
    dos1.writeInt(numPairs);
    dos1.writeInt(startPos);
    int dataoffset = 12;
    String str;
    for (int i = 0; i < numPairs; i++) {
        str = bin[i].toStringUsingUTF8();
        dos1.writeUTF(str);
        dataoffset = dos1.size();
        dos2.writeInt(dataoffset);
    }
    System.out.println("315  offset.size() " + bos2.size() + "  decoding.szie   " + bos2.toByteArray().length);
    System.out.println("316  dataoffet   " + dataoffset);
    dos1.write(bos2.toByteArray(), 0, bos2.size());
    inBuf.close();
    System.out.println("316   bos1  " + bos1.toByteArray().length + "    " + bos1.size());
    byte[] bytes = bos1.toByteArray();
    dos2.close();
    bos2.close();
    bos1.close();
    dos1.close();
    return bytes;
}

From source file:org.ctuning.openme.openme.java

public static JSONObject remote_access(JSONObject i) throws JSONException {
    /*//w w w .  ja  v  a 2  s . co  m
    Input:  {
      remote_server_url      - remote server URL
      (module_uoa)           - module to run
      (action)               - action to perform
                                  if =='download', prepare entry/file download through Internet
            
      (save_to_file)         - if web_action==download,
                                  save output to this file
            
      (out)                  - if 'json', treat output as json
                                  if 'json_after_text', strip everything before json
                                  if 'txt', output to stdout
            
      ...                    - all other request parameters
            
      //FGG TBD - should add support for proxy
    }
            
    Output: {
      return   - return code = 0 if successful
                                > 0 if error
                                < 0 if warning (rarely used at this moment)
      (error)  - error text, if return > 0
      (stdout) - if out='txt', output there
    }
    */

    // Prepare return object
    JSONObject r = new JSONObject();

    URL u;
    HttpURLConnection c = null;

    // Prepare request
    String x = "";
    String post = "";

    String con = "";

    x = "";
    if (i.has("out"))
        x = (String) i.get("out");
    if (x != null && x != "")
        con = x;

    String url = "";
    if (i.has("remote_server_url")) {
        url = (String) i.get("remote_server_url");
        i.remove("remote_server_url");
    }
    if (url == null || url == "") {
        r.put("return", new Integer(1));
        r.put("error", "'remote_server_url is not defined");
        return r;
    }

    String save_to_file = "";
    if (i.has("save_to_file")) {
        save_to_file = (String) i.get("save_to_file");
        i.remove("save_to_file");
    }

    // Check if data download, not json and convert it to download request
    boolean download = false;

    x = "";
    if (i.has("action")) {
        x = (String) i.get("action");
    }
    if (x == "download" || x == "show") {
        download = true;
        if (post != "")
            post += "&";
        post += "module_uoa=web&action=" + x;

        if (i.has("module_uoa"))
            i.remove("module_uoa");
        if (i.has("out"))
            i.remove("out");
        i.remove("action");
    }

    // Prepare dict to transfer through Internet
    JSONObject ii = new JSONObject();
    ii.put("dict", i);
    JSONObject rx = convert_array_to_uri(ii);
    if ((Integer) rx.get("return") > 0)
        return rx;

    if (post != "")
        post += "&";
    post += "ck_json=" + ((String) rx.get("string"));

    // Prepare URL request
    String s = "";
    try {
        u = new URL(url);
        c = (HttpURLConnection) u.openConnection();

        c.setRequestMethod("POST");
        c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        c.setRequestProperty("Content-Length", Integer.toString(post.getBytes().length));
        c.setUseCaches(false);
        c.setDoInput(true);
        c.setDoOutput(true);

        //Send request
        DataOutputStream dos = new DataOutputStream(c.getOutputStream());
        dos.writeBytes(post);
        dos.flush();
        dos.close();
    } catch (IOException e) {
        if (c != null)
            c.disconnect();
        r.put("return", new Integer(1));
        r.put("error", "Failed sending request to remote server (" + e.getMessage() + ") ...");
        return r;
    }

    r.put("return", new Integer(0));

    // Check if download, not json!
    if (download) {
        String name = "default_download_name.dat";

        x = "";
        if (i.has("filename")) {
            x = ((String) i.get("filename"));
        }
        if (x != null && x != "") {
            File xf = new File(x);
            name = xf.getName();
        }

        if (save_to_file != null && save_to_file != "")
            name = save_to_file;

        //Reading response in binary and at the same time saving to file
        try {
            //Read response
            DataInputStream dis = new DataInputStream(c.getInputStream());
            DataOutputStream dos = new DataOutputStream(new FileOutputStream(name));

            byte[] buf = new byte[16384];

            int len;

            while ((len = dis.read(buf)) != -1)
                dos.write(buf, 0, len);

            dos.close();
            dis.close();
        } catch (Exception e) {
            if (c != null)
                c.disconnect();

            r.put("return", new Integer(1));
            r.put("error",
                    "Failed reading stream from remote server or writing to file (" + e.getMessage() + ") ...");
            return r;
        }
    } else {
        //Reading response in text
        try {
            //Read response
            InputStream is = c.getInputStream();
            BufferedReader f = new BufferedReader(new InputStreamReader(is));
            StringBuffer ss = new StringBuffer();

            while ((x = f.readLine()) != null) {
                ss.append(x);
                ss.append('\r');
            }

            f.close();
            s = ss.toString();
        } catch (Exception e) {
            if (c != null)
                c.disconnect();

            r.put("return", new Integer(1));
            r.put("error", "Failed reading stream from remote server (" + e.getMessage() + ") ...");
            return r;
        }

        if (con == "json_after_text") {
            String json_sep = "*** ### --- CM JSON SEPARATOR --- ### ***";
            int li = s.lastIndexOf(json_sep);
            if (li >= 0) {
                s = s.substring(li + json_sep.length());
                s = s.trim();
            }
        }

        if (con == "json_after_text" || con == "json")
            r = new JSONObject(s);
        else
            r.put("stdout", s);
    }

    if (c != null)
        c.disconnect();

    return r;
}

From source file:com.bjorsond.android.timeline.sync.ServerUploader.java

protected static void uploadFile(String locationFilename, String saveFilename) {
    System.out.println("saving " + locationFilename + "!! ");
    if (!saveFilename.contains("."))
        saveFilename = saveFilename + Utilities.getExtension(locationFilename);

    if (!fileExistsOnServer(saveFilename)) {

        HttpURLConnection connection = null;
        DataOutputStream outputStream = null;

        String pathToOurFile = locationFilename;
        String urlServer = "http://folk.ntnu.no/bjornava/upload/upload.php";
        //         String urlServer = "http://timelinegamified.appspot.com/upload.php";
        String lineEnd = "\r\n";
        String twoHyphens = "--";
        String boundary = "*****";

        int bytesRead, bytesAvailable, bufferSize;
        byte[] buffer;
        int maxBufferSize = 1 * 1024 * 1024 * 1024;

        try {//from  www . jav  a  2 s.c  o  m
            FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile));

            URL url = new URL(urlServer);
            connection = (HttpURLConnection) url.openConnection();

            // Allow Inputs & Outputs
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setUseCaches(false);

            // Enable POST method
            connection.setRequestMethod("POST");

            connection.setRequestProperty("Connection", "Keep-Alive");
            connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);

            outputStream = new DataOutputStream(connection.getOutputStream());
            outputStream.writeBytes(twoHyphens + boundary + lineEnd);
            outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\""
                    + saveFilename + "\"" + lineEnd);
            outputStream.writeBytes(lineEnd);

            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            buffer = new byte[bufferSize];

            // Read file
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);

            while (bytesRead > 0) {
                outputStream.write(buffer, 0, bufferSize);
                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);
            }

            outputStream.writeBytes(lineEnd);
            outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

            // Responses from the server (code and message)
            int serverResponseCode = connection.getResponseCode();
            String serverResponseMessage = connection.getResponseMessage();

            fileInputStream.close();
            outputStream.flush();
            outputStream.close();

            System.out.println("Server response: " + serverResponseCode + " Message: " + serverResponseMessage);
        } catch (Exception ex) {
            //Exception handling
        }
    } else {
        System.out.println("image exists on server");
    }
}

From source file:org.apache.jackrabbit.core.persistence.bundle.util.BundleBinding.java

/**
 * Write a small binary value and return the data.
 *
 * @param out the output stream to write
 * @param blobVal the binary value//from   w w  w  . j  a va  2s.com
 * @param state the property state (for error messages)
 * @param i the index (for error messages)
 * @return the data
 * @throws IOException if the data could not be read
 */
private byte[] writeSmallBinary(DataOutputStream out, InternalValue value, NodePropBundle.PropertyEntry state,
        int i) throws IOException {
    try {
        int size = (int) value.getLength();
        out.writeInt(size);
        byte[] data = new byte[size];
        DataInputStream in = new DataInputStream(value.getStream());
        try {
            in.readFully(data);
        } finally {
            IOUtils.closeQuietly(in);
        }
        out.write(data, 0, data.length);
        return data;
    } catch (Exception e) {
        String msg = "Error while storing blob. id=" + state.getId() + " idx=" + i + " value=" + value;
        log.error(msg, e);
        throw new IOException(msg);
    }
}

From source file:org.apache.fontbox.ttf.TTFSubsetter.java

private long writeTableHeader(DataOutputStream out, String tag, long offset, byte[] bytes) throws IOException {
    long checksum = 0;
    for (int nup = 0, n = bytes.length; nup < n; nup++) {
        checksum += (bytes[nup] & 0xffL) << 24 - nup % 4 * 8;
    }// w  w  w  .  j a va 2  s .co  m
    checksum &= 0xffffffffL;

    byte[] tagbytes = tag.getBytes("US-ASCII");

    out.write(tagbytes, 0, 4);
    out.writeInt((int) checksum);
    out.writeInt((int) offset);
    out.writeInt(bytes.length);

    // account for the checksum twice, once for the header field, once for the content itself
    return toUInt32(tagbytes) + checksum + checksum + offset + bytes.length;
}

From source file:edu.ku.brc.specify.tools.FormDisplayer.java

/**
 * /*  w  w w .  j a v a2s  .  co  m*/
 */
private File checkForTemplateFiles(final String dstDirPath) {
    String templatePath = dstDirPath + File.separator + "schema_template.html";

    File templateFile = new File(templatePath); //$NON-NLS-1$
    if (templateFile.exists()) {
        return templateFile;
    }

    System.out.println(templatePath);
    try {
        File dstDirFile = new File(dstDirPath);
        if (!dstDirFile.exists()) {
            if (!dstDirFile.mkdirs()) {
                JOptionPane.showMessageDialog(null, "Error creating the site directory.");
            }
        }

        String zipFilePath = dstDirPath + File.separator + "site.zip";
        System.out.println("[" + zipFilePath + "]");

        String url = "http://files.specifysoftware.org/site.zip";
        HTTPGetter getter = new HTTPGetter();
        InputStream ins = getter.beginHTTPRequest(url);
        //DataInputStream  dins   = new DataInputStream(ins);
        DataOutputStream dos = new DataOutputStream(new FileOutputStream(zipFilePath));
        byte[] bytes = new byte[4096];

        int totalBytes = 0;
        /*while (dins.available() > 0)
        {
        int len = dins.read(bytes);
        dos.write(bytes, 0, len);
        totalBytes += len;
        System.out.println(len+" / "+totalBytes);
        }*/
        int numBytes = 0;
        do {
            numBytes = ins.read(bytes);
            if (numBytes > 0) {
                dos.write(bytes, 0, numBytes);
                totalBytes += numBytes;
                System.out.println(numBytes);
            }

        } while (numBytes > 0);

        dos.flush();
        dos.close();
        //dins.close();

        System.out.println(totalBytes);

        File zipFile = new File(zipFilePath);
        System.out.println("zipFile: " + zipFile + " exists: " + zipFile.exists());

        List<File> unzippedFiles = ZipFileHelper.getInstance().unzipToFiles(zipFile);
        for (File unzippedFile : unzippedFiles) {
            FileUtils.copyFileToDirectory(unzippedFile, dstDirFile);
        }

        if (templateFile.exists()) {
            return templateFile;
        }

    } catch (Exception ex) {
        ex.printStackTrace();
        JOptionPane.showMessageDialog(null, "You are missing the template that is needed to run this tool.");
    }
    return null;
}

From source file:com.example.will.sendpic.Camera2BasicFragment.java

private String sendFileGetResult(File file, String url, int port, int secTimeout) {
    Socket s = new Socket();
    String res = "Fail";

    try {//from   www. ja  va  2 s  . c  o m
        s.connect(new InetSocketAddress(url, port), secTimeout * 1000);
        DataOutputStream dos = new DataOutputStream(s.getOutputStream());
        FileInputStream fis = new FileInputStream(file);
        byte[] sendBytes = new byte[1024 * 4];
        int len = 0;

        while ((len = fis.read(sendBytes, 0, sendBytes.length)) > 0) {
            dos.write(sendBytes, 0, len);
            dos.flush();
        }
        s.shutdownOutput();

        //get the result from server
        //you can change the protocol
        InputStream in = s.getInputStream();
        byte[] result = new byte[1024];
        int num = in.read(result);

        res = new String(result, 0, num);
        System.out.println(res);

        //closing resources
        s.close();
        dos.close();
        fis.close();
        in.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return res;
}

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

/**
 * Write a value to the output stream using UTF-8 encoding
 * /*from  w  w  w  .  j a  v a  2  s  .co m*/
 * @param value
 *            output value
 * @throws IOException
 */
void write(Object value, DataOutputStream out) throws IOException {
    byte[] bval;
    int valSize;
    if (value instanceof BytesWritable) {
        BytesWritable val = (BytesWritable) value;
        bval = val.getBytes();
        valSize = val.getLength();
    } else if (value instanceof Text) {
        Text val = (Text) value;
        bval = val.getBytes();
        valSize = val.getLength();
    } else {
        String sval = value.toString();
        bval = sval.getBytes("UTF-8");
        valSize = bval.length;
    }
    out.write(bval, 0, valSize);
}

From source file:org.apache.jackrabbit.core.persistence.bundle.util.BundleBinding.java

/**
 * Write a small binary value and return the data.
 *
 * @param out the output stream to write
 * @param size the size//from   w  w  w. jav a2s . c  o m
 * @param blobVal the binary value
 * @param state the property state (for error messages)
 * @param i the index (for error messages)
 * @return the data
 * @throws IOException if the data could not be read
 */
private byte[] writeSmallBinary(DataOutputStream out, BLOBFileValue blobVal, NodePropBundle.PropertyEntry state,
        int i) throws IOException {
    int size = (int) blobVal.getLength();
    out.writeInt(size);
    byte[] data = new byte[size];
    try {
        DataInputStream in = new DataInputStream(blobVal.getStream());
        try {
            in.readFully(data);
        } finally {
            IOUtils.closeQuietly(in);
        }
    } catch (Exception e) {
        String msg = "Error while storing blob. id=" + state.getId() + " idx=" + i + " size=" + size;
        log.error(msg, e);
        throw new IOException(msg);
    }
    out.write(data, 0, data.length);
    return data;
}