ByteArrayOutputStream baos = new ByteArrayOutputStream(); org.apache.commons.io.IOUtils.copy(in, baos); byte[] bytes = baos.toByteArray(); // either while (needToReadAgain) { ByteArrayInputStream bais = new ByteArrayInputStream(bytes); read(bais); } // or ByteArrayInputStream bais = new ByteArrayInputStream(bytes); while (needToReadAgain) { bais.reset(); read(bais); }
@Override public synchronized void reset() { getByteArrayInputStream().reset(); }
@Override public void close() throws IOException { getByteArrayInputStream().reset(); // According to the Java documentation this does nothing, but just in case getByteArrayInputStream().close(); }
inMemoryStream.reset(); InputStream responseStream; if (formatName.toLowerCase().equals("png")) {
@Override public Form getFormParameters() { if (MediaTypes.typeEquals(MediaType.APPLICATION_FORM_URLENCODED_TYPE, getMediaType())) { InputStream in = getEntityInputStream(); if (in.getClass() != ByteArrayInputStream.class) { // Buffer input ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); try { ReaderWriter.writeTo(in, byteArrayOutputStream); } catch (IOException e) { throw new IllegalArgumentException(e); } in = new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); setEntityInputStream(in); } ByteArrayInputStream byteArrayInputStream = (ByteArrayInputStream) in; Form f = getEntity(Form.class); byteArrayInputStream.reset(); return f; } else { return new Form(); } }
public static void createTestFiles(RemoteFileTemplate<LsEntry> template, final String... fileNames) { if (template != null) { final ByteArrayInputStream stream = new ByteArrayInputStream("foo".getBytes()); template.execute((SessionCallback<LsEntry, Void>) session -> { try { session.mkdir("si.sftp.sample"); } catch (Exception e) { assertThat(e.getMessage(), containsString("failed to create")); } for (int i = 0; i < fileNames.length; i++) { stream.reset(); session.write(stream, "si.sftp.sample/" + fileNames[i]); } return null; }); } }
lastException = null; if (mode > 0) { bis.reset(); iis.close(); iis = new MemoryCacheImageInputStream(bis);
/** Try reading an object from the stream */ private Object readObject(ByteArrayInputStream bais, boolean unzip) throws IOException, ClassNotFoundException { bais.reset(); InputStream in = bais; if (unzip) in = new InflaterInputStream(in); final ObjectInputStream ois = new ObjectInputStream(in); return ois.readObject(); } };
bais.reset(); CreateTxnV0 createv0 = new CreateTxnV0(); createv0.deserialize(ia, "txn");
bais.reset(); CreateTxnV0 createv0 = new CreateTxnV0(); createv0.deserialize(ia, "txn");
private static InputStream copySchemaSource(String url, InputStream bytes, StscState state) { //Copy the schema file if it wasn't already copied if (state.getSchemasDir() == null) return bytes; String schemalocation = state.sourceNameForUri(url); File targetFile = new File(state.getSchemasDir(), schemalocation); if (targetFile.exists()) return bytes; try { File parentDir = new File(targetFile.getParent()); IOUtil.createDir(parentDir, null); ByteArrayInputStream bais = copy(bytes); FileOutputStream out = new FileOutputStream(targetFile); IOUtil.copyCompletely(bais, out); bais.reset(); return bais; } catch (IOException e) { System.err.println("IO Error " + e); return bytes; } }
(ByteArrayInputStream)inputStream; byteArrayInputStream.reset();
peekByte = (byte) peekInt; stream.reset(); // Reset back to the last known position without 0x00 or 0xFF stream.reset(); byte[] lengthBytes = new byte[posBefore - posAfter]; stream.reset(); stream.read(valueBytes, 0, len); length = len; peekByte = (byte) peekInt; stream.reset(); // Reset back to the last known position without 0x00 or 0xFF
/** * @throws IOException * @see OpenmrsUtil#copyFile(InputStream, OutputStream) */ @Test public void copyFile_shouldCopyInputstreamToOutputstreamAndCloseTheOutputstream() throws IOException { String exampleInputStreamString = "ExampleInputStream"; ByteArrayInputStream expectedByteArrayInputStream = new ByteArrayInputStream(exampleInputStreamString.getBytes()); ByteArrayOutputStream output = spy(new ByteArrayOutputStream()); OpenmrsUtil.copyFile(expectedByteArrayInputStream, output); expectedByteArrayInputStream.reset(); ByteArrayInputStream byteArrayInputStreamFromOutputStream = new ByteArrayInputStream(output.toByteArray()); assertTrue(IOUtils.contentEquals(expectedByteArrayInputStream, byteArrayInputStreamFromOutputStream)); verify(output, times(1)).close(); }
continue; stream.reset();
@Test public void testDownloadFile() throws Exception { RuleDatabaseItemUpdateRunnable itemUpdateRunnable = new RuleDatabaseItemUpdateRunnable(realTask, mockContext, mock(Configuration.Item.class)); byte[] bar = new byte[]{'h', 'a', 'l', 'l', 'o'}; final ByteArrayInputStream bis = new ByteArrayInputStream(bar); final ByteArrayOutputStream bos = new ByteArrayOutputStream(); FileOutputStream fos = mock(FileOutputStream.class); when(fos, "write", any(byte[].class), anyInt(), anyInt()).then(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocation) throws Throwable { byte[] buffer = invocation.getArgumentAt(0, byte[].class); int off = invocation.getArgumentAt(1, Integer.class); int len = invocation.getArgumentAt(2, Integer.class); bos.write(buffer, off, len); return null; } }); when(connection.getInputStream()).thenReturn(bis); when(singleWriterMultipleReaderFile.startWrite()).thenReturn(fos); itemUpdateRunnable.downloadFile(file, singleWriterMultipleReaderFile, connection); assertArrayEquals(bar, bos.toByteArray()); assertEquals(0, failAnswer.numCalls); assertEquals(1, finishAnswer.numCalls); bis.reset(); bos.reset(); }
byteStream.reset();
@Test public void testCRLF() throws IOException { ByteArrayCrLfSerializer deser = new ByteArrayCrLfSerializer(); deser.setPoolSize(2); ByteArrayInputStream bais = new ByteArrayInputStream("foo\r\n".getBytes()); for (int i = 0; i < 5; i++) { bais.reset(); byte[] bytes = deser.deserialize(bais); assertEquals("foo", new String(bytes)); } try { deser.deserialize(bais); fail("Expected SoftEndOfStreamException"); } catch (SoftEndOfStreamException e) { // expected } assertEquals(1, TestUtils.getPropertyValue(deser, "pool.allocated", Set.class).size()); assertEquals(0, TestUtils.getPropertyValue(deser, "pool.inUse", Set.class).size()); }
countDown = count; active = true; inputStream.reset(); int offset = framePosition*format.getFrameSize(); try { inputStream.reset(); inputStream.read(new byte[offset]); } catch(Exception e) { if (framePosition>=loopPointEnd) { framePosition = loopPointStart; inputStream.reset(); countDown--; logger.log(Level.FINEST, byte[] temp = getEveryNthFrame(tempData, 2); newData = reverseFrames(temp); inputStream.reset(); totalBytes -= 2*bytesRead; framePosition -= 2*framesRead;
/** * Resets the buffer to the marked position. The marked position * is 0 unless another position was marked or an offset was specified * in the constructor. */ public static void reset(InputStream is) throws IOException { ensureIsByteArrayInputStream(is); ((ByteArrayInputStream) is).reset(); }