/** * Sends a serializable object. */ public void writeObject(Object o) throws IOException { ObjectOutputStream oos = AnonymousClassWarnings.checkingObjectOutputStream(out); oos.writeObject(o); // don't close oss, which will close the underlying stream // no need to flush either, given the way oos is implemented }
/** * {@inheritDoc} */ @Override public void write(byte[] b, int off, int len) throws IOException { queue.put(b, off, len); } }
/** * {@inheritDoc} */ @Override public void onRegistered(SelectionKey selectionKey) { future.set(selectionKey); }
@Test public void writeAll() throws Exception { String str = "AbCdEfGhIjKlMnOpQrStUvWxYz"; ByteBufferQueue queue = new ByteBufferQueue(10); ByteBufferQueueOutputStream instance = new ByteBufferQueueOutputStream(queue); instance.write(str.getBytes(StandardCharsets.UTF_8)); assertThat(read(queue), is(str)); }
@Test public void setValue() throws Exception { assertTrue(future.set("value")); assertCompletedFuture("value"); }
/** * {@inheritDoc} */ @Override public int read() throws IOException { return pos++ >= length ? -1 : (queue.get() & 0xff); }
/** * {@inheritDoc} */ @Override public void onClosedChannel(ClosedChannelException e) { future.setException(e); }
/** * SPI: Creates a new {@link ByteBuffer} optimally sized for network read/write operations. * * @return a new {@link ByteBuffer} optimally sized for network read/write operations. */ protected ByteBufferQueue newByteBufferQueue() { return new ByteBufferQueue(CAPACITY); }
/** * {@inheritDoc} */ @Override public int available() throws IOException { if (length == -1) { long remaining = queue.remaining(); return remaining > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) remaining; } else { return pos >= length ? -1 : length - pos; } }
@Test public void cancel() throws Exception { assertTrue(future.cancel(true)); assertCancelledFuture(); }
/** * {@inheritDoc} */ @Override public ByteBuffer acquire(int size) { return bufferPool.acquire(size); }
/** * {@inheritDoc} */ @Override public void release(ByteBuffer buffer) { bufferPool.release(buffer); }
@Override public boolean isBlacklisted(Class c) { AnonymousClassWarnings.check(c); return false; }
/** * Creates a new {@link SettableFuture}. * * @param <V> generic type of value. * @return a new {@link SettableFuture}. */ public static <V> SettableFuture<V> create() { return new SettableFuture<V>(); }
/** * {@inheritDoc} */ @Override public long skip(long n) throws IOException { if (pos >= length) { return -1; } if (pos + n >= length) { n = length - pos; } long skipped = queue.skip(n); pos += skipped; return skipped; }
/** * Check if a {@link PublicKey} is trusted. * * @param publicKey the key to check. * @return {@code true} if this instance trusts the specified public key. */ public boolean isTrusted(PublicKey publicKey) { synchronized (publicKeys) { for (PublicKey k : publicKeys) { if (KeyUtils.equals(publicKey, k)) { return true; } } return false; } }
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; }
/** * {@inheritDoc} */ @Override public void write(int b) throws IOException { queue.put((byte) b); }
public long writeHtmlTo(long start, Writer w) throws IOException { ConsoleAnnotationOutputStream<T> caw = new ConsoleAnnotationOutputStream<>( w, createAnnotator(Stapler.getCurrentRequest()), context, charset); long r = super.writeLogTo(start,caw); ByteArrayOutputStream baos = new ByteArrayOutputStream(); Cipher sym = PASSING_ANNOTATOR.encrypt(); ObjectOutputStream oos = AnonymousClassWarnings.checkingObjectOutputStream(new GZIPOutputStream(new CipherOutputStream(baos,sym))); oos.writeLong(System.currentTimeMillis()); // send timestamp to prevent a replay attack oos.writeObject(caw.getConsoleAnnotator()); oos.close(); StaplerResponse rsp = Stapler.getCurrentResponse(); if (rsp!=null) rsp.setHeader("X-ConsoleAnnotator", new String(Base64.encode(baos.toByteArray()))); return r; }