@Override public int read(byte b[], int off, int len) throws IOException { int r = in.read(b, off, len); if(r>0) { int sp = space(); if(sp>0) side.write(b,off,Math.min(r, sp)); } return r; }
private ByteArrayOutputStream encodeToBytes() throws IOException { ByteArrayOutputStream buf = new ByteArrayOutputStream(); try (OutputStream gzos = new GZIPOutputStream(buf); ObjectOutputStream oos = JenkinsJVM.isJenkinsJVM() ? AnonymousClassWarnings.checkingObjectOutputStream(gzos) : new ObjectOutputStream(gzos)) { oos.writeObject(this); } ByteArrayOutputStream buf2 = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(new Base64OutputStream(buf2,true,-1,null)); try { buf2.write(PREAMBLE); if (JenkinsJVM.isJenkinsJVM()) { // else we are in another JVM and cannot sign; result will be ignored unless INSECURE byte[] mac = MAC.mac(buf.toByteArray()); dos.writeInt(- mac.length); // negative to differentiate from older form dos.write(mac); } dos.writeInt(buf.size()); buf.writeTo(dos); } finally { dos.close(); } buf2.write(POSTAMBLE); return buf2; }
@Override public void write(final byte[] ba) throws IOException { if (ba != null){ super.write(ba); } } };
written = baout.write(new ByteArrayInputStream(ref.toByteArray())); assertEquals(155, written); checkStreams(baout, ref);
@Override public int read() throws IOException { int i = in.read(); if(i>=0 && space()>0) side.write(i); return i; }
@Override public void write(final byte[] b, final int off, final int len) { numWrites.incrementAndGet(); super.write(b, off, len); } };
/** * Fetches entire contents of an <code>InputStream</code> and represent * same data as result InputStream. * <p> * This method is useful where, * <ul> * <li>Source InputStream is slow.</li> * <li>It has network resources associated, so we cannot keep it open for * long time.</li> * <li>It has network timeout associated.</li> * </ul> * It can be used in favor of {@link #toByteArray()}, since it * avoids unnecessary allocation and copy of byte[].<br> * This method buffers the input internally, so there is no need to use a * <code>BufferedInputStream</code>. * * @param input Stream to be fully buffered. * @param size the initial buffer size * @return A fully buffered stream. * @throws IOException if an I/O error occurs * @since 2.5 */ public static InputStream toBufferedInputStream(final InputStream input, final int size) throws IOException { // It does not matter if a ByteArrayOutputStream is not closed as close() is a no-op @SuppressWarnings("resource") final ByteArrayOutputStream output = new ByteArrayOutputStream(size); output.write(input); return output.toInputStream(); }
private int writeData(final ByteArrayOutputStream baout, final java.io.ByteArrayOutputStream ref, final int count) { if (count > DATA.length) { throw new IllegalArgumentException("Requesting too many bytes"); } if (count == 0) { baout.write(100); ref.write(100); return 1; } else { baout.write(DATA, 0, count); ref.write(DATA, 0, count); return count; } }
@Test public void testWriteNullBaSucceeds() throws Exception { final byte[] ba = null; original.write(ba); proxied.write(ba); } }
@Test(expectedExceptions = NtlmEngineException.class) public void testGenerateType3MsgThworsExceptionWhenType2IndicatorNotPresent() throws IOException { ByteArrayOutputStream buf = new ByteArrayOutputStream(); buf.write("NTLMSSP".getBytes(StandardCharsets.US_ASCII)); buf.write(0); // type 2 indicator buf.write(3); buf.write(0); buf.write(0); buf.write(0); buf.write("challenge".getBytes()); NtlmEngine engine = new NtlmEngine(); engine.generateType3Msg("username", "password", "localhost", "workstation", Base64.getEncoder().encodeToString(buf.toByteArray())); buf.close(); fail("An NtlmEngineException must have occurred as type 2 indicator is incorrect"); }
public HttpResponse sendResponse(HttpResponseStatus responseCode, String responseBody) { String actualResponseBody = responseBody + "\n"; HttpResponse response = new DefaultHttpResponse(HTTP_1_1, responseCode); response.setHeader(CONTENT_LENGTH, actualResponseBody.length()); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try { outputStream.write(actualResponseBody.getBytes()); } catch(IOException e) { logger.error("IOException while trying to write the outputStream for an admin response", e); throw new RuntimeException(e); } ChannelBuffer responseContent = ChannelBuffers.dynamicBuffer(); responseContent.writeBytes(outputStream.toByteArray()); response.setContent(responseContent); if (logger.isDebugEnabled()) { logger.debug("Sent " + response); } return response; }
blob = new ByteArrayOutputStream(n); blob.write(buf, 0, n); blobLength += n; if (blobLength >= maxBlobLength) {
@Test(expectedExceptions = NtlmEngineException.class) public void testGenerateType3MsgThrowsExceptionWhenUnicodeSupportNotIndicated() throws IOException { ByteArrayOutputStream buf = new ByteArrayOutputStream(); buf.write("NTLMSSP".getBytes(StandardCharsets.US_ASCII)); buf.write(0); // type 2 indicator buf.write(2); buf.write(0); buf.write(0); buf.write(0); buf.write(longToBytes(1L)); // we want to write a Long // flags buf.write(0);// unicode support indicator buf.write(0); buf.write(0); buf.write(0); buf.write(longToBytes(1L));// challenge NtlmEngine engine = new NtlmEngine(); engine.generateType3Msg("username", "password", "localhost", "workstation", Base64.getEncoder().encodeToString(buf.toByteArray())); buf.close(); fail("An NtlmEngineException must have occurred as unicode support is not indicated"); }
@Test public void testGenerateType3Msg() throws IOException { ByteArrayOutputStream buf = new ByteArrayOutputStream(); buf.write("NTLMSSP".getBytes(StandardCharsets.US_ASCII)); buf.write(0); // type 2 indicator buf.write(2); buf.write(0); buf.write(0); buf.write(0); buf.write(longToBytes(0L)); // we want to write a Long // flags buf.write(1);// unicode support indicator buf.write(0); buf.write(0); buf.write(0); buf.write(longToBytes(1L));// challenge NtlmEngine engine = new NtlmEngine(); String type3Msg = engine.generateType3Msg("username", "password", "localhost", "workstation", Base64.getEncoder().encodeToString(buf.toByteArray())); buf.close(); assertEquals( type3Msg, "TlRMTVNTUAADAAAAGAAYAEgAAAAYABgAYAAAABIAEgB4AAAAEAAQAIoAAAAWABYAmgAAAAAAAACwAAAAAQAAAgUBKAoAAAAP1g6lqqN1HZ0wSSxeQ5riQkyh7/UexwVlCPQm0SHU2vsDQm2wM6NbT2zPonPzLJL0TABPAEMAQQBMAEgATwBTAFQAdQBzAGUAcgBuAGEAbQBlAFcATwBSAEsAUwBUAEEAVABJAE8ATgA=", "Incorrect type3 message generated"); }
blob = new ByteArrayOutputStream(n); blob.write(buf, 0, n); blobLength += n; if (blobLength >= maxBlobLength) {
@Test public void testClose() throws IOException { shielded.close(); assertFalse("closed", closed); try { shielded.write('x'); fail("write(b)"); } catch (final IOException ignore) { // expected } original.write('y'); assertEquals(1, original.size()); assertEquals('y', original.toByteArray()[0]); }
@Test public void testAddDelete() throws Exception { final byte[] data = PtBytes.toBytes("streamed"); final long streamId = txManager.runTaskWithRetry((TransactionTask<Long, Exception>) t -> { Sha256Hash hash = Sha256Hash.computeHash(data); byte[] reference = "ref".getBytes(); return defaultStore.getByHashOrStoreStreamAndMarkAsUsed(t, hash, new ByteArrayInputStream(data), reference); }); txManager.runTaskWithRetry((TransactionTask<Void, Exception>) t -> { Optional<InputStream> inputStream = defaultStore.loadSingleStream(t, streamId); assertTrue(inputStream.isPresent()); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); outputStream.write(inputStream.get()); assertArrayEquals(data, outputStream.toByteArray()); return null; }); }
@Override public int read(byte b[], int off, int len) throws IOException { int r = in.read(b, off, len); if(r>0) { int sp = space(); if(sp>0) side.write(b,off,Math.min(r, sp)); } return r; }
@Override public int read() throws IOException { int i = in.read(); if(i>=0 && space()>0) side.write(i); return i; }
certDataStream.write(sslCert.getCert().getBytes());