List of usage examples for java.io DataOutputStream writeUTF
public final void writeUTF(String str) throws IOException
From source file:org.apache.nifi.distributed.cache.client.DistributedMapCacheClientService.java
@Override public <K, V> boolean putIfAbsent(final K key, final V value, final Serializer<K> keySerializer, final Serializer<V> valueSerializer) throws IOException { return withCommsSession(new CommsAction<Boolean>() { @Override//w ww . j av a2 s .c o m public Boolean execute(final CommsSession session) throws IOException { final DataOutputStream dos = new DataOutputStream(session.getOutputStream()); dos.writeUTF("putIfAbsent"); serialize(key, keySerializer, dos); serialize(value, valueSerializer, dos); dos.flush(); final DataInputStream dis = new DataInputStream(session.getInputStream()); return dis.readBoolean(); } }); }
From source file:org.apache.nifi.distributed.cache.client.DistributedMapCacheClientService.java
@Override public <K, V> V get(final K key, final Serializer<K> keySerializer, final Deserializer<V> valueDeserializer) throws IOException { return withCommsSession(new CommsAction<V>() { @Override//from w ww.j ava2 s . c o m public V execute(final CommsSession session) throws IOException { final DataOutputStream dos = new DataOutputStream(session.getOutputStream()); dos.writeUTF("get"); serialize(key, keySerializer, dos); dos.flush(); // read response final DataInputStream dis = new DataInputStream(session.getInputStream()); final byte[] responseBuffer = readLengthDelimitedResponse(dis); return valueDeserializer.deserialize(responseBuffer); } }); }
From source file:org.apache.nifi.distributed.cache.client.DistributedMapCacheClientService.java
@Override public <K, V> V getAndPutIfAbsent(final K key, final V value, final Serializer<K> keySerializer, final Serializer<V> valueSerializer, final Deserializer<V> valueDeserializer) throws IOException { return withCommsSession(new CommsAction<V>() { @Override/*from w w w . j av a 2 s . c o m*/ public V execute(final CommsSession session) throws IOException { final DataOutputStream dos = new DataOutputStream(session.getOutputStream()); dos.writeUTF("getAndPutIfAbsent"); serialize(key, keySerializer, dos); serialize(value, valueSerializer, dos); dos.flush(); // read response final DataInputStream dis = new DataInputStream(session.getInputStream()); final byte[] responseBuffer = readLengthDelimitedResponse(dis); return valueDeserializer.deserialize(responseBuffer); } }); }
From source file:org.apache.nifi.distributed.cache.client.DistributedMapCacheClientService.java
@Override public <K, V> CacheEntry<K, V> fetch(final K key, final Serializer<K> keySerializer, final Deserializer<V> valueDeserializer) throws IOException { return withCommsSession(session -> { validateProtocolVersion(session, 2); final DataOutputStream dos = new DataOutputStream(session.getOutputStream()); dos.writeUTF("fetch"); serialize(key, keySerializer, dos); dos.flush();//from www . j a v a 2 s .com // read response final DataInputStream dis = new DataInputStream(session.getInputStream()); final long revision = dis.readLong(); final byte[] responseBuffer = readLengthDelimitedResponse(dis); if (revision < 0) { // This indicates that key was not found. return null; } final StandardCacheEntry<K, V> standardCacheEntry = new StandardCacheEntry<>(key, valueDeserializer.deserialize(responseBuffer), revision); return standardCacheEntry; }); }
From source file:org.apache.nifi.distributed.cache.client.DistributedMapCacheClientService.java
@Override public <K, V> void put(final K key, final V value, final Serializer<K> keySerializer, final Serializer<V> valueSerializer) throws IOException { withCommsSession(new CommsAction<Object>() { @Override//ww w. j a va 2 s. c o m public Object execute(final CommsSession session) throws IOException { final DataOutputStream dos = new DataOutputStream(session.getOutputStream()); dos.writeUTF("put"); serialize(key, keySerializer, dos); serialize(value, valueSerializer, dos); dos.flush(); final DataInputStream dis = new DataInputStream(session.getInputStream()); final boolean success = dis.readBoolean(); if (!success) { throw new IOException( "Expected to receive confirmation of 'put' request but received unexpected response"); } return null; } }); }
From source file:de.burlov.ultracipher.core.Database.java
public String computeChecksum() { DigestOutputStream digest = new DigestOutputStream(new RIPEMD160Digest()); DataOutputStream out = new DataOutputStream(digest); try {// w w w . j ava 2 s . c om for (Entry<String, Long> entry : deletedEntries.entrySet()) { out.writeUTF(entry.getKey()); out.writeLong(entry.getValue()); } for (DataEntry entry : entries.values()) { out.writeUTF(entry.getId()); out.writeUTF(entry.getName()); out.writeUTF(entry.getTags()); out.writeUTF(entry.getText()); } out.close(); } catch (IOException e) { throw new RuntimeException(e); } return new String(Hex.encode(digest.getDigest())); }
From source file:RMSGameScores.java
/** * Add a new score to the storage./* ww w . ja va 2s . c o m*/ * * @param score * the score to store. * @param playerName * the name of the play achieving this score. */ public void addScore(int score, String playerName) { // Each score is stored in a separate record, formatted with // the score, followed by the player name. ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream outputStream = new DataOutputStream(baos); try { // Push the score into a byte array. outputStream.writeInt(score); // Then push the player name. outputStream.writeUTF(playerName); } catch (IOException ioe) { System.out.println(ioe); ioe.printStackTrace(); } // Extract the byte array byte[] b = baos.toByteArray(); try { // Add it to the record store recordStore.addRecord(b, 0, b.length); } catch (RecordStoreException rse) { System.out.println(rse); rse.printStackTrace(); } }
From source file:bankingclient.DNFrame.java
public DNFrame(MainFrame vmain) { initComponents();//from www. j ava 2 s .c o m this.jTextField1.setText(""); this.jTextField2.setText(""); this.jTextField_cmt.setText(""); this.jTextField_sdt.setText(""); this.main = vmain; noAcc = new NewOrOldAccFrame(this); this.setVisible(false); jL_sdt.setVisible(false); jL_cmtnd.setVisible(false); jTextField_cmt.setVisible(false); jTextField_sdt.setVisible(false); jBt_dn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (!jCheck_qmk.isSelected()) { try { Socket client = new Socket("113.22.46.207", 6013); DataOutputStream dout = new DataOutputStream(client.getOutputStream()); dout.writeByte(2); dout.writeUTF(jTextField1.getText() + "\n" + jTextField2.getText()); dout.flush(); while (true) { break; } DataInputStream din = new DataInputStream(client.getInputStream()); byte check = din.readByte(); if (check == 1) { noAcc.setVisible(true); DNFrame.this.setVisible(false); noAcc.setMainCustomer(jTextField1.getText()); } else { JOptionPane.showMessageDialog(new JFrame(), "Tn ?ang Nhp Khng Tn Ti, hoac mat khau sai"); } } catch (Exception ex) { ex.printStackTrace(); JOptionPane.showMessageDialog(rootPane, "C Li Kt Ni Mng...."); } } else if ((!jTextField_cmt.getText().equals("")) && (!jTextField_sdt.getText().equals("")) && (NumberUtils.isNumber(jTextField_cmt.getText())) && (NumberUtils.isNumber(jTextField_sdt.getText()))) { try { Socket client = new Socket("113.22.46.207", 6013); DataOutputStream dout = new DataOutputStream(client.getOutputStream()); dout.writeByte(9); dout.writeUTF(jTextField1.getText() + "\n" + jTextField_sdt.getText() + "\n" + jTextField_cmt.getText()); dout.flush(); DataInputStream din = new DataInputStream(client.getInputStream()); byte check = din.readByte(); if (check == 1) { noAcc.setVisible(true); DNFrame.this.setVisible(false); noAcc.setMainCustomer(jTextField1.getText()); } else { JOptionPane.showMessageDialog(new JFrame(), "Khong dang nhap duoc, thong tin sai"); } } catch (Exception ex) { ex.printStackTrace(); } } else { JOptionPane.showMessageDialog(new JFrame(), "Can dien day du thong tin va dung mau"); } } }); jBt_ql.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { main.setVisible(true); DNFrame.this.setVisible(false); } }); jCheck_qmk.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (jCheck_qmk.isSelected()) { jL_sdt.setVisible(true); jL_cmtnd.setVisible(true); jTextField_cmt.setVisible(true); jTextField_sdt.setVisible(true); } else { jL_sdt.setVisible(false); jL_cmtnd.setVisible(false); jTextField_cmt.setVisible(false); jTextField_sdt.setVisible(false); } } }); }
From source file:org.outerrim.snippad.ui.swt.dnd.WikiTransfer.java
/** * Writes the given WikiWord to the output stream. * * @param word//from w w w. j ava 2 s . c o m * The word to write * @param dataOut * The stream to write to * @throws IOException */ private void writeWikiWord(final WikiWord word, final DataOutputStream dataOut) throws IOException { /* * Serialization format is as follows: (String) name of the word * (String) wiki text (int) number of child words (WikiWord) child 1 ... * repeat for each child */ dataOut.writeUTF(word.getName()); dataOut.writeUTF(word.getWikiText()); List children = word.getWikiWords(); LOG.debug("Word has " + children.size() + " children"); dataOut.writeInt(children.size()); for (int i = 0, size = children.size(); i < size; ++i) { writeWikiWord((WikiWord) children.get(i), dataOut); } }
From source file:com.exzogeni.dk.http.cache.DiscCacheStore.java
private void saveMetaFile(@NonNull File metaFile, @NonNull Map<String, List<String>> metaHeaders, long maxAge) throws IOException { final AtomicFile af = new AtomicFile(metaFile); final FileOutputStream fos = af.startWrite(); try {//from ww w. j a va 2s.c om final DataOutputStream dat = new DataOutputStream(new BufferPoolOutputStream(fos)); dat.writeLong(System.currentTimeMillis() + maxAge); dat.writeInt(metaHeaders.size()); for (final Map.Entry<String, List<String>> header : metaHeaders.entrySet()) { dat.writeUTF(header.getKey()); dat.writeInt(header.getValue().size()); for (final String value : header.getValue()) { dat.writeUTF(value); } } IOUtils.closeQuietly(dat); af.finishWrite(fos); } catch (IOException e) { af.failWrite(fos); af.delete(); throw e; } }