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

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

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

ByteArrayOutputStream.close介绍

[英]Closes this stream. This releases system resources used for this stream.
[中]

代码示例

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

private Object serializeValue(SerializationDelegate serialization, Object storeValue) throws IOException {
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  try {
    serialization.serialize(storeValue, out);
    return out.toByteArray();
  }
  finally {
    out.close();
  }
}

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

HttpClient httpclient = new DefaultHttpClient();
 HttpResponse response = httpclient.execute(new HttpGet(URL));
 StatusLine statusLine = response.getStatusLine();
 if(statusLine.getStatusCode() == HttpStatus.SC_OK){
   ByteArrayOutputStream out = new ByteArrayOutputStream();
   response.getEntity().writeTo(out);
   String responseString = out.toString();
   out.close();
   //..more logic
 } else{
   //Closes the connection.
   response.getEntity().getContent().close();
   throw new IOException(statusLine.getReasonPhrase());
 }

代码示例来源:origin: daniulive/SmarterStreaming

private byte[] ReadAssetFileDataToByte(InputStream in) throws IOException {
  ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
  int c = 0;
  while ((c = in.read()) != -1) {
    bytestream.write(c);
  }
  byte bytedata[] = bytestream.toByteArray();
  bytestream.close();
  return bytedata;
}

代码示例来源:origin: daniulive/SmarterStreaming

private byte[] ReadAssetFileDataToByte(InputStream in) throws IOException
{
  ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
  int c = 0;
      
  while ( (c = in.read()) != -1 )
  {
   bytestream.write(c);
  }
            byte bytedata[] = bytestream.toByteArray();
      bytestream.close();
      return bytedata;
}

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

/**
 * {@inheritDoc}
 */
public Sink write(Manifest manifest) throws IOException {
  if (manifest != null) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try {
      manifest.write(outputStream);
    } finally {
      outputStream.close();
    }
    storage.put(JarFile.MANIFEST_NAME, outputStream.toByteArray());
  }
  return this;
}

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

private static String toJson(Applications applications) throws IOException {
  ByteArrayOutputStream os = new ByteArrayOutputStream();
  new EntityBodyConverter().write(applications, os, MediaType.APPLICATION_JSON_TYPE);
  os.close();
  return os.toString();
}

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

URL url = new URL("http://www.yahoo.com/image_to_read.jpg");
InputStream in = new BufferedInputStream(url.openStream());
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int n = 0;
while (-1!=(n=in.read(buf)))
{
  out.write(buf, 0, n);
}
out.close();
in.close();
byte[] response = out.toByteArray();

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

/**
 * @return a byte[] containing the information contained in the
 * specified InputStream.
 * @throws java.io.IOException
 */
public static byte[] getBytes(InputStream input)
  throws IOException {
 ByteArrayOutputStream result = new ByteArrayOutputStream();
 copy(input, result);
 result.close();
 return result.toByteArray();
}

代码示例来源:origin: JessYanCoding/MVPArms

public static String bytyToString(InputStream in) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte[] buf = new byte[1024];
    int num = 0;
    while ((num = in.read(buf)) != -1) {
      out.write(buf, 0, buf.length);
    }
    String result = out.toString();
    out.close();
    return result;
  }
}

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

/**
 * Gets the stack trace from a Throwable as a String.
 *
 * @param cause the {@link Throwable} to be examined
 * @return the stack trace as generated by {@link Throwable#printStackTrace(java.io.PrintWriter)} method.
 */
public static String stackTraceToString(Throwable cause) {
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  PrintStream pout = new PrintStream(out);
  cause.printStackTrace(pout);
  pout.flush();
  try {
    return new String(out.toByteArray());
  } finally {
    try {
      out.close();
    } catch (IOException ignore) {
      // ignore as should never happen
    }
  }
}

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

@Override
public byte[] rowKey(List<Object> keys) {
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  try {
    for (Object key : keys) {
      bos.write(String.valueOf(key).getBytes());
    }
    bos.close();
  } catch (IOException e) {
    throw new RuntimeException("IOException creating HBase row key.", e);
  }
  return bos.toByteArray();
}

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

@Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    resp.setHeader("Plugins-Status", status);
    pluginProps.setProperty("Active Mock Bundle 1", "1.1.1");
    pluginProps.setProperty("Active Mock Bundle 2", "2.2.2");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    pluginProps.store(baos, "Go Plugins for Testing");
    resp.getOutputStream().write(baos.toByteArray());
    baos.close();
  }
}

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

ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = null;
try {
 out = new ObjectOutputStream(bos);   
 out.writeObject(yourObject);
 out.flush();
 byte[] yourBytes = bos.toByteArray();
 ...
} finally {
 try {
  bos.close();
 } catch (IOException ex) {
  // ignore close exception
 }
}

代码示例来源:origin: org.springframework/spring-context

private Object serializeValue(SerializationDelegate serialization, Object storeValue) throws IOException {
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  try {
    serialization.serialize(storeValue, out);
    return out.toByteArray();
  }
  finally {
    out.close();
  }
}

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

public static byte[] gunzip(byte[] data) {
  try {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ByteArrayInputStream bis = new ByteArrayInputStream(data);
    GZIPInputStream in = new GZIPInputStream(bis);
    byte[] buffer = new byte[1024];
    int len = 0;
    while ((len = in.read(buffer)) >= 0) {
      bos.write(buffer, 0, len);
    }
    in.close();
    bos.close();
    return bos.toByteArray();
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}

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

public String print(Annotation ann, Options options) throws IOException {
 ByteArrayOutputStream os = new ByteArrayOutputStream();
 print(ann, os, options);
 os.close();
 return new String(os.toByteArray());
}

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

/**
   * Create Mongo _id based on input keys.
   * @param keys keys
   * @return Mongo _id
   */
  public static byte[] getId(List<Object> keys) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try {
      for (Object key : keys) {
        bos.write(String.valueOf(key).getBytes());
      }
      bos.close();
    } catch (IOException e) {
      throw new RuntimeException("IOException creating Mongo document _id.", e);
    }
    return bos.toByteArray();
  }
}

代码示例来源:origin: jeasonlzy/okhttp-OkGo

public static byte[] toByteArray(InputStream input) throws IOException {
  ByteArrayOutputStream output = new ByteArrayOutputStream();
  write(input, output);
  output.close();
  return output.toByteArray();
}

代码示例来源:origin: aws/aws-sdk-java

/**
 * Reads and returns the rest of the given input stream as a byte array.
 * Caller is responsible for closing the given input stream.
 */
public static byte[] toByteArray(InputStream is) throws IOException {
  ByteArrayOutputStream output = new ByteArrayOutputStream();
  try {
    byte[] b = new byte[BUFFER_SIZE];
    int n = 0;
    while ((n = is.read(b)) != -1) {
      output.write(b, 0, n);
    }
    return output.toByteArray();
  } finally {
    output.close();
  }
}

代码示例来源:origin: jeasonlzy/okhttp-OkGo

public static byte[] toByteArray(Reader input) throws IOException {
  ByteArrayOutputStream output = new ByteArrayOutputStream();
  write(input, output);
  output.close();
  return output.toByteArray();
}

相关文章