java.io.DataOutputStream.write()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(10.7k)|赞(0)|评价(0)|浏览(148)

本文整理了Java中java.io.DataOutputStream.write()方法的一些代码示例,展示了DataOutputStream.write()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。DataOutputStream.write()方法的具体详情如下:
包路径:java.io.DataOutputStream
类名称:DataOutputStream
方法名:write

DataOutputStream.write介绍

[英]Writes a byte to the target stream. Only the least significant byte of the integer oneByte is written.
[中]将字节写入目标流。只写入整数一字节中的最低有效字节。

代码示例

代码示例来源:origin: apache/incubator-dubbo

@Override
public void writeBytes(byte[] v) throws IOException {
  dos.writeInt(v.length);
  dos.write(v);
}

代码示例来源:origin: redisson/redisson

void write(DataOutputStream out) throws IOException {
  out.writeShort(name);
  out.writeInt(info.length);
  if (info.length > 0)
    out.write(info);
}

代码示例来源:origin: google/ExoPlayer

@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);
 }
}

代码示例来源:origin: com.h2database/h2

private void sendMessage() throws IOException {
  dataOut.flush();
  byte[] buff = outBuffer.toByteArray();
  int len = buff.length;
  dataOut = new DataOutputStream(out);
  dataOut.write(messageType);
  dataOut.writeInt(len + 4);
  dataOut.write(buff);
  dataOut.flush();
}

代码示例来源:origin: Dreampie/Resty

/**
 * 输出参数
 *
 * @param conn
 * @param method
 * @param requestParams
 * @throws IOException
 */
private void outputParam(HttpURLConnection conn, String method, String requestParams, String encoding) throws IOException {
 //写入参数
 if (requestParams != null && !"".equals(requestParams)) {
  DataOutputStream writer = new DataOutputStream(conn.getOutputStream());
  logger.debug("Request out method " + method + ",out parameters " + requestParams);
  writer.write(requestParams.getBytes(encoding));
  writer.flush();
  writer.close();
 }
}

代码示例来源:origin: wildfly/wildfly

public static byte[] createAuthenticationDigest(String passcode,long t1,double q1) throws IOException,
                                             NoSuchAlgorithmException {
  ByteArrayOutputStream baos=new ByteArrayOutputStream(512);
  DataOutputStream out=new DataOutputStream(baos);
  byte[] digest=createDigest(passcode,t1,q1);
  out.writeLong(t1);
  out.writeDouble(q1);
  out.writeInt(digest.length);
  out.write(digest);
  out.flush();
  return baos.toByteArray();
}

代码示例来源:origin: redisson/redisson

ByteArrayOutputStream bIn = new ByteArrayOutputStream(1000); // 1000 should be large enough to fit the entire class
try {
  in = new DataOutputStream(bIn);
  in.write(MAGIC);
  in.write(VERSION);
  in.writeShort(CONSTANT_POOL_COUNT);
  in.writeInt(CODE_ATTRIBUTE_LENGTH); // attribute length
  in.writeShort(1); // max_stack
  in.writeShort(1); // max_locals
  in.writeInt(CODE.length); // code length
  in.write(CODE);
  in.writeShort(0); // exception_table_length = 0
  in.writeShort(0); // attributes count = 0, no need to have LineNumberTable and LocalVariableTable
  if(in != null) {
   try {
     in.close();
   } catch (IOException e) {
     throw new ObjenesisException(e);
return bIn.toByteArray();

代码示例来源:origin: apache/hive

public static byte[] encodeDelegationTokenInformation(DelegationTokenInformation token) {
 try {
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  DataOutputStream out = new DataOutputStream(bos);
  WritableUtils.writeVInt(out, token.password.length);
  out.write(token.password);
  out.writeLong(token.renewDate);
  out.flush();
  return bos.toByteArray();
 } catch (IOException ex) {
  throw new RuntimeException("Failed to encode token.", ex);
 }
}

代码示例来源:origin: voldemort/voldemort

public byte[] getBytes() {
  try {
    ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
    DataOutputStream output = new DataOutputStream(byteOutput);
    output.writeByte(opCode);
    if(opCode != VoldemortOpCode.GET_OP_CODE)
      output.write(version.toBytes());
    output.writeUTF(key);
    if(opCode == VoldemortOpCode.PUT_OP_CODE) {
      output.writeInt(value.length);
      output.write(value);
    }
    return byteOutput.toByteArray();
  } catch(IOException e) {
    throw new SerializationException(e);
  }
}

代码示例来源:origin: apache/hbase

@Test
public void testReads() throws Exception {
 ByteArrayOutputStream bos = new ByteArrayOutputStream(100);
 DataOutputStream dos = new DataOutputStream(bos);
 String s = "test";
 int i = 128;
 dos.write(1);
 dos.writeInt(i);
 dos.writeBytes(s);
 dos.writeLong(12345L);
 dos.writeShort(2);
 dos.flush();
 ByteBuffer bb = ByteBuffer.wrap(bos.toByteArray());
 bbis.close();
 bb = ByteBuffer.wrap(bos.toByteArray());
 bbis = new ByteBuffInputStream(new MultiByteBuff(bb));
 DataInputStream dis = new DataInputStream(bbis);

代码示例来源:origin: google/ExoPlayer

private static void doTestSerializationV0RoundTrip(HlsDownloadAction action) throws IOException {
 ByteArrayOutputStream out = new ByteArrayOutputStream();
 DataOutputStream output = new DataOutputStream(out);
 DataOutputStream dataOutputStream = new DataOutputStream(output);
 dataOutputStream.writeUTF(action.type);
 dataOutputStream.writeInt(/* version */ 0);
 dataOutputStream.writeUTF(action.uri.toString());
 dataOutputStream.writeBoolean(action.isRemoveAction);
 dataOutputStream.writeInt(action.data.length);
 dataOutputStream.write(action.data);
 dataOutputStream.writeInt(action.keys.size());
 for (int i = 0; i < action.keys.size(); i++) {
  StreamKey key = action.keys.get(i);
  dataOutputStream.writeInt(key.groupIndex);
  dataOutputStream.writeInt(key.trackIndex);
 }
 dataOutputStream.flush();
 assertEqual(action, deserializeActionFromStream(out));
}

代码示例来源:origin: apache/incubator-gobblin

@Test
public void test() throws Exception {
 ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
 Meter meter = new Meter();
 MeteredOutputStream mos = MeteredOutputStream.builder().out(outputStream).meter(meter).updateFrequency(1).build();
 MyOutputStream duplicated = new MyOutputStream(mos);
 DataOutputStream dos = new DataOutputStream(duplicated);
 dos.write("abcde".getBytes(Charsets.UTF_8));
 Assert.assertEquals(outputStream.toString(Charsets.UTF_8.name()), "aabbccddee");
 Optional<MeteredOutputStream> meteredOutputStream = MeteredOutputStream.findWrappedMeteredOutputStream(dos);
 Assert.assertEquals(meteredOutputStream.get(), mos);
 Assert.assertEquals(meteredOutputStream.get().getBytesProcessedMeter().getCount(), 10);
}

代码示例来源:origin: google/ExoPlayer

writeUnsignedInt(dataOutputStream, duration);
 writeUnsignedInt(dataOutputStream, eventMessage.id);
 dataOutputStream.write(eventMessage.messageData);
 dataOutputStream.flush();
 return byteArrayOutputStream.toByteArray();
} catch (IOException e) {

代码示例来源:origin: apache/rocketmq

private static String createBodyFile(MessageExt msg) throws IOException {
  DataOutputStream dos = null;
  try {
    String bodyTmpFilePath = "/tmp/rocketmq/msgbodys";
    File file = new File(bodyTmpFilePath);
    if (!file.exists()) {
      file.mkdirs();
    }
    bodyTmpFilePath = bodyTmpFilePath + "/" + msg.getMsgId();
    dos = new DataOutputStream(new FileOutputStream(bodyTmpFilePath));
    dos.write(msg.getBody());
    return bodyTmpFilePath;
  } finally {
    if (dos != null)
      dos.close();
  }
}

代码示例来源:origin: libgdx/libgdx

/** Writes the ETC1Data with a PKM header to the given file.
 * @param file the file. */
public void write (FileHandle file) {
  DataOutputStream write = null;
  byte[] buffer = new byte[10 * 1024];
  int writtenBytes = 0;
  compressedData.position(0);
  compressedData.limit(compressedData.capacity());
  try {
    write = new DataOutputStream(new GZIPOutputStream(file.write(false)));
    write.writeInt(compressedData.capacity());
    while (writtenBytes != compressedData.capacity()) {
      int bytesToWrite = Math.min(compressedData.remaining(), buffer.length);
      compressedData.get(buffer, 0, bytesToWrite);
      write.write(buffer, 0, bytesToWrite);
      writtenBytes += bytesToWrite;
    }
  } catch (Exception e) {
    throw new GdxRuntimeException("Couldn't write PKM file to '" + file + "'", e);
  } finally {
    StreamUtils.closeQuietly(write);
  }
  compressedData.position(dataOffset);
  compressedData.limit(compressedData.capacity());
}

代码示例来源:origin: jenkinsci/jenkins

private ByteArrayOutputStream encodeToBytes() throws IOException {
  ByteArrayOutputStream buf = new ByteArrayOutputStream();
  try (OutputStream gzos = new GZIPOutputStream(buf);
     ObjectOutputStream oos = JenkinsJVM.isJenkinsJVM() ? AnonymousClassWarnings.checkingObjectOutputStream(gzos) : new ObjectOutputStream(gzos)) {
    oos.writeObject(this);
  }
  ByteArrayOutputStream buf2 = new ByteArrayOutputStream();
  DataOutputStream dos = new DataOutputStream(new Base64OutputStream(buf2,true,-1,null));
  try {
    buf2.write(PREAMBLE);
    if (JenkinsJVM.isJenkinsJVM()) { // else we are in another JVM and cannot sign; result will be ignored unless INSECURE
      byte[] mac = MAC.mac(buf.toByteArray());
      dos.writeInt(- mac.length); // negative to differentiate from older form
      dos.write(mac);
    }
    dos.writeInt(buf.size());
    buf.writeTo(dos);
  } finally {
    dos.close();
  }
  buf2.write(POSTAMBLE);
  return buf2;
}

代码示例来源:origin: marytts/marytts

/**
 * Outputs the data in wav format.
 * 
 * @param fileName
 *            file name
 * @param sampleRate
 *            sample rate
 * @throws IOException
 *             IOException
 */
private void doWrite(String fileName, int sampleRate) throws IOException {
  DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(fileName)));
  dos.writeBytes("RIFF"); // "RIFF" in ascii
  dos.writeInt(byteswap(36 + buf.length)); // Chunk size
  dos.writeBytes("WAVEfmt ");
  dos.writeInt(byteswap(16)); // chunk size, 16 for PCM
  dos.writeShort(byteswap((short) 1)); // PCM format
  dos.writeShort(byteswap((short) 1)); // Mono, one channel
  dos.writeInt(byteswap(sampleRate)); // Samplerate
  dos.writeInt(byteswap(sampleRate * nBytesPerSample)); // Byte-rate
  dos.writeShort(byteswap((short) (nBytesPerSample))); // Nbr of bytes per samples x nbr of channels
  dos.writeShort(byteswap((short) (nBytesPerSample * 8))); // nbr of bits per sample
  dos.writeBytes("data");
  dos.writeInt(byteswap(buf.length));
  dos.write(buf); // <= This buffer should already be byte-swapped at this stage
  dos.close();
}

代码示例来源:origin: google/guava

private void initializeData(DataOutputStream out) throws IOException {
 /* Write out various test values NORMALLY */
 out.write(new byte[] {-100, 100});
 out.writeBoolean(true);
 out.writeBoolean(false);
 out.writeByte(100);
 out.writeByte(-100);
 out.writeByte((byte) 200);
 out.writeChar('a');
 out.writeShort((short) -30000);
 out.writeShort((short) 50000);
 out.writeInt(0xCAFEBABE);
 out.writeLong(0xDEADBEEFCAFEBABEL);
 out.writeUTF("Herby Derby");
 out.writeFloat(Float.intBitsToFloat(0xCAFEBABE));
 out.writeDouble(Double.longBitsToDouble(0xDEADBEEFCAFEBABEL));
}

代码示例来源:origin: jenkinsci/jenkins

protected final synchronized void send(Op op, byte[] chunk, int off, int len) throws IOException {
  dos.writeInt(len);
  writeOp(op);
  dos.write(chunk, off, len);
  dos.flush();
}

代码示例来源:origin: jphp-group/jphp

protected void write(CommandArguments args, AbstractCommand command) {
  try {
    Document document = xmlBuilder.newDocument();
    command.run(this, args, document);
    StringWriter xmlWriter = new StringWriter();
    StreamResult xmlResult = new StreamResult(xmlWriter);
    transformer.transform(new DOMSource(document), xmlResult);
    byte[] xmlBytes = xmlWriter.toString().getBytes();
    DataOutputStream out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
    out.write(String.valueOf(xmlBytes.length).getBytes());
    out.write("\0".getBytes());
      out.write(xmlBytes);
    out.write("\0".getBytes());
    out.flush();
  } catch (IOException | TransformerException e) {
    throw new DebuggerException(e);
  }
}

相关文章