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

x33g5p2x  于2022-01-16 转载在 其他  
字(9.3k)|赞(0)|评价(0)|浏览(243)

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

BufferedOutputStream.write介绍

[英]Writes one byte to this stream. Only the low order byte of the integer oneByte is written. If there is room in the buffer, the byte is copied into the buffer and the count incremented. Otherwise, the buffer plus oneByte are written to the target stream, the target is flushed, and the buffer is reset.
[中]将一个字节写入此流。只写入整数1字节的低位字节。如果缓冲区中有空间,则将字节复制到缓冲区中,并且计数递增。否则,将缓冲区加上一个字节写入目标流,刷新目标,并重置缓冲区。

代码示例

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

/**
 * @param req JDBC request bytes.
 * @throws IOException On error.
 */
private void send(byte[] req) throws IOException {
  int size = req.length;
  out.write(size & 0xFF);
  out.write((size >> 8) & 0xFF);
  out.write((size >> 16) & 0xFF);
  out.write((size >> 24) & 0xFF);
  out.write(req);
  out.flush();
}

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

/**
* Write all class bytes to a file.
*
* @param fileName file where the bytes will be written
* @param bytes bytes representing the class
* @throws IOException if we fail to write the class
*/
public static void writeClass(String fileName, byte[] bytes) throws IOException {
 BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(fileName));
 try {
   out.write(bytes);
 }
 finally {
   out.close();
 }
}

代码示例来源:origin: org.objenesis/objenesis

/**
  * Write all class bytes to a file.
  *
  * @param fileName file where the bytes will be written
  * @param bytes bytes representing the class
  * @throws IOException if we fail to write the class
  */
  public static void writeClass(String fileName, byte[] bytes) throws IOException {
   try (BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(fileName))) {
     out.write(bytes);
   }
  }
}

代码示例来源:origin: Netflix/Priam

private void decompress(InputStream input, OutputStream output) throws IOException {
    byte data[] = new byte[BUFFER];
    try (BufferedOutputStream dest1 = new BufferedOutputStream(output, BUFFER);
        SnappyInputStream is = new SnappyInputStream(new BufferedInputStream(input))) {
      int c;
      while ((c = is.read(data, 0, BUFFER)) != -1) {
        dest1.write(data, 0, c);
      }
    }
  }
}

代码示例来源:origin: kaaproject/kaa

@Override
public void saveConfiguration(ByteBuffer buffer) throws IOException {
 PersistentStorage storage = context.createPersistentStorage();
 BufferedOutputStream os = new BufferedOutputStream(storage.openForWrite(path));
 byte[] data = new byte[buffer.remaining()];
 buffer.get(data);
 LOG.trace("Writing {} bytes to output stream", data.length);
 os.write(data);
 os.close();
}

代码示例来源:origin: alibaba/TProfiler

/**
 * 输出状态
 * 
 * @param os
 * @throws IOException
 */
private void write(OutputStream os) throws IOException {
  BufferedOutputStream out = new BufferedOutputStream(os);
  if (Manager.instance().getSwitchFlag()) {
    out.write("running".getBytes());
  } else {
    out.write("stop".getBytes());
  }
  out.write('\r');
  out.flush();
}

代码示例来源:origin: MovingBlocks/Terasology

/**
 * Updates the autosave file with the current state of the tree.
 */
protected void updateAutosave() {
  if (!disableAutosave) {
    try (BufferedOutputStream outputStream = new BufferedOutputStream(Files.newOutputStream(getAutosaveFile()))) {
      JsonElement editorContents = JsonTreeConverter.deserialize(getEditor().getModel().getNode(0).getRoot());
      JsonObject autosaveObject = new JsonObject();
      autosaveObject.addProperty("selectedAsset", getSelectedAsset());
      autosaveObject.add("editorContents", editorContents);
      String jsonString = new GsonBuilder().setPrettyPrinting().create().toJson(autosaveObject);
      outputStream.write(jsonString.getBytes());
    } catch (IOException e) {
      logger.warn("Could not save to autosave file", e);
    }
  }
}

代码示例来源:origin: android-hacker/VirtualXposed

public static void writeToFile(InputStream dataIns, File target) throws IOException {
  final int BUFFER = 1024;
  BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(target));
  int count;
  byte data[] = new byte[BUFFER];
  while ((count = dataIns.read(data, 0, BUFFER)) != -1) {
    bos.write(data, 0, count);
  }
  bos.close();
}

代码示例来源:origin: googleapis/google-cloud-java

private void extractFile(ZipInputStream zipIn, File filePath) throws IOException {
 try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath))) {
  byte[] bytesIn = new byte[1024];
  int read;
  while ((read = zipIn.read(bytesIn)) != -1) {
   bos.write(bytesIn, 0, read);
  }
 }
}

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

public void download() throws IOException {
  FacesContext facesContext = FacesContext.getCurrentInstance();
  ExternalContext externalContext = facesContext.getExternalContext();
  HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();

  response.reset(); // Some JSF component library or some Filter might have set some headers in the buffer beforehand. We want to get rid of them, else it may collide.
  response.setContentType("application/xml"); // Check http://www.iana.org/assignments/media-types for all types. Use if necessary ServletContext#getMimeType() for auto-detection based on filename.
  response.setHeader("Content-disposition", "attachment; filename=\"name.xml\""); // The Save As popup magic is done here. You can give it any filename you want, this only won't work in MSIE, it will use current request URL as filename instead.

  BufferedInputStream input = null;
  BufferedOutputStream output = null;

  try {
    input = new BufferedInputStream(getYourXmlAsInputStream());
    output = new BufferedOutputStream(response.getOutputStream());

    byte[] buffer = new byte[10240];
    for (int length; (length = input.read(buffer)) > 0;) {
      output.write(buffer, 0, length);
    }
  } finally {
    close(output);
    close(input);
  }

  facesContext.responseComplete(); // Important! Else JSF will attempt to render the response which obviously will fail since it's already written with a file and closed.
}

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

public void sendMessage(byte[] msg) throws IOException {
    System.out.println("Sending to client");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    BufferedOutputStream os = new BufferedOutputStream(socket.getOutputStream());
    //first byte is kind of frame
    baos.write(SINGLE_FRAME_UNMASKED);

    //Next byte is length of payload
    baos.write(msg.length);

    //Then goes the message
    baos.write(msg);
    baos.flush();
    baos.close();
    //This function only prints the byte representation of the frame in hex to console
    convertAndPrint(baos.toByteArray());

    //Send the frame to the client
    os.write(baos.toByteArray(), 0, baos.size());
    os.flush();
}

代码示例来源:origin: k9mail/k-9

private void writeLine(String s) throws IOException {
  out.write(s.getBytes());
  out.write('\r');
  out.write('\n');
  out.flush();
}

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

BufferedInputStream bis = new BufferedInputStream(entity.getContent());
String filePath = "sample.txt";
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(filePath)));
int inByte;
while((inByte = bis.read()) != -1) bos.write(inByte);
bis.close();
bos.close();

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

@NoWarning("OS_OPEN_STREAM")
public void writeObject2c(String file, byte b) throws FileNotFoundException, IOException {
  try (FileOutputStream fOut = new FileOutputStream(file);
      BufferedOutputStream output = new BufferedOutputStream(fOut)) {
    output.write(b);
    }
}
@NoWarning("OS_OPEN_STREAM")

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

Long imageId = Long.valueOf(request.getPathInfo().substring(1)); // 123
Image image = imageDAO.find(imageId); // Get Image from DB.
// Image class is just a Javabean with the following properties:
// private String filename;
// private Long length;
// private InputStream content;

response.setHeader("Content-Type", getServletContext().getMimeType(image.getFilename()));
response.setHeader("Content-Length", String.valueOf(image.getLength()));
response.setHeader("Content-Disposition", "inline; filename=\"" + image.getFilename() + "\"");

BufferedInputStream input = null;
BufferedOutputStream output = null;

try {
  input = new BufferedInputStream(image.getContent());
  output = new BufferedOutputStream(response.getOutputStream());
  byte[] buffer = new byte[8192];
  for (int length = 0; (length = input.read(buffer)) > 0) {
    output.write(buffer, 0, length);
  }
} finally {
  if (output != null) try { output.close(); } catch (IOException logOrIgnore) {}
  if (input != null) try { input.close(); } catch (IOException logOrIgnore) {}
}

代码示例来源:origin: LaiFeng-Android/SopCastComponent

@Override
public void onData(byte[] data, int type) {
  if (mBuffer != null){
    try {
      mBuffer.write(data);
      mBuffer.flush();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

代码示例来源:origin: pentaho/pentaho-kettle

public static String encodeBinaryData( byte[] val ) throws IOException {
 ByteArrayOutputStream baos = new ByteArrayOutputStream();
 GZIPOutputStream gzos = new GZIPOutputStream( baos );
 BufferedOutputStream bos = new BufferedOutputStream( gzos );
 bos.write( val );
 bos.flush();
 bos.close();
 return new String( Base64.encodeBase64( baos.toByteArray() ) );
}

代码示例来源:origin: mttkay/ignition

@Override
  protected void writeValueToDisk(File file, byte[] imageData) throws IOException {
    BufferedOutputStream ostream = new BufferedOutputStream(new FileOutputStream(file));

    ostream.write(imageData);

    ostream.close();
  }
}

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

public void write(String file, String text) throws IOException {
  BufferedOutputStream out = null;
  try {
    out = new BufferedOutputStream(new FileOutputStream(file));
    out.write(42);
  } finally {
    if (out != null)
      out.close();
  }
}

代码示例来源:origin: plutext/docx4j

private byte[] getBytesFromInputStream(InputStream is)
    throws Exception {
    BufferedInputStream bufIn = new BufferedInputStream(is);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    BufferedOutputStream bos = new BufferedOutputStream(baos);
    int c = bufIn.read();
    while (c != -1) {
      bos.write(c);
      c = bufIn.read();
    }
    bos.flush();
    baos.flush();
    //bufIn.close(); //don't do that, since it closes the ZipInputStream after we've read an entry!
    bos.close();
    return baos.toByteArray();
  }

相关文章