protected final void send(Op op, String text) throws IOException { ByteArrayOutputStream buf = new ByteArrayOutputStream(); new DataOutputStream(buf).writeUTF(text); send(op, buf.toByteArray()); }
static void roll() { try { Socket socket = new Socket(host, port); DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); DataInputStream dis = new DataInputStream(socket.getInputStream()); dos.writeUTF(ExternallyRolledFileAppender.ROLL_OVER); String rc = dis.readUTF(); if(ExternallyRolledFileAppender.OK.equals(rc)) { cat.info("Roll over signal acknowledged by remote appender."); } else { cat.warn("Unexpected return code "+rc+" from remote entity."); System.exit(2); } } catch(IOException e) { cat.error("Could not send roll signal on host "+host+" port "+port+" .", e); System.exit(2); } System.exit(0); } }
/** * Write the data header * * @param stream the output stream * @param header the header to write * @throws IOException */ private void writeHeader(OutputStream stream, Header header) throws IOException { DataOutputStream dos = new DataOutputStream(stream); dos.writeUTF(HEADER); // Write the current version dos.writeInt(1); // Write the normalizer opType dos.writeUTF(header.normalizerType.toString()); // If the header contains a custom class opName, write that too if (header.customStrategyClass != null) { dos.writeUTF(header.customStrategyClass.getName()); } }
Socket socket = ...; // Create and connect the socket DataOutputStream dOut = new DataOutputStream(socket.getOutputStream()); // Send first message dOut.writeByte(1); dOut.writeUTF("This is the first type of message."); dOut.flush(); // Send off the data // Send the second message dOut.writeByte(2); dOut.writeUTF("This is the second type of message."); dOut.flush(); // Send off the data // Send the third message dOut.writeByte(3); dOut.writeUTF("This is the third type of message (Part 1)."); dOut.writeUTF("This is the third type of message (Part 2)."); dOut.flush(); // Send off the data // Send the exit message dOut.writeByte(-1); dOut.flush(); dOut.close();
private static void writeLdc(LdcInsnNode lin, DataOutputStream dos) throws IOException { Object o = lin.cst; if (o instanceof Integer) { dos.writeShort(1); dos.writeInt((Integer)o); } else if (o instanceof Float) { dos.writeShort(2); dos.writeFloat((Float)o); } else if (o instanceof Long) { dos.writeShort(3); dos.writeLong((Long)o); } else if (o instanceof Double) { dos.writeShort(4); dos.writeDouble((Double)o); } else if (o instanceof String) { dos.writeShort(5); dos.writeUTF((String)o); } else if (o instanceof Type) { dos.writeShort(6); dos.writeUTF(((Type)o).getDescriptor()); } else { dos.writeShort(0); } }
public boolean connect(Socket socket) throws IOException { try { LOGGER.log(Level.FINE, "Requesting ping from {0}", socket.getRemoteSocketAddress()); try (DataOutputStream out = new DataOutputStream(socket.getOutputStream())) { out.writeUTF("Protocol:Ping"); try (InputStream in = socket.getInputStream()) { byte[] response = new byte[ping.length]; int responseLength = in.read(response); if (responseLength == ping.length && Arrays.equals(response, ping)) { LOGGER.log(Level.FINE, "Received ping response from {0}", socket.getRemoteSocketAddress()); return true; } else { LOGGER.log(Level.FINE, "Expected ping response from {0} of {1} got {2}", new Object[]{ socket.getRemoteSocketAddress(), new String(ping, "UTF-8"), responseLength > 0 && responseLength <= response.length ? new String(response, 0, responseLength, "UTF-8") : "bad response length " + responseLength }); return false; } } } } finally { socket.close(); } } }
public GFSnapshotExporter(File out, String region, InternalCache cache) throws IOException { this.cache = cache; FileOutputStream fos = new FileOutputStream(out); fc = fos.getChannel(); dos = new DataOutputStream(new BufferedOutputStream(fos)); // write snapshot version dos.writeByte(SNAP_VER_2); // write format type dos.write(SNAP_FMT); // write temporary pdx location in bytes 4-11 dos.writeLong(-1); // write region name dos.writeUTF(region); }
/** Serializes {@code action} type and data into the {@code output}. */ public static void serializeToStream(DownloadAction action, OutputStream output) throws IOException { // Don't close the stream as it closes the underlying stream too. DataOutputStream dataOutputStream = new DataOutputStream(output); dataOutputStream.writeUTF(action.type); dataOutputStream.writeInt(action.version); action.writeToStream(dataOutputStream); dataOutputStream.flush(); }
static boolean saveDat(String path, TreeMap<String, String> map) { Collection<String> dependencyList = map.values(); // 缓存值文件 try { DataOutputStream out = new DataOutputStream(IOUtil.newOutputStream(path + ".bi" + Predefine.BIN_EXT)); out.writeInt(dependencyList.size()); for (String dependency : dependencyList) { out.writeUTF(dependency); } if (!trie.save(out)) return false; out.close(); } catch (Exception e) { logger.warning("保存失败" + e); return false; } return true; }
public static void serialize(Dictionary<?> dict, OutputStream outputStream) { try { DataOutputStream out = new DataOutputStream(outputStream); out.writeUTF(dict.getClass().getName()); dict.write(out); out.flush(); } catch (IOException e) { throw new RuntimeException(e); } }
public void write(DataOutputStream out) throws IOException { if (value instanceof String) { out.writeByte(CONSTANT_UTF8); out.writeUTF((String) value); } else if (value instanceof Integer) { out.writeByte(CONSTANT_INTEGER); out.writeInt(((Integer) value).intValue()); } else if (value instanceof Float) { out.writeByte(CONSTANT_FLOAT); out.writeFloat(((Float) value).floatValue()); } else if (value instanceof Long) { out.writeByte(CONSTANT_LONG); out.writeLong(((Long) value).longValue()); } else if (value instanceof Double) { out.writeDouble(CONSTANT_DOUBLE); out.writeDouble(((Double) value).doubleValue()); } else { throw new InternalError("bogus value entry: " + value); } } }
private void readWriteTest(Dictionary<String> dict) throws Exception { final String path = "src/test/resources/dict/tmp_dict"; File f = new File(path); f.deleteOnExit(); f.createNewFile(); String dictClassName = dict.getClass().getName(); DataOutputStream out = new DataOutputStream(new FileOutputStream(f)); out.writeUTF(dictClassName); dict.write(out); out.close(); //read dict DataInputStream in = null; Dictionary<String> dict2 = null; try { File f2 = new File(path); in = new DataInputStream(new FileInputStream(f2)); String dictClassName2 = in.readUTF(); dict2 = (Dictionary<String>) ClassUtil.newInstance(dictClassName2); dict2.readFields(in); } finally { if (in != null) { in.close(); } } assertTrue(dict.equals(dict2)); } }
@Override protected void writeToStream(DataOutputStream output) throws IOException { output.writeUTF(uri.toString()); output.writeBoolean(isRemoveAction); output.writeInt(data.length); output.write(data); boolean customCacheKeySet = customCacheKey != null; output.writeBoolean(customCacheKeySet); if (customCacheKeySet) { output.writeUTF(customCacheKey); } }
@Override public void run() throws IOException, InterruptedException { try { DataOutputStream out = new DataOutputStream(socket.getOutputStream()); out.writeUTF("Welcome"); // perform coin-toss and come up with a session key to encrypt data Connection c = new Connection(socket); byte[] secret = c.diffieHellman(true).generateSecret(); SecretKey sessionKey = new SecretKeySpec(Connection.fold(secret,128/8),"AES"); c = c.encryptConnection(sessionKey,"AES/CFB8/NoPadding"); try { // HACK: TODO: move the transport support into modules Class<?> cls = Jenkins.getActiveInstance().pluginManager.uberClassLoader.loadClass("org.jenkinsci.main.modules.instance_identity.InstanceIdentity"); Object iid = cls.getDeclaredMethod("get").invoke(null); PrivateKey instanceId = (PrivateKey)cls.getDeclaredMethod("getPrivate").invoke(iid); // send a signature to prove our identity Signature signer = Signature.getInstance("SHA1withRSA"); signer.initSign(instanceId); signer.update(secret); c.writeByteArray(signer.sign()); } catch (ClassNotFoundException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { throw new Error(e); } runCli(c); } catch (GeneralSecurityException e) { throw new IOException("Failed to encrypt the CLI channel",e); } } }
private static byte[] getSerializedBytes(Class<?> clazz) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (DataOutputStream data = new DataOutputStream(baos)) { data.writeShort(ObjectStreamConstants.STREAM_MAGIC); data.writeShort(ObjectStreamConstants.STREAM_VERSION); data.writeByte(ObjectStreamConstants.TC_OBJECT); data.writeByte(ObjectStreamConstants.TC_CLASSDESC); data.writeUTF(clazz.getName()); Long suid = getSerializableUID(clazz); data.writeLong(suid); data.writeByte(2); // classDescFlags (2 = Serializable) data.writeShort(0); // field count data.writeByte(ObjectStreamConstants.TC_ENDBLOCKDATA); data.writeByte(ObjectStreamConstants.TC_NULL); } return baos.toByteArray(); }
/** * Dump the CARTs in the cart map to destinationDir/CARTS.bin * * @param cart * tree * @param destFile * the destination file * @throws IOException * IOException */ public void dumpWagonCART(CART cart, String destFile) throws IOException { System.out.println("Dumping CART to " + destFile + " ..."); // Open the destination file (cart.bin) and output the header DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(destFile))); // create new CART-header and write it to output file MaryHeader hdr = new MaryHeader(MaryHeader.CARTS); hdr.writeTo(out); // write number of nodes out.writeInt(cart.getNumNodes()); String name = ""; // dump name and CART out.writeUTF(name); // dump CART dumpBinary(cart, out); // finish out.close(); System.out.println(" ... done\n"); }