List of usage examples for java.io DataOutputStream writeInt
public final void writeInt(int v) throws IOException
int
to the underlying output stream as four bytes, high byte first. From source file:org.prorefactor.refactor.PUB.java
private void writeFileIndex(DataOutputStream out) throws IOException { String[] files = tree.getFilenames(); for (int i = 0; i < files.length; i++) { out.writeInt(i); out.writeUTF(files[i]);/*from ww w . j a v a 2 s .c o m*/ } out.writeInt(-1); out.writeUTF(""); }
From source file:org.prorefactor.refactor.PUB.java
private void writeTree(DataOutputStream out, JPNode node) throws IOException { int nodeType = node.getType(); out.writeInt(node.getSubtypeIndex()); out.writeInt(nodeType);// ww w. java 2s. c om out.writeShort(node.getFileIndex()); out.writeInt(node.getLine()); out.writeShort(node.getColumn()); out.writeInt(node.getSourceNum()); if (!TokenTypes.hasDefaultText(nodeType)) { out.writeInt(NODETEXT); out.writeInt(stringIndex(node.getText())); } String comments = node.getComments(); if (comments != null) { out.writeInt(NODECOMMENTS); out.writeInt(stringIndex(comments)); } if (node.attrGet(IConstants.STATEHEAD) == IConstants.TRUE) { out.writeInt(IConstants.STATEHEAD); out.writeInt(IConstants.TRUE); out.writeInt(IConstants.STATE2); out.writeInt(node.getState2()); } int attrVal; if ((attrVal = node.attrGet(IConstants.STORETYPE)) > 0) { out.writeInt(IConstants.STORETYPE); out.writeInt(attrVal); } if (node instanceof ProparseDirectiveNode) { out.writeInt(IConstants.PROPARSEDIRECTIVE); out.writeInt(stringIndex(((ProparseDirectiveNode) node).getDirectiveText())); } if ((attrVal = node.attrGet(IConstants.OPERATOR)) > 0) { out.writeInt(IConstants.OPERATOR); out.writeInt(attrVal); } if ((attrVal = node.attrGet(IConstants.INLINE_VAR_DEF)) > 0) { out.writeInt(IConstants.INLINE_VAR_DEF); out.writeInt(attrVal); } if (nodeType == TokenTypes.TYPE_NAME) { out.writeInt(IConstants.QUALIFIED_CLASS_INT); out.writeInt(stringIndex(node.attrGetS(IConstants.QUALIFIED_CLASS_STRING))); } out.writeInt(-1); out.writeInt(-1); // Terminate the attribute key/value pairs. JPNode next; if ((next = node.firstChild()) != null) writeTree(out, next); else out.writeInt(-1); if ((next = node.nextSibling()) != null) writeTree(out, next); else out.writeInt(-1); }
From source file:org.mrgeo.vector.mrsvector.VectorTile.java
private static void writeBlob(final OutputStream stream, final Message msg, final String type) throws IOException { final BlobHeader.Builder headerbuilder = BlobHeader.newBuilder(); final Blob.Builder blobbuilder = Blob.newBuilder(); final ByteArrayOutputStream baos = new ByteArrayOutputStream(); // uncomment to write uncompressed (and comment out zip section to before the builder.build()) msg.writeTo(baos);//from w w w . j av a 2s .c om // add the uncompressed data to the blob blobbuilder.setRaw(ByteString.copyFrom(baos.toByteArray())); // // set up the zip stream // final DeflaterOutputStream zip = new DeflaterOutputStream(baos, new // Deflater(Deflater.BEST_SPEED, false)); // // write to the zipper // msg.writeTo(zip); // // // make sure we've finished processing // zip.close(); // // // set the raw size of the data // blobbuilder.setRawSize(msg.getSerializedSize()); // // // add the zipped data to the blob // blobbuilder.setZlibData(ByteString.copyFrom(baos.toByteArray())); final Blob blob = blobbuilder.build(); // set the size headerbuilder.setDatasize(blob.getSerializedSize()); headerbuilder.setType(type); final BlobHeader bh = headerbuilder.build(); // need to write length of the header... final DataOutputStream daos = new DataOutputStream(stream); daos.writeInt(bh.getSerializedSize()); // write the header bh.writeTo(stream); headerbuilder.clear(); // now write the data blob blob.writeTo(stream); blobbuilder.clear(); }
From source file:ch.unil.genescore.vegas.Snp.java
public void writePosAndAllele(DataOutputStream os) throws IOException { // NOTE: ALSO CHANGE readBinary() IF YOU CHANGE THIS os.writeUTF(id_);//from w w w .j av a 2 s . c o m os.writeUTF(chr_); os.writeInt(start_); os.writeInt(end_); os.writeBoolean(posStrand_); os.writeChar(minorAllele_); }
From source file:darks.learning.word2vec.Word2Vec.java
/** * Save model//www. ja v a2 s. co m * * @param file Model file saved */ public void saveModel(File file) { log.info("Saving word2vec model to " + file); DataOutputStream dos = null; try { dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file))); dos.writeInt(wordNodes.size()); dos.writeInt(config.featureSize); double[] syn0 = null; for (Entry<String, WordNode> element : wordNodes.entrySet()) { byte[] bytes = element.getKey().getBytes(); dos.writeInt(bytes.length); dos.write(bytes); syn0 = (element.getValue()).feature.toArray(); for (int i = 0; i < config.featureSize; i++) { dos.writeDouble(syn0[i]); } } } catch (IOException e) { log.error(e.getMessage(), e); } finally { IOUtils.closeStream(dos); } }
From source file:org.apache.hadoop.hbase.io.hfile.TestHFileBlockCompatibility.java
@Test public void testReaderV2() throws IOException { if (includesTag) { TEST_UTIL.getConfiguration().setInt("hfile.format.version", 3); }/*from ww w .j a v a2 s .c o m*/ for (Compression.Algorithm algo : COMPRESSION_ALGORITHMS) { for (boolean pread : new boolean[] { false, true }) { LOG.info("testReaderV2: Compression algorithm: " + algo + ", pread=" + pread); Path path = new Path(TEST_UTIL.getDataTestDir(), "blocks_v2_" + algo); FSDataOutputStream os = fs.create(path); Writer hbw = new Writer(algo, null, includesMemstoreTS, includesTag); long totalSize = 0; for (int blockId = 0; blockId < 2; ++blockId) { DataOutputStream dos = hbw.startWriting(BlockType.DATA); for (int i = 0; i < 1234; ++i) dos.writeInt(i); hbw.writeHeaderAndData(os); totalSize += hbw.getOnDiskSizeWithHeader(); } os.close(); FSDataInputStream is = fs.open(path); HFileContext meta = new HFileContextBuilder().withHBaseCheckSum(false) .withIncludesMvcc(includesMemstoreTS).withIncludesTags(includesTag).withCompression(algo) .build(); HFileBlock.FSReader hbr = new HFileBlock.FSReaderV2(new FSDataInputStreamWrapper(is), totalSize, fs, path, meta); HFileBlock b = hbr.readBlockData(0, -1, -1, pread); is.close(); b.sanityCheck(); assertEquals(4936, b.getUncompressedSizeWithoutHeader()); assertEquals(algo == GZ ? 2173 : 4936, b.getOnDiskSizeWithoutHeader() - b.totalChecksumBytes()); String blockStr = b.toString(); if (algo == GZ) { is = fs.open(path); hbr = new HFileBlock.FSReaderV2(new FSDataInputStreamWrapper(is), totalSize, fs, path, meta); b = hbr.readBlockData(0, 2173 + HConstants.HFILEBLOCK_HEADER_SIZE_NO_CHECKSUM + b.totalChecksumBytes(), -1, pread); assertEquals(blockStr, b.toString()); int wrongCompressedSize = 2172; try { b = hbr.readBlockData(0, wrongCompressedSize + HConstants.HFILEBLOCK_HEADER_SIZE_NO_CHECKSUM, -1, pread); fail("Exception expected"); } catch (IOException ex) { String expectedPrefix = "On-disk size without header provided is " + wrongCompressedSize + ", but block header contains " + b.getOnDiskSizeWithoutHeader() + "."; assertTrue( "Invalid exception message: '" + ex.getMessage() + "'.\nMessage is expected to start with: '" + expectedPrefix + "'", ex.getMessage().startsWith(expectedPrefix)); } is.close(); } } } }
From source file:runtime.starter.MPJRun.java
private Integer[] getNextAvialablePorts(String machineName) { Integer[] ports = new Integer[2]; Socket portClient = null;/*w w w . j a v a2s . co m*/ try { portClient = new Socket(machineName, portManagerPort); OutputStream outToServer = portClient.getOutputStream(); DataOutputStream out = new DataOutputStream(outToServer); out.writeInt(1); out.flush(); DataInputStream din = new DataInputStream(portClient.getInputStream()); ports[0] = din.readInt(); ports[1] = din.readInt(); out.writeInt(2); out.flush(); } catch (IOException e) { System.out.println("Cannot connect to the daemon " + "at machine <" + machineName + "> and port <" + portManagerPort + ">." + "Please make sure that the machine is reachable " + "and portmanager is running"); } finally { try { if (!portClient.isClosed()) portClient.close(); } catch (IOException e) { e.printStackTrace(); } } return ports; }
From source file:net.sf.gazpachoquest.rest.auth.TokenStore.java
/** * Stores the current set of tokens to the token file *///from w w w. ja v a 2 s .com private void saveTokens() { FileOutputStream fout = null; DataOutputStream keyOutputStream = null; try { File parent = tokenFile.getAbsoluteFile().getParentFile(); log.info("Token File {} parent {} ", tokenFile, parent); if (!parent.exists()) { parent.mkdirs(); } fout = new FileOutputStream(tmpTokenFile); keyOutputStream = new DataOutputStream(fout); keyOutputStream.writeInt(currentToken); keyOutputStream.writeLong(nextUpdate); for (int i = 0; i < currentTokens.length; i++) { if (currentTokens[i] == null) { keyOutputStream.writeInt(0); } else { keyOutputStream.writeInt(1); byte[] b = currentTokens[i].getEncoded(); keyOutputStream.writeInt(b.length); keyOutputStream.write(b); } } keyOutputStream.close(); tmpTokenFile.renameTo(tokenFile); } catch (IOException e) { log.error("Failed to save cookie keys " + e.getMessage()); } finally { try { keyOutputStream.close(); } catch (Exception e) { } try { fout.close(); } catch (Exception e) { } } }
From source file:com.trigger_context.Main_Service.java
private void takeAction(String mac, InetAddress ip) { noti("comes to ", mac); SharedPreferences conditions = getSharedPreferences(mac, MODE_PRIVATE); Map<String, ?> cond_map = conditions.getAll(); Set<String> key_set = cond_map.keySet(); Main_Service.main_Service.noti(cond_map.toString(), ""); if (key_set.contains("SmsAction")) { String number = conditions.getString("SmsActionNumber", null); String message = conditions.getString("SmsActionMessage", null); try {//from w ww. ja v a2s .c o m SmsManager smsManager = SmsManager.getDefault(); smsManager.sendTextMessage(number, null, message, null, null); noti("Sms Sent To : ", "" + number); } catch (Exception e) { noti("Sms Sending To ", number + "Failed"); } } if (key_set.contains("OpenWebsiteAction")) { Intent dialogIntent = new Intent(getBaseContext(), Action_Open_Url.class); dialogIntent.putExtra("urlAction", (String) cond_map.get("urlAction")); dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); getApplication().startActivity(dialogIntent); } if (key_set.contains("ToggleAction")) { if (key_set.contains("bluetoothAction") && conditions.getBoolean("bluetoothAction", false)) { final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); bluetoothAdapter.enable(); } else if (key_set.contains("bluetoothAction")) { final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); bluetoothAdapter.disable(); } if (key_set.contains("wifiAction") && conditions.getBoolean("wifiAction", false)) { final WifiManager wm = (WifiManager) getSystemService(Context.WIFI_SERVICE); wm.setWifiEnabled(true); } else if (key_set.contains("wifiAction")) { final WifiManager wm = (WifiManager) getSystemService(Context.WIFI_SERVICE); wm.setWifiEnabled(false); } } if (key_set.contains("TweetAction")) { Intent dialogIntent = new Intent(getBaseContext(), Action_Post_Tweet.class); dialogIntent.putExtra("tweetTextAction", (String) cond_map.get("tweetTextAction")); dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); getApplication().startActivity(dialogIntent); } if (key_set.contains("EmailAction")) { Intent dialogIntent = new Intent(getBaseContext(), Action_Email_Client.class); dialogIntent.putExtra("toAction", (String) cond_map.get("toAction")); dialogIntent.putExtra("subjectAction", (String) cond_map.get("subjectAction")); dialogIntent.putExtra("emailMessageAction", (String) cond_map.get("emailMessageAction")); dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); getApplication().startActivity(dialogIntent); } if (key_set.contains("RemoteServerCmd")) { String text = conditions.getString("cmd", null); new Thread(new SendData("224.0.0.1", 9876, text)).start(); } // network activities from here. // in all network actions send username first if (key_set.contains("FileTransferAction")) { String path = conditions.getString("filePath", null); SharedPreferences my_data = getSharedPreferences(Main_Service.MY_DATA, MODE_PRIVATE); String usrName = my_data.getString("name", "userName"); // type 1 try { Socket socket = new Socket(ip, COMM_PORT); DataOutputStream out = null; out = new DataOutputStream(socket.getOutputStream()); out.writeUTF(usrName); out.writeInt(1); sendFile(out, path); noti("Sent " + path + " file to :", getSharedPreferences(Main_Service.USERS, MODE_PRIVATE).getString("name", "userName")); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (key_set.contains("ccfMsgAction")) { String msg = conditions.getString("ccfMsg", null); SharedPreferences my_data = getSharedPreferences(Main_Service.MY_DATA, MODE_PRIVATE); String usrName = my_data.getString("name", "userName"); // type 2 is msg try { Socket socket = new Socket(ip, COMM_PORT); DataOutputStream out = null; out = new DataOutputStream(socket.getOutputStream()); out.writeUTF(usrName); out.writeInt(2); out.writeUTF(msg); noti("Sent msg : '" + msg + "'", "to : " + getSharedPreferences(Main_Service.USERS, MODE_PRIVATE).getString("name", "userName")); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (key_set.contains("sync")) { SharedPreferences my_data = getSharedPreferences(Main_Service.MY_DATA, MODE_PRIVATE); String usrName = my_data.getString("name", "userName"); // type 3 is sync try { Socket socket = new Socket(ip, COMM_PORT); DataInputStream in = new DataInputStream(socket.getInputStream()); DataOutputStream out = new DataOutputStream(socket.getOutputStream()); out.writeUTF(usrName); out.writeInt(3); senderSync(in, out, Environment.getExternalStorageDirectory().getPath() + "/TriggerSync/"); noti("Synced with:", getSharedPreferences(Main_Service.USERS, MODE_PRIVATE).getString("name", "userName")); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
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 a 2 s. co 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; }