window.ENTITIES={'/api/snippets/java/java%20-%20convert%20outputstream%20to%20inputstream':[{"result":true,"message":null,"batch":{"type":"java","name":"java - convert outputstream to inputstream","items":[{"id":"1GRLqj","type":"java","name":"Java - convert OutputStream to InputStream","content":"// Note: in the example, InputStream waits until data is available.\n\n\nimport java.io.InputStream;\nimport java.io.OutputStream;\nimport java.io.IOException;\nimport java.util.concurrent.BlockingQueue;\nimport java.util.concurrent.LinkedBlockingDeque;\n\npublic class Program {\n\n public static void main(String[] args) throws IOException {\n\n BlockingQueue queue = new LinkedBlockingDeque<>();\n OutputStream outputStream = new OutputStream() {\n @Override\n public void write(int value) throws IOException {\n queue.add(value);\n }\n };\n InputStream inputStream = new InputStream() {\n @Override\n public int read() throws IOException {\n try {\n return queue.take(); // returns byte or waits until byte is available\n } catch (InterruptedException e) {\n throw new IOException(\"Input stream interrupted.\", e);\n }\n }\n };\n\n outputStream.write(new byte[] {1, 2, 3, 4, 5});\n\n byte[] inputData = new byte[5]; // {0, 0, 0, 0, 0}\n int readCount = inputStream.read(inputData); // 5\n // {1, 2, 3, 4, 5}\n }\n}\n\n\n\n// Hint: to read and return only available data without waiting use this source code as InputStream:\n/*\n InputStream inputStream = new InputStream() {\n @Override\n public int read() throws IOException {\n Integer value = queue.poll();\n if (value == null) {\n return -1;\n }\n return value;\n }\n };\n*/","source":"","author":{"id":"r0erGD","name":"Huzaifa-Ball","avatar":"1629140956240__r0erGD__w40px_h40px.jpg","points":475,"role":"BASIC"},"creationTime":1696852511000,"updateTime":1697306478000,"removalTime":null},{"id":"jMgRxD","type":"java","name":"Java - convert OutputStream to InputStream","content":"// Note: in the example, InputStream waits until data is available.\n\n\n// ------------------------------------------------------------\n// Program.java file:\n// ------------------------------------------------------------\n\nimport java.io.InputStream;\nimport java.io.OutputStream;\nimport java.io.IOException;\n\npublic class Program {\n\n public static void main(String[] args) throws IOException {\n\n CombinedStream combinedStream = new CombinedStream();\n\n OutputStream outputStream = combinedStream.getOutputStream();\n InputStream inputStream = combinedStream.getInputStream();\n\n outputStream.write(new byte[] {1, 2, 3, 4, 5});\n\n byte[] inputData = new byte[5]; // {0, 0, 0, 0, 0}\n int readCount = inputStream.read(inputData); // 5\n // {1, 2, 3, 4, 5}\n }\n}\n\n\n\n// ------------------------------------------------------------\n// CombinedStream.java file:\n// ------------------------------------------------------------\n\nimport java.io.InputStream;\nimport java.io.OutputStream;\nimport java.io.IOException;\nimport java.util.concurrent.BlockingQueue;\nimport java.util.concurrent.LinkedBlockingDeque;\n\npublic static class CombinedStream {\n\n private BlockingQueue queue = new LinkedBlockingDeque<>();\n\n private OutputStream outputStream = null;\n private InputStream inputStream = null;\n\n public OutputStream getOutputStream() {\n if (this.outputStream == null) {\n this.outputStream = new OutputStream() {\n @Override\n public void write(int value) throws IOException {\n queue.add(value);\n }\n };\n }\n return this.outputStream;\n }\n\n public InputStream getInputStream() {\n if (this.inputStream == null) {\n this.inputStream = new InputStream() {\n @Override\n public int read() throws IOException {\n try {\n return queue.take(); // returns byte or waits until byte is available\n } catch (InterruptedException e) {\n throw new IOException(\"Input stream interrupted.\", e);\n }\n }\n };\n }\n return this.inputStream;\n }\n}\n\n\n\n// Hint: to read and return only available data without waiting use this source code as InputStream:\n/*\n public InputStream getInputStream() {\n if (this.inputStream == null) {\n this.inputStream = new InputStream() {\n @Override\n public int read() throws IOException {\n Integer value = queue.poll();\n if (value == null) {\n return -1;\n }\n return value;\n }\n };\n }\n return this.inputStream;\n }\n*/","source":"","author":{"id":"VDpkz0","name":"Abel-Burks","avatar":"1629130737090__VDpkz0__w40px_h40px.jpg","points":758,"role":"BASIC"},"creationTime":1696853002000,"updateTime":1697306595000,"removalTime":null}]}}]};