java.io.ByteArrayOutputStream.flush()方法的使用及代码示例

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

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

ByteArrayOutputStream.flush介绍

暂无

代码示例

代码示例来源:origin: spring-projects/spring-framework

/**
 * Reads the given input stream and returns its content as a byte array.
 *
 * @param inputStream an input stream.
 * @param close true to close the input stream after reading.
 * @return the content of the given input stream.
 * @throws IOException if a problem occurs during reading.
 */
private static byte[] readStream(final InputStream inputStream, final boolean close)
  throws IOException {
 if (inputStream == null) {
  throw new IOException("Class not found");
 }
 try {
  ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  byte[] data = new byte[INPUT_STREAM_DATA_CHUNK_SIZE];
  int bytesRead;
  while ((bytesRead = inputStream.read(data, 0, data.length)) != -1) {
   outputStream.write(data, 0, bytesRead);
  }
  outputStream.flush();
  return outputStream.toByteArray();
 } finally {
  if (close) {
   inputStream.close();
  }
 }
}

代码示例来源:origin: SonarSource/sonarqube

public byte[] getFlushedOutput() {
  try {
   output.flush();
   return output.toByteArray();
  } catch (IOException e) {
   throw Throwables.propagate(e);
  }
 }
}

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

public static byte[] convertToByteArray(Properties props) throws IOException {
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  props.store(out, null);
  out.flush();
  return out.toByteArray();
}

代码示例来源:origin: twitter/elephant-bird

/**
 * Method used to read a protobuf input stream into a byte array
 * @param in the protobuf input stream
 * @param out the byte output stream
 * @param buffer the array out output bytes
 */
public static void readFully(InputStream in, ByteArrayOutputStream out, byte[] buffer) {
 try {
  int numRead = 0;
  while ((numRead = in.read(buffer, 0, buffer.length)) != -1) {
   out.write(buffer, 0, numRead);
  }
  out.flush();
 } catch (IOException e) {
  throw new RuntimeException(e);
 }
}

代码示例来源:origin: Nepxion/Discovery

public static String getText(Document document, String charset) throws IOException, UnsupportedEncodingException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    OutputFormat outputFormat = new OutputFormat("  ", true, charset);

    try {
      XMLWriter writer = new XMLWriter(baos, outputFormat);
      writer.write(document);

      baos.flush();
    } catch (UnsupportedEncodingException e) {
      throw e;
    } catch (IOException e) {
      throw e;
    } finally {
      if (baos != null) {
        baos.close();
      }
    }

    return baos.toString(charset);
  }
}

代码示例来源:origin: CarGuo/GSYVideoPlayer

ByteArrayOutputStream baos = new ByteArrayOutputStream();
AnimatedGifEncoder localAnimatedGifEncoder = new AnimatedGifEncoder();
  FileOutputStream fos = new FileOutputStream(file.getPath());
  baos.writeTo(fos);
  baos.flush();
  fos.flush();
  baos.close();
  fos.close();
} catch (IOException e) {

代码示例来源:origin: testcontainers/testcontainers-java

@Override
public void accept(OutputFrame outputFrame) {
  try {
    if (outputFrame.getBytes() != null) {
      if (!firstLine) {
        stringBuffer.write(NEW_LINE);
      }
      stringBuffer.write(outputFrame.getBytes());
      stringBuffer.flush();
      firstLine = false;
    }
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}

代码示例来源:origin: north2016/T-MVP

/**
 * 保存文件
 *
 * @param baos
 * @param fileName
 * @throws Exception
 */
public static String saveFile(ByteArrayOutputStream baos, String fileName)
    throws Exception {
  String Path = App.getAppContext().getFilesDir().getPath();
  File dirFile = new File(Path);
  if (!dirFile.exists()) {
    dirFile.mkdir();
  }
  File myCaptureFile = new File(Path + "/" + fileName);
  FileOutputStream fi = new FileOutputStream(myCaptureFile);
  baos.writeTo(fi);
  baos.flush();
  baos.close();
  return myCaptureFile.getAbsolutePath();
}

代码示例来源:origin: bumptech/glide

private static byte[] inputStreamToBytes(InputStream is) {
  final int bufferSize = 16384;
  ByteArrayOutputStream buffer = new ByteArrayOutputStream(bufferSize);
  try {
   int nRead;
   byte[] data = new byte[bufferSize];
   while ((nRead = is.read(data)) != -1) {
    buffer.write(data, 0, nRead);
   }
   buffer.flush();
  } catch (IOException e) {
   if (Log.isLoggable(TAG, Log.WARN)) {
    Log.w(TAG, "Error reading data from stream", e);
   }
   return null;
  }
  return buffer.toByteArray();
 }
}

代码示例来源:origin: spring-projects/spring-loaded

public static byte[] write(Object o) {
  try {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(o);
    oos.close();
    baos.flush();
    return baos.toByteArray();
  } catch (Exception e) {
    return null;
  }
}

代码示例来源:origin: OfficeDev/ews-java-api

throws XMLStreamException, IOException {
String lineSeparator = System.getProperty("line.separator");
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
XMLOutputFactory factory = XMLOutputFactory.newInstance();
XMLStreamWriter writer = factory.createXMLStreamWriter(outStream);
writer.flush();
writer.close();
outStream.flush();
String formattedLogMessage = outStream.toString();
formattedLogMessage = formattedLogMessage.replaceAll("'", "'");
formattedLogMessage = formattedLogMessage.replaceAll("&lt;", "<");
formattedLogMessage = formattedLogMessage.replaceAll("&amp;", "&");
outStream.close();
return formattedLogMessage;

代码示例来源:origin: SonarSource/sonarqube

public byte[] getFlushedOutput() {
  try {
   output.flush();
   return output.toByteArray();
  } catch (IOException e) {
   throw Throwables.propagate(e);
  }
 }
}

代码示例来源:origin: prometheus/client_java

public void handle(HttpExchange t) throws IOException {
  String query = t.getRequestURI().getRawQuery();
  ByteArrayOutputStream response = this.response.get();
  response.reset();
  OutputStreamWriter osw = new OutputStreamWriter(response);
  TextFormat.write004(osw,
      registry.filteredMetricFamilySamples(parseQuery(query)));
  osw.flush();
  osw.close();
  response.flush();
  response.close();
  t.getResponseHeaders().set("Content-Type",
      TextFormat.CONTENT_TYPE_004);
  if (shouldUseCompression(t)) {
    t.getResponseHeaders().set("Content-Encoding", "gzip");
    t.sendResponseHeaders(HttpURLConnection.HTTP_OK, 0);
    final GZIPOutputStream os = new GZIPOutputStream(t.getResponseBody());
    response.writeTo(os);
    os.close();
  } else {
    t.getResponseHeaders().set("Content-Length",
        String.valueOf(response.size()));
    t.sendResponseHeaders(HttpURLConnection.HTTP_OK, response.size());
    response.writeTo(t.getResponseBody());
  }
  t.close();
}

代码示例来源:origin: hankcs/HanLP

/**
 * 将非FileInputStream的某InputStream中的全部数据读入到字节数组中
 *
 * @param is
 * @return
 * @throws IOException
 */
public static byte[] readBytesFromOtherInputStream(InputStream is) throws IOException
{
  ByteArrayOutputStream data = new ByteArrayOutputStream();
  int readBytes;
  byte[] buffer = new byte[Math.max(is.available(), 4096)]; // 最低4KB的缓冲区
  while ((readBytes = is.read(buffer, 0, buffer.length)) != -1)
  {
    data.write(buffer, 0, readBytes);
  }
  data.flush();
  return data.toByteArray();
}

代码示例来源:origin: spring-projects/spring-loaded

public static byte[] write(Object o) {
  try {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(o);
    oos.close();
    baos.flush();
    return baos.toByteArray();
  } catch (Exception e) {
    return null;
  }
}

代码示例来源:origin: vondear/RxTool

private byte[] getBytes(InputStream is) throws Exception {
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  byte[] buffer = new byte[1024];
  int len = 0;
  while ((len = is.read(buffer)) != -1) {
    bos.write(buffer, 0, len);
  }
  is.close();
  bos.flush();
  byte[] result = bos.toByteArray();
  System.out.println(new String(result));
  return result;
}

代码示例来源:origin: spring-projects/spring-loaded

public static byte[] write(Object o) {
  try {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(o);
    oos.close();
    baos.flush();
    return baos.toByteArray();
  } catch (Exception e) {
    return null;
  }
}

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

public static byte [] readFully(InputStream is) {
  try {
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    int num;
    byte[] data = new byte[1 << 12];
    while ((num = is.read( data, 0, data.length )) != -1)
      buffer.write(data, 0, num);
    buffer.flush();
    return buffer.toByteArray();
  }
  catch (IOException ex) { return null; }
}

代码示例来源:origin: stanfordnlp/CoreNLP

byte[] getProtoBufAnnotation(List<CoreLabel> tokens) throws IOException {
 ByteArrayOutputStream os = new ByteArrayOutputStream();
 for(CoreLabel token: tokens){
  CoreNLPProtos.Token ptoken = p.toProto(token);
  ptoken.writeDelimitedTo(os);
 }
 os.flush();
 return os.toByteArray();
}

代码示例来源:origin: siyamed/android-shape-imageview

private void copy() throws IOException {
  _copy = new ByteArrayOutputStream();
  int chunk;
  byte[] data = new byte[256];
  while(-1 != (chunk = _is.read(data))) {
    _copy.write(data, 0, chunk);
  }
  _copy.flush();
}

相关文章