下面列出了java.io.ByteArrayOutputStream#flush ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public Bitmap loadFromBuffer(ByteArrayOutputStream coverBuffer)
{
if (coverBuffer == null)
{
Logging.info(this, "can not open image: empty stream");
return null;
}
Bitmap cover = null;
try
{
Logging.info(this, "loading image from stream");
coverBuffer.flush();
coverBuffer.close();
final byte[] out = coverBuffer.toByteArray();
cover = BitmapFactory.decodeByteArray(out, 0, out.length);
}
catch (Exception e)
{
Logging.info(this, "can not open image: " + e.getLocalizedMessage());
}
if (cover == null)
{
Logging.info(this, "can not open image");
}
return cover;
}
/**
* Generates a deterministic serialization of a group of EC Points
*
* @param integers
* @return
* @throws IOException
*/
public static byte[] concatenate(EcPoint... points) {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
for (EcPoint p : points) {
byte[] encoded1 = p.getX().toByteArray();
dos.writeInt(encoded1.length);
dos.write(encoded1);
byte[] encoded2 = p.getY().toByteArray();
dos.writeInt(encoded2.length);
dos.write(encoded2);
}
dos.flush();
bos.flush();
return bos.toByteArray();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public byte[] writeData(SignedData data) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(baos);
dataOutputStream.writeUTF(data.getAlgorithm());
byte[] payload = data.getData();
dataOutputStream.writeInt(payload.length);
dataOutputStream.write(payload);
byte[] signature = data.getSignature();
dataOutputStream.writeInt(signature.length);
dataOutputStream.write(signature);
dataOutputStream.flush();
baos.flush();
byte[] result = baos.toByteArray();
dataOutputStream.close();
baos.close();
return result;
}
@Override
public byte[] getSnapshot() {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(bos);
out.writeObject(tableMap);
out.flush();
bos.flush();
out.close();
bos.close();
return bos.toByteArray();
} catch (IOException ex) {
ex.printStackTrace();
return new byte[0];
}
}
/**
* Generates a deterministic serialization of a list of byte arrays
*
* @param arrays
* @return
* @throws IOException
*/
public static byte[] concatenate(final List<byte[]> list) {
try {
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
final DataOutputStream dos = new DataOutputStream(bos);
for (byte[] array : list) {
dos.writeInt(array.length);
dos.write(array);
}
dos.flush();
bos.flush();
return bos.toByteArray();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
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);
t.getResponseHeaders().set("Content-Length", String.valueOf(response.size()));
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.finish();
} else {
t.sendResponseHeaders(HttpURLConnection.HTTP_OK, response.size());
response.writeTo(t.getResponseBody());
}
t.close();
}
public static String inputStream2String(InputStream in) throws IOException {
byte[] b = new byte[512];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int read = 0;
while ((read = in.read(b)) != -1) {
bos.write(b, 0, read);
}
byte[] outbyte = bos.toByteArray();
bos.flush();
bos.close();
if (in != null) {
in.close();
}
String result = new String(outbyte, "utf-8");
return result;
}
public static byte[] decompress(byte[] unSealmessage) throws IOException {
long size = (long)unSealmessage.length;
ByteArrayInputStream inputstream = new ByteArrayInputStream(unSealmessage);
GZIPInputStream input = new GZIPInputStream(inputstream);
ByteArrayOutputStream o = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int i;
while((i = input.read(buffer)) > 0) {
o.write(buffer, 0, i);
}
o.flush();
input.close();
inputstream.close();
o.close();
byte[] ret = o.toByteArray();
LOG.info("Decompression of data from " + size + " bytes to " + ret.length + " bytes");
return ret;
}
/**
* Write the provided IndexMatch to a byte array and construct a fresh IndexMatch from that byte array.
*
* @param match
* the IndexMatch to be written to a byte array.
* @return a new IndexMatch constructed from the byte array
* @throws IOException
*/
private IndexMatch writeRead(IndexMatch match) throws IOException {
// Write the IndexMatch to a byte array.
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
DataOutput output = new DataOutputStream(outputStream);
match.write(output);
outputStream.flush();
// Construct input stream from byte array.
ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
DataInput input = new DataInputStream(inputStream);
// Construct IndexMatch from input stream and return.
IndexMatch other = new IndexMatch();
other.readFields(input);
return other;
}
private static MyClassLoader createClassLoaderWithEventClass() throws Exception {
String resourceName = EVENT_NAME.replace('.', '/') + ".class";
try (InputStream is = TestUnloadingEventClass.class.getClassLoader().getResourceAsStream(resourceName)) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
int byteValue = 0;
while ((byteValue = is.read(buffer, 0, buffer.length)) != -1) {
baos.write(buffer, 0, byteValue);
}
baos.flush();
MyClassLoader myClassLoader = new MyClassLoader();
Class<?> eventClass = myClassLoader.defineClass(EVENT_NAME, baos.toByteArray());
if (eventClass == null) {
throw new Exception("Could not define test class");
}
if (eventClass.getSuperclass() != Event.class) {
throw new Exception("Superclass should be jdk.jfr.Event");
}
if (eventClass.getSuperclass().getClassLoader() != null) {
throw new Exception("Class loader of jdk.jfr.Event should be null");
}
if (eventClass.getClassLoader() != myClassLoader) {
throw new Exception("Incorrect class loader for event class");
}
eventClass.newInstance(); // force <clinit>
return myClassLoader;
}
}
private static byte[] getOriginalClassBytes(Class<?> clazz) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
String name = "/" + clazz.getName().replace(".", "/") + ".class";
InputStream is = SecuritySupport.getResourceAsStream(name);
int bytesRead;
byte[] buffer = new byte[16384];
while ((bytesRead = is.read(buffer, 0, buffer.length)) != -1) {
baos.write(buffer, 0, bytesRead);
}
baos.flush();
is.close();
return baos.toByteArray();
}
private static MyClassLoader createClassLoaderWithEventClass() throws Exception {
String resourceName = EVENT_NAME.replace('.', '/') + ".class";
try (InputStream is = TestUnloadEventClassCount.class.getClassLoader().getResourceAsStream(resourceName)) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
int byteValue = 0;
while ((byteValue = is.read(buffer, 0, buffer.length)) != -1) {
baos.write(buffer, 0, byteValue);
}
baos.flush();
MyClassLoader myClassLoader = new MyClassLoader();
Class<?> eventClass = myClassLoader.defineClass(EVENT_NAME, baos.toByteArray());
if (eventClass == null) {
throw new Exception("Could not define test class");
}
if (eventClass.getSuperclass() != Event.class) {
throw new Exception("Superclass should be jdk.jfr.Event");
}
if (eventClass.getSuperclass().getClassLoader() != null) {
throw new Exception("Class loader of jdk.jfr.Event should be null");
}
if (eventClass.getClassLoader() != myClassLoader) {
throw new Exception("Incorrect class loader for event class");
}
eventClass.newInstance(); // force <clinit>
return myClassLoader;
}
}
@Test
public void testBothWays() throws SerializationException, IOException {
Map<String, String> attributes = new HashMap<>();
attributes.put("a", "1");
attributes.put("b", "2");
ByteArrayOutputStream output = new ByteArrayOutputStream();
serializer.serialize(attributes, output);
output.flush();
Map<String, String> result = serializer.deserialize(output.toByteArray());
Assert.assertEquals(attributes, result);
}
protected String getMessage(MimeMessage mimeMessage) throws MessagingException, IOException {
DataHandler dataHandler = mimeMessage.getDataHandler();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
dataHandler.writeTo(baos);
baos.flush();
return baos.toString();
}
private static byte[] doRSA(Cipher cipher, int mode, byte[] data, int keySize) throws IOException, BadPaddingException, IllegalBlockSizeException {
int maxBlock;
if(mode == Cipher.DECRYPT_MODE){
maxBlock = keySize / 8;
}else{
maxBlock = keySize / 8 - 11;
}
int length = data.length;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
int offset = 0;
byte[] buf;
int i = 0;
// 对数据分段加密
while (length - offset > 0) {
if (length - offset > maxBlock) {
buf = cipher.doFinal(data, offset, maxBlock);
} else {
buf = cipher.doFinal(data, offset, length - offset);
}
baos.write(buf, 0, buf.length);
i++;
offset = i * maxBlock;
}
baos.flush();
return baos.toByteArray();
} finally {
baos.close();
}
}
private static MyClassLoader createClassLoaderWithEventClass() throws Exception {
String resourceName = EVENT_NAME.replace('.', '/') + ".class";
try (InputStream is = TestUnloadingEventClass.class.getClassLoader().getResourceAsStream(resourceName)) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
int byteValue = 0;
while ((byteValue = is.read(buffer, 0, buffer.length)) != -1) {
baos.write(buffer, 0, byteValue);
}
baos.flush();
MyClassLoader myClassLoader = new MyClassLoader();
Class<?> eventClass = myClassLoader.defineClass(EVENT_NAME, baos.toByteArray());
if (eventClass == null) {
throw new Exception("Could not define test class");
}
if (eventClass.getSuperclass() != Event.class) {
throw new Exception("Superclass should be jdk.jfr.Event");
}
if (eventClass.getSuperclass().getClassLoader() != null) {
throw new Exception("Class loader of jdk.jfr.Event should be null");
}
if (eventClass.getClassLoader() != myClassLoader) {
throw new Exception("Incorrect class loader for event class");
}
eventClass.newInstance(); // force <clinit>
return myClassLoader;
}
}
@Override
public String toString() {
StringBuilder buf = new StringBuilder();
buf.append(super.toString());
String content;
try {
Message msg = getMessage();
if (msg != null) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
XMLStreamWriter xmlWriter = XMLStreamWriterFactory.create(baos, "UTF-8");
msg.copy().writeTo(xmlWriter);
xmlWriter.flush();
xmlWriter.close();
baos.flush();
XMLStreamWriterFactory.recycle(xmlWriter);
byte[] bytes = baos.toByteArray();
//message = Messages.create(XMLStreamReaderFactory.create(null, new ByteArrayInputStream(bytes), "UTF-8", true));
content = new String(bytes, "UTF-8");
} else {
content = "<none>";
}
} catch (Throwable t) {
throw new WebServiceException(t);
}
buf.append(" Content: ").append(content);
return buf.toString();
}
public static Object serializeAndDeserialize(Object o) throws IOException, ClassNotFoundException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(o);
oos.flush();
baos.flush();
byte[] bytes = baos.toByteArray();
ByteArrayInputStream is = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(is);
Object o2 = ois.readObject();
return o2;
}
/**
* Reads the given {@link InputStream} into a byte array.
*
* @param _is a {@link java.io.InputStream} object.
* @throws java.io.IOException
* @return an array of {@link byte} objects.
*/
public static byte[] readInputStream(InputStream _is) throws IOException {
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
final byte[] byte_buffer = new byte[1024];
int len = 0;
while((len = _is.read(byte_buffer)) != -1)
bos.write(byte_buffer,0,len);
bos.flush();
return bos.toByteArray();
}
public static byte[] decode(byte[] data) throws IOException {
ByteArrayInputStream bais = new ByteArrayInputStream(data);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
decompress(bais, baos);
baos.flush();
baos.close();
bais.close();
return baos.toByteArray();
}