java.io.ByteArrayOutputStream#flush ( )源码实例Demo

下面列出了java.io.ByteArrayOutputStream#flush ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: onpc   文件: JacketArtMsg.java
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;
}
 
源代码2 项目: protect   文件: Parse.java
/**
 * 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);
	}
}
 
源代码3 项目: java-11-examples   文件: SignedDataWriter.java
@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;
}
 
源代码4 项目: protect   文件: BFTMapServer.java
@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];
	}
}
 
源代码5 项目: protect   文件: Parse.java
/**
 * 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);
	}
}
 
源代码6 项目: dts   文件: MetricsHttpServer.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);
    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();
}
 
源代码7 项目: search-spring-boot-starter   文件: CommonUtils.java
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;
}
 
源代码8 项目: freehealth-connector   文件: CommonIOUtils.java
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;
}
 
源代码9 项目: datawave   文件: IndexMatchTest.java
/**
 * 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;
}
 
源代码10 项目: openjdk-jdk8u   文件: TestUnloadingEventClass.java
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;
    }
}
 
源代码11 项目: openjdk-jdk8u   文件: JIClassInstrumentation.java
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();
}
 
源代码12 项目: openjdk-jdk8u   文件: TestUnloadEventClassCount.java
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);
}
 
源代码14 项目: activiti6-boot2   文件: EmailSendTaskTest.java
protected String getMessage(MimeMessage mimeMessage) throws MessagingException, IOException {
  DataHandler dataHandler = mimeMessage.getDataHandler();
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  dataHandler.writeTo(baos);
  baos.flush();
  return baos.toString();
}
 
源代码15 项目: juice   文件: RSAUtils.java
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();
    }
}
 
源代码16 项目: dragonwell8_jdk   文件: TestUnloadingEventClass.java
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;
    }
}
 
源代码17 项目: TencentKona-8   文件: Packet.java
@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;
}
 
源代码19 项目: steady   文件: FileUtil.java
/**
 * 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();		
}
 
源代码20 项目: huobi_Java   文件: InternalUtils.java
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();
}