Java Code Examples for java.io.ByteArrayOutputStream#close()
The following examples show how to use
java.io.ByteArrayOutputStream#close() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TelnetCodecTest.java From dubbo-2.6.5 with Apache License 2.0 | 6 votes |
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); }
Example 2
Source File: KryoSerializer.java From Lottor with MIT License | 6 votes |
/** * 序列化 * * @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; }
Example 3
Source File: KafkaKeySerializer.java From kareldb with Apache License 2.0 | 6 votes |
@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); } }
Example 4
Source File: Transaction.java From nuls-v2 with MIT License | 6 votes |
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(); } } }
Example 5
Source File: Utils.java From pasm-yolov3-Android with GNU General Public License v3.0 | 6 votes |
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; }
Example 6
Source File: StringSerializationTest.java From flink with Apache License 2.0 | 6 votes |
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); }
Example 7
Source File: JsonUnitTest.java From EasyHttp with Apache License 2.0 | 6 votes |
/** * 获取资产目录下面文件的字符串 */ 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; } }
Example 8
Source File: StringValueSerializationTest.java From Flink-CEPplus with Apache License 2.0 | 6 votes |
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); }
Example 9
Source File: Utils.java From onpc with GNU General Public License v3.0 | 6 votes |
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(); }
Example 10
Source File: CompressionUtils.java From kylin-on-parquet-v2 with Apache License 2.0 | 6 votes |
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; }
Example 11
Source File: GzipUtil.java From Data_Processor with Apache License 2.0 | 5 votes |
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"); }
Example 12
Source File: MyService.java From wallpaper with GNU General Public License v2.0 | 5 votes |
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(); }
Example 13
Source File: Test.java From jdk8u60 with GNU General Public License v2.0 | 5 votes |
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()); }
Example 14
Source File: JaxRSTestCase.java From quarkus with Apache License 2.0 | 5 votes |
@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(); }
Example 15
Source File: Art.java From uracer-kotd with Apache License 2.0 | 5 votes |
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; }
Example 16
Source File: StringArrayTest.java From zserio with BSD 3-Clause "New" or "Revised" License | 5 votes |
@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)); }
Example 17
Source File: MarshallingIssuesTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@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() ); } }
Example 18
Source File: GZipUtils.java From zheshiyigeniubidexiangmu with MIT License | 5 votes |
/** * @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; }
Example 19
Source File: ParserBase.java From org.hl7.fhir.core with Apache License 2.0 | 4 votes |
public byte[] composeBytes(DataType type, String typeName) throws IOException { ByteArrayOutputStream bytes = new ByteArrayOutputStream(); compose(bytes, type, typeName); bytes.close(); return bytes.toByteArray(); }
Example 20
Source File: BulkIngestMapFileLoaderTest.java From datawave with Apache License 2.0 | 4 votes |
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()); }