java.io.DataOutputStream类的使用及代码示例

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

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

DataOutputStream介绍

[英]Wraps an existing OutputStream and writes big-endian typed data to it. Typically, this stream can be read in by DataInputStream. Types that can be written include byte, 16-bit short, 32-bit int, 32-bit float, 64-bit long, 64-bit double, byte strings, and DataInput encoded strings.
[中]包装现有的OutputStream并将big-endian类型的数据写入其中。通常,此流可以由DataInputStream读入。可以写入的类型包括byte、16位short、32位int、32位float、64位long、64位double、字节字符串和数据输入编码字符串。

代码示例

代码示例来源:origin: stackoverflow.com

ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
dos.writeLong(someLong);
dos.close();
byte[] longBytes = baos.toByteArray();

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

@Override
public void close() throws IOException {
  out.close();
}

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

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

代码示例来源:origin: skylot/jadx

private void writeString(DataOutputStream out, String name) throws IOException {
  byte[] bytes = name.getBytes(STRING_CHARSET);
  out.writeByte(bytes.length);
  out.write(bytes);
}

代码示例来源: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: stackoverflow.com

FileInputStream fileInputStream = new FileInputStream(new File(existingFileName));
URL url = new URL(urlString);
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + existingFileName + "\"" + lineEnd);
dos.writeBytes(lineEnd);
  dos.write(buffer, 0, bufferSize);
  bytesAvailable = fileInputStream.available();
  bufferSize = Math.min(bytesAvailable, maxBufferSize);
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
fileInputStream.close();
dos.flush();
dos.close();

代码示例来源:origin: stackoverflow.com

OutputStream outputStream = connection.getOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
dataOutputStream.writeBytes(query.toString());
  dataOutputStream.writeBytes(twoHyphens + boundary + crlf);
  dataOutputStream.writeBytes("Content-Disposition: form-data; name=\"" + avatarName + "\";filename=\"" + new File(avatarPath).getName() + "\";" + crlf);
  dataOutputStream.writeBytes(crlf);
  inputStream = new FileInputStream(avatarPath);
  byte[] data = new byte[1024];
  int read;
  while((read = inputStream.read(data)) != -1)
    dataOutputStream.write(data, 0, read);
  inputStream.close();
  dataOutputStream.writeBytes(crlf);
  dataOutputStream.writeBytes(twoHyphens + boundary + twoHyphens + crlf);
dataOutputStream.flush();
dataOutputStream.close();
String responseMessage = connection.getResponseMessage();
Log.d(TAG, responseMessage);
InputStream in = connection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in, "UTF-8"));

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

public DataOutputStream writeHeader(String file, boolean bLeaveStreamOpen) throws IOException {
  DataOutputStream stream = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
  if (stream != null) {
    writeHeader(stream);
    if (!bLeaveStreamOpen) {
      stream.close();
      stream = null;
    }
  }
  return stream;
}

代码示例来源:origin: stackoverflow.com

private static void downloadFile(String url, File outputFile) {
 try {
   URL u = new URL(url);
   URLConnection conn = u.openConnection();
   int contentLength = conn.getContentLength();

   DataInputStream stream = new DataInputStream(u.openStream());

    byte[] buffer = new byte[contentLength];
    stream.readFully(buffer);
    stream.close();

    DataOutputStream fos = new DataOutputStream(new FileOutputStream(outputFile));
    fos.write(buffer);
    fos.flush();
    fos.close();
 } catch(FileNotFoundException e) {
   return; // swallow a 404
 } catch (IOException e) {
   return; // swallow a 404
 }
}

代码示例来源:origin: stackoverflow.com

URL url = new URL(targetURL);
 connection = (HttpURLConnection) url.openConnection();
 connection.setRequestMethod("POST");
 connection.setRequestProperty("Content-Type", 
   "application/x-www-form-urlencoded");
 connection.setRequestProperty("Content-Length", 
   Integer.toString(urlParameters.getBytes().length));
 connection.setRequestProperty("Content-Language", "en-US");  
 connection.setUseCaches(false);
 DataOutputStream wr = new DataOutputStream (
   connection.getOutputStream());
 wr.writeBytes(urlParameters);
 wr.close();
 InputStream is = connection.getInputStream();
 BufferedReader rd = new BufferedReader(new InputStreamReader(is));
 StringBuilder response = new StringBuilder(); // or StringBuffer if Java version 5+
 String line;
} finally {
 if (connection != null) {
  connection.disconnect();

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

private void gzipFile(String srcPath, String destPath) throws Exception {
  byte[] buffer = new byte[1024];
  FileOutputStream fileOutputStream = new FileOutputStream(destPath);
  DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new GZIPOutputStream(fileOutputStream)));
  FileInputStream fileInput = new FileInputStream(srcPath);
  int bytes_read;
  while((bytes_read = fileInput.read(buffer)) > 0) {
    out.write(buffer, 0, bytes_read);
  }
  fileInput.close();
  out.close();
}

代码示例来源:origin: stackoverflow.com

DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(filename), 64*1024));
int len = dis.readInt();
int[] ints = new int[len];
for(int i=0;i<len;i++)
  ints[i] = dis.readInt();
return ints;
DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(name), 64*1024));
dos.writeInt(numbers.length);
for (int number : numbers)
  dos.writeInt(number);
dos.close();

代码示例来源:origin: stackoverflow.com

theUrlConnection.setRequestProperty("Content-Type", "multipart/form-data; boundary="
    + Boundary);
DataOutputStream httpOut = new DataOutputStream(theUrlConnection.getOutputStream());
        + "\r\n";
  httpOut.write(str.getBytes());
  FileInputStream uploadFileReader = new FileInputStream(f);
  int numBytesToRead = 1024;
  int availableBytesToRead;
  while ((availableBytesToRead = uploadFileReader.available()) > 0)
    uploadFileReader.read(bufferBytesRead);
    httpOut.write(bufferBytesRead);
    httpOut.flush();
  httpOut.write(("--" + Boundary + "--\r\n").getBytes());
httpOut.write(("--" + Boundary + "--\r\n").getBytes());
httpOut.flush();
httpOut.close();
list.add(new File("C:\\narrow.png"));
UploaderExample uploader = new UploaderExample();
uploader.upload(new URL("http://systemout.com/upload.php"), list);

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

private static void doTestSerializationRoundTrip(DownloadAction action) throws IOException {
 ByteArrayOutputStream out = new ByteArrayOutputStream();
 DataOutputStream output = new DataOutputStream(out);
 DownloadAction.serializeToStream(action, output);
 ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
 DataInputStream input = new DataInputStream(in);
 DownloadAction action2 =
   DownloadAction.deserializeFromStream(
     new DownloadAction.Deserializer[] {ProgressiveDownloadAction.DESERIALIZER}, input);
 assertThat(action2).isEqualTo(action);
}

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

protected byte[] randomPayload(int sizeBytes) throws Exception {
  Random random = new Random();
  byte[] payload = new byte[sizeBytes + 4];
  random.nextBytes(payload);
  ByteArrayOutputStream prefixOs = new ByteArrayOutputStream();
  DataOutputStream prefixDos = new DataOutputStream(prefixOs);
  prefixDos.writeInt(sizeBytes);
  prefixDos.flush();
  prefixDos.close();
  prefixOs.flush();
  prefixOs.close();
  byte[] prefix = prefixOs.toByteArray();
  System.arraycopy(prefix, 0, payload, 0, prefix.length);
  return payload;
}

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

@Override
public byte[] toBytes()
{
 try {
  final ByteArrayOutputStream out = new ByteArrayOutputStream();
  bitmap.serialize(new DataOutputStream(out));
  return out.toByteArray();
 }
 catch (Exception e) {
  throw Throwables.propagate(e);
 }
}

代码示例来源:origin: stackoverflow.com

ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream w = new DataOutputStream(baos);

w.writeInt(100);
w.write(byteArray);

w.flush();

byte[] result = baos.toByteArray();

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

private static byte[] toBytecode(ClassFile cf) throws IOException {
  ByteArrayOutputStream barray = new ByteArrayOutputStream();
  DataOutputStream out = new DataOutputStream(barray);
  try {
    cf.write(out);
  }
  finally {
    out.close();
  }
  return barray.toByteArray();
}

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

protected final void send(Op op, String text) throws IOException {
  ByteArrayOutputStream buf = new ByteArrayOutputStream();
  new DataOutputStream(buf).writeUTF(text);
  send(op, buf.toByteArray());
}

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

public Connection(Socket sock) throws IOException {
  this.sock=sock;
  this.in=new DataInputStream(sock.getInputStream());
  this.out=new DataOutputStream(sock.getOutputStream());
}

相关文章