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

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

源代码1 项目: dubbo-2.6.5   文件: TelnetCodecTest.java
protected byte[] objectToByte(Object obj) {
    byte[] bytes;
    if (obj instanceof String) {
        bytes = ((String) obj).getBytes();
    } else if (obj instanceof byte[]) {
        bytes = (byte[]) obj;
    } else {
        try {
            //object to bytearray
            ByteArrayOutputStream bo = new ByteArrayOutputStream();
            ObjectOutputStream oo = new ObjectOutputStream(bo);
            oo.writeObject(obj);
            bytes = bo.toByteArray();
            bo.close();
            oo.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    return (bytes);
}
 
源代码2 项目: Lottor   文件: KryoSerializer.java
/**
 * 序列化
 *
 * @param obj 需要序更列化的对象
 * @return 序列化后的byte 数组
 * @throws TransactionException
 */
@Override
public byte[] serialize(Object obj) throws TransactionException {
    byte[] bytes;
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try {
        //获取kryo对象
        Kryo kryo = new Kryo();
        Output output = new Output(outputStream);
        kryo.writeObject(output, obj);
        bytes = output.toBytes();
        output.flush();
    } catch (Exception ex) {
        throw new TransactionException("kryo serialize error" + ex.getMessage());
    } finally {
        try {
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {

        }
    }
    return bytes;
}
 
源代码3 项目: nuls-v2   文件: Transaction.java
public byte[] serializeForHash() throws IOException {
    ByteArrayOutputStream bos = null;
    try {
        int size = size() - SerializeUtils.sizeOfBytes(transactionSignature);
        bos = new UnsafeByteArrayOutputStream(size);
        NulsOutputStreamBuffer buffer = new NulsOutputStreamBuffer(bos);
        if (size == 0) {
            bos.write(ToolsConstant.PLACE_HOLDER);
        } else {
            buffer.writeUint16(type);
            buffer.writeUint32(time);
            buffer.writeBytesWithLength(remark);
            buffer.writeBytesWithLength(txData);
            buffer.writeBytesWithLength(coinData);
        }
        return bos.toByteArray();
    } finally {
        if (bos != null) {
            bos.close();
        }
    }
}
 
源代码4 项目: pasm-yolov3-Android   文件: Utils.java
public static Mat loadResource(Context context, int resourceId, int flags) throws IOException
{
    InputStream is = context.getResources().openRawResource(resourceId);
    ByteArrayOutputStream os = new ByteArrayOutputStream(is.available());

    byte[] buffer = new byte[4096];
    int bytesRead;
    while ((bytesRead = is.read(buffer)) != -1) {
        os.write(buffer, 0, bytesRead);
    }
    is.close();

    Mat encoded = new Mat(1, os.size(), CvType.CV_8U);
    encoded.put(0, 0, os.toByteArray());
    os.close();

    Mat decoded = Imgcodecs.imdecode(encoded, flags);
    encoded.release();

    return decoded;
}
 
源代码5 项目: flink   文件: StringSerializationTest.java
public static final void testSerialization(String[] values) throws IOException {
	ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);
	DataOutputStream serializer = new DataOutputStream(baos);
	
	for (String value : values) {
		StringValue.writeString(value, serializer);
	}
	
	serializer.close();
	baos.close();
	
	ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
	DataInputStream deserializer = new DataInputStream(bais);
	
	int num = 0;
	while (deserializer.available() > 0) {
		String deser = StringValue.readString(deserializer);
		
		assertEquals("DeserializedString differs from original string.", values[num], deser);
		num++;
	}
	
	assertEquals("Wrong number of deserialized values", values.length, num);
}
 
public static void testSerialization(String[] values) throws IOException {
	ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);
	DataOutputViewStreamWrapper serializer = new DataOutputViewStreamWrapper(baos);
	
	for (String value : values) {
		StringValue sv = new StringValue(value);
		sv.write(serializer);
	}
	
	serializer.close();
	baos.close();
	
	ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
	DataInputViewStreamWrapper deserializer = new DataInputViewStreamWrapper(bais);
	
	int num = 0;
	while (bais.available() > 0) {
		StringValue deser = new StringValue();
		deser.read(deserializer);
		
		assertEquals("DeserializedString differs from original string.", values[num], deser.getValue());
		num++;
	}
	
	assertEquals("Wrong number of deserialized values", values.length, num);
}
 
源代码7 项目: onpc   文件: Utils.java
public static byte[] streamToByteArray(InputStream stream) throws IOException
{
    byte[] buffer = new byte[1024];
    ByteArrayOutputStream os = new ByteArrayOutputStream();

    int line;
    // read bytes from stream, and store them in buffer
    while ((line = stream.read(buffer)) != -1)
    {
        // Writes bytes from byte array (buffer) into output stream.
        os.write(buffer, 0, line);
    }
    os.flush();
    os.close();
    stream.close();
    return os.toByteArray();
}
 
源代码8 项目: kylin-on-parquet-v2   文件: CompressionUtils.java
public static byte[] compress(byte[] data) throws IOException {
    long startTime = System.currentTimeMillis();
    Deflater deflater = new Deflater(1);
    deflater.setInput(data);

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);

    deflater.finish();
    byte[] buffer = new byte[1024];
    while (!deflater.finished()) {
        int count = deflater.deflate(buffer); // returns the generated code... index
        outputStream.write(buffer, 0, count);
    }
    outputStream.close();
    byte[] output = outputStream.toByteArray();

    logger.debug("Original: " + data.length + " bytes. " + "Compressed: " + output.length + " byte. Time: " + (System.currentTimeMillis() - startTime));
    return output;
}
 
源代码9 项目: EasyHttp   文件: JsonUnitTest.java
/**
 * 获取资产目录下面文件的字符串
 */
private static String getAssetsString(Context context, String file) {
    try {
        InputStream inputStream = context.getAssets().open(file);
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[512];
        int length;
        while ((length = inputStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, length);
        }
        outStream.close();
        inputStream.close();
        return outStream.toString();
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}
 
源代码10 项目: kareldb   文件: KafkaKeySerializer.java
@Override
public byte[] serialize(String topic, Comparable[] object) {
    if (object == null) {
        return null;
    }
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        BinaryEncoder encoder = encoderFactory.directBinaryEncoder(out, null);
        writer.write(toRecord(object), encoder);
        encoder.flush();
        byte[] bytes = out.toByteArray();
        out.close();
        return bytes;
    } catch (IOException | RuntimeException e) {
        // avro serialization can throw AvroRuntimeException, NullPointerException,
        // ClassCastException, etc
        LOG.error("Error serializing Avro key " + e.getMessage());
        throw new SerializationException("Error serializing Avro key", e);
    }
}
 
源代码11 项目: zheshiyigeniubidexiangmu   文件: GZipUtils.java
/**
 * @param @param  unZipfile
 * @param @param  destFile 指定读取文件,需要从压缩文件中读取文件内容的文件名
 * @param @return 设定文件
 * @return String 返回类型
 * @throws
 * @Title: unZip
 * @Description: TODO(这里用一句话描述这个方法的作用)
 */
public static String unZip(String unZipfile, String destFile) {// unZipfileName需要解压的zip文件名
    InputStream inputStream;
    String inData = null;
    try {
        // 生成一个zip的文件
        File f = new File(unZipfile);
        ZipFile zipFile = new ZipFile(f);

        // 遍历zipFile中所有的实体,并把他们解压出来
        ZipEntry entry = zipFile.getEntry(destFile);
        if (!entry.isDirectory()) {
            // 获取出该压缩实体的输入流
            inputStream = zipFile.getInputStream(entry);

            ByteArrayOutputStream out = new ByteArrayOutputStream();
            byte[] bys = new byte[4096];
            for (int p = -1; (p = inputStream.read(bys)) != -1; ) {
                out.write(bys, 0, p);
            }
            inData = out.toString();
            out.close();
            inputStream.close();
        }
        zipFile.close();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
    return inData;
}
 
源代码12 项目: kogito-runtimes   文件: MarshallingIssuesTest.java
@Test
public void testJBRULES_1946_3() {
    KieBase kbase = loadKnowledgeBase("../Sample.drl" );

    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DroolsObjectOutputStream oos = new DroolsObjectOutputStream( baos );

        oos.writeObject( kbase );
        oos.flush();
        oos.close();
        baos.flush();
        baos.close();

        byte[] serializedKb = baos.toByteArray();

        ByteArrayInputStream bais = new ByteArrayInputStream( serializedKb );
        ObjectInputStream ois = new ObjectInputStream( bais );

        KieBase kb2 = (KieBase) ois.readObject();
        fail( "Should have raised an IllegalArgumentException since the kbase was serialized with a Drools Stream but deserialized with a regular stream" );
    } catch ( IllegalArgumentException ode ) {
        // success
    } catch ( Exception e ) {
        e.printStackTrace();
        fail( "Unexpected exception: " + e.getMessage() );
    }
}
 
源代码13 项目: zserio   文件: StringArrayTest.java
@Test
public void testConstructor() throws IOException
{
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final MemoryCacheImageOutputStream os = new MemoryCacheImageOutputStream(baos);
    os.writeUTF("");
    os.close();
    baos.close();
    final ByteArrayBitStreamReader in = new ByteArrayBitStreamReader(baos.toByteArray());
    stringArray = new StringArray(in, 1);
    assertEquals(1, stringArray.length());
    assertEquals("", stringArray.elementAt(0));
}
 
源代码14 项目: Data_Processor   文件: GzipUtil.java
public static byte[] compress(byte[] data) throws IOException {
	if (null== data|| 0== data.length) {
		return null;
	}
	ByteArrayOutputStream out= new ByteArrayOutputStream();
	GZIPOutputStream gzip= new GZIPOutputStream(out);
	gzip.write(data);
	gzip.finish();
	gzip.close();
	byte[] ret= out.toByteArray();
	out.close();
	return  ret;//out.toString("ISO-8859-1");
}
 
源代码15 项目: uracer-kotd   文件: Art.java
public static Texture getFlag (String countryCode) {
	String filename = countryCode + ".png";
	FileHandle zip = Gdx.files.internal("data/flags.zip");
	ZipInputStream zin = new ZipInputStream(zip.read());
	ZipEntry ze = null;
	try {
		while ((ze = zin.getNextEntry()) != null) {
			if (ze.getName().equals(filename)) {
				ByteArrayOutputStream streamBuilder = new ByteArrayOutputStream();
				int bytesRead;
				byte[] tempBuffer = new byte[8192 * 2];
				while ((bytesRead = zin.read(tempBuffer)) != -1) {
					streamBuilder.write(tempBuffer, 0, bytesRead);
				}

				Pixmap px = new Pixmap(streamBuilder.toByteArray(), 0, streamBuilder.size());

				streamBuilder.close();
				zin.close();

				boolean mipMap = false;
				Texture t = new Texture(px);

				if (mipMap) {
					t.setFilter(TextureFilter.MipMapLinearNearest, TextureFilter.Nearest);
				} else {
					t.setFilter(TextureFilter.Nearest, TextureFilter.Nearest);
				}

				px.dispose();
				return t;
			}
		}
	} catch (IOException e) {
	}

	return null;
}
 
源代码16 项目: quarkus   文件: JaxRSTestCase.java
@Test
public void testGzipConfig() throws Exception {
    //gzip.maxInput set to 10 and expects 413 status code
    ByteArrayOutputStream obj = new ByteArrayOutputStream(12);
    GZIPOutputStream gzip = new GZIPOutputStream(obj);
    gzip.write("1234567890AB".getBytes("UTF-8"));
    gzip.close();
    RestAssured.given()
            .header("Content-Encoding", "gzip")
            .body(obj.toByteArray())
            .post("/test/gzip")
            .then().statusCode(413);
    obj.close();
}
 
源代码17 项目: jdk8u60   文件: Test.java
private static String toString(Source response) throws TransformerException, IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.transform(response, new StreamResult(bos));
    bos.close();
    return new String(bos.toByteArray());
}
 
源代码18 项目: wallpaper   文件: MyService.java
public static byte[] readInputStream(InputStream inStream) throws Exception {
	ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
	byte[] buffer = new byte[1024 * 1024];
	int len = 0;
	while ((len = inStream.read(buffer)) != -1) {
		outSteam.write(buffer, 0, len);
	}
	outSteam.close();
	inStream.close();
	return outSteam.toByteArray();
}
 
源代码19 项目: org.hl7.fhir.core   文件: ParserBase.java
public byte[] composeBytes(DataType type, String typeName) throws IOException {
  ByteArrayOutputStream bytes = new ByteArrayOutputStream();
  compose(bytes, type, typeName);
  bytes.close();
  return bytes.toByteArray();
}
 
源代码20 项目: datawave   文件: BulkIngestMapFileLoaderTest.java
protected ByteArrayInputStream createMockInputStream(String[] additionalEntries) throws IOException {
    
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(baos));
    
    for (int index = 0; index < 3; index++) {
        
        bw.write(String.format("/flagged/file%d\n", index));
    }
    
    if (null != additionalEntries) {
        
        for (String entries : additionalEntries) {
            
            bw.write(entries);
        }
    }
    
    bw.close();
    baos.close();
    
    return new ByteArrayInputStream(baos.toByteArray());
}