@Override public OutputStream openStream() { return ByteStreams.nullOutputStream(); } }
private static void skipRequestBody(HttpServletRequest request) throws IOException { // If we send the challenge without consuming the body of the request, // the server will close the connection after sending the response. // The client may interpret this as a failed request and not resend the // request with the authentication header. We can avoid this behavior // in the client by reading and discarding the entire body of the // unauthenticated request before sending the response. try (InputStream inputStream = request.getInputStream()) { copy(inputStream, nullOutputStream()); } } }
@Override public void close() throws IOException { // Jersey will close the stream and release the connection after we read all the data. // We cannot call the stream's close method because it an instance of UncloseableInputStream, // where close is a no-op. copy(stream, nullOutputStream()); } }
@Override public void close() throws IOException { // Jersey will close the stream and release the connection after we read all the data. // We cannot call the stream's close method because it an instance of UncloseableInputStream, // where close is a no-op. copy(stream, nullOutputStream()); } }
public void testNullOutputStream() throws Exception { // create a null output stream OutputStream nos = ByteStreams.nullOutputStream(); // write to the output stream nos.write('n'); String test = "Test string for NullOutputStream"; nos.write(test.getBytes()); nos.write(test.getBytes(), 2, 10); // nothing really to assert? assertSame(ByteStreams.nullOutputStream(), ByteStreams.nullOutputStream()); }
private void consumeInputStreams(HttpURLConnection httpURLConnection) throws IOException { Closer closer = Closer.create(); try { InputStream in = closer.register(httpURLConnection.getInputStream()); InputStream err = closer.register(httpURLConnection.getErrorStream()); copy(in, nullOutputStream()); if (err != null) copy(err, nullOutputStream()); } catch (Throwable t) { throw closer.rethrow(t); } finally { closer.close(); } }
public TestWriter(TestOption... options) throws IOException { this(new TestOutputStream(ByteStreams.nullOutputStream(), options)); }
System.setOut(new PrintStream(nullOutputStream())); System.setErr(new PrintStream(nullOutputStream()));
/** * Decompresses the file to obtain the diff ID. * * @param compressedFile the file containing the compressed contents * @return the digest of the decompressed file * @throws IOException if an I/O exception occurs */ private static DescriptorDigest getDiffIdByDecompressingFile(Path compressedFile) throws IOException { try (CountingDigestOutputStream diffIdCaptureOutputStream = new CountingDigestOutputStream(ByteStreams.nullOutputStream())) { try (InputStream fileInputStream = new BufferedInputStream(Files.newInputStream(compressedFile)); GZIPInputStream decompressorStream = new GZIPInputStream(fileInputStream)) { ByteStreams.copy(decompressorStream, diffIdCaptureOutputStream); } return diffIdCaptureOutputStream.toBlobDescriptor().getDigest(); } }
@SuppressWarnings("UseOfSystemOutOrSystemErr") public static void initializeLogging(boolean debug) { // unhook out and err while initializing logging or logger will print to them PrintStream out = System.out; PrintStream err = System.err; try { if (debug) { Logging logging = Logging.initialize(); logging.configure(new LoggingConfiguration()); logging.setLevel("com.facebook.presto", Level.DEBUG); } else { System.setOut(new PrintStream(nullOutputStream())); System.setErr(new PrintStream(nullOutputStream())); Logging logging = Logging.initialize(); logging.configure(new LoggingConfiguration()); logging.disableConsole(); } } catch (IOException e) { throw new UncheckedIOException(e); } finally { System.setOut(out); System.setErr(err); } } }
/** * Creates a new instance of {@link UfsJournalLogWriter}. * * @param journal the handle to the journal * @param nextSequenceNumber the sequence number to begin writing at */ UfsJournalLogWriter(UfsJournal journal, long nextSequenceNumber) throws IOException { mJournal = Preconditions.checkNotNull(journal, "journal"); mUfs = mJournal.getUfs(); mNextSequenceNumber = nextSequenceNumber; mMaxLogSize = ServerConfiguration.getBytes(PropertyKey.MASTER_JOURNAL_LOG_SIZE_BYTES_MAX); mRotateLogForNextWrite = true; UfsJournalFile currentLog = UfsJournalSnapshot.getCurrentLog(mJournal); if (currentLog != null) { mJournalOutputStream = new JournalOutputStream(currentLog, ByteStreams.nullOutputStream()); } mGarbageCollector = new UfsJournalGarbageCollector(mJournal); mEntriesToFlush = new ArrayDeque<>(); }
/** * Generates a selector for the list of {@link LayerEntry}s. The selector is unique to each unique * set of layer entries, regardless of order. TODO: Should we care about order? * * @param layerEntries the layer entries * @return the selector * @throws IOException if an I/O exception occurs */ static DescriptorDigest generateSelector(ImmutableList<LayerEntry> layerEntries) throws IOException { return JsonTemplateMapper.toBlob(toSortedJsonTemplates(layerEntries)) .writeTo(ByteStreams.nullOutputStream()) .getDigest(); }
/** * Gets a {@link BuildResult} from an {@link Image}. * * @param image the image * @param targetFormat the target format of the image * @return a new {@link BuildResult} with the image's digest and id * @throws IOException if writing the digest or container configuration fails */ static BuildResult fromImage( Image<Layer> image, Class<? extends BuildableManifestTemplate> targetFormat) throws IOException { ImageToJsonTranslator imageToJsonTranslator = new ImageToJsonTranslator(image); BlobDescriptor containerConfigurationBlobDescriptor = imageToJsonTranslator .getContainerConfigurationBlob() .writeTo(ByteStreams.nullOutputStream()); BuildableManifestTemplate manifestTemplate = imageToJsonTranslator.getManifestTemplate( targetFormat, containerConfigurationBlobDescriptor); DescriptorDigest imageDigest = JsonTemplateMapper.toBlob(manifestTemplate) .writeTo(ByteStreams.nullOutputStream()) .getDigest(); DescriptorDigest imageId = containerConfigurationBlobDescriptor.getDigest(); return new BuildResult(imageDigest, imageId); }
@Override public DescriptorDigest handleResponse(Response response) throws IOException { // Checks if the image digest is as expected. DescriptorDigest expectedDigest = JsonTemplateMapper.toBlob(manifestTemplate) .writeTo(ByteStreams.nullOutputStream()) .getDigest(); List<String> receivedDigests = response.getHeader(RESPONSE_DIGEST_HEADER); if (receivedDigests.size() == 1) { try { DescriptorDigest receivedDigest = DescriptorDigest.fromDigest(receivedDigests.get(0)); if (expectedDigest.equals(receivedDigest)) { return expectedDigest; } } catch (DigestException ex) { // Invalid digest. } } // The received digest is not as expected. Warns about this. eventDispatcher.dispatch( LogEvent.warn(makeUnexpectedImageDigestWarning(expectedDigest, receivedDigests))); return expectedDigest; }
/** * Loads a file into Alluxio by reading it. * * @param fs a {@link FileSystem} * @param fileName the name of the file to load */ public static void loadFile(FileSystem fs, String fileName) { try (FileInStream is = fs.openFile(new AlluxioURI(fileName), OpenFilePOptions.newBuilder().setReadType(ReadPType.CACHE).build())) { IOUtils.copy(is, ByteStreams.nullOutputStream()); } catch (IOException | AlluxioException e) { throw new RuntimeException(e); } }
public void testCopyToStream_doesNotCloseThatStream() throws IOException { TestOutputStream out = new TestOutputStream(ByteStreams.nullOutputStream()); assertFalse(out.closed()); source.copyTo(out); assertFalse(out.closed()); }
public void testClosesOnErrors_copyingToOutputStreamThatThrows() { TestByteSource okSource = new TestByteSource(bytes); try { OutputStream out = new TestOutputStream(ByteStreams.nullOutputStream(), WRITE_THROWS); okSource.copyTo(out); fail(); } catch (IOException expected) { } assertTrue(okSource.wasStreamClosed()); }
private PushBlobStep afterBuildConfigurationFutureFuture() throws ExecutionException, IOException { try (ProgressEventDispatcher progressEventDispatcher = progressEventDispatcherFactory.create( BuildStepType.PUSH_CONTAINER_CONFIGURATION, "pushing container configuration", 1); TimerEventDispatcher ignored = new TimerEventDispatcher(buildConfiguration.getEventDispatcher(), DESCRIPTION)) { Image<Layer> image = NonBlockingSteps.get(NonBlockingSteps.get(buildImageStep)); Blob containerConfigurationBlob = new ImageToJsonTranslator(image).getContainerConfigurationBlob(); BlobDescriptor blobDescriptor = containerConfigurationBlob.writeTo(ByteStreams.nullOutputStream()); return new PushBlobStep( listeningExecutorService, buildConfiguration, progressEventDispatcher.newChildProducer(), authenticatePushStep, blobDescriptor, containerConfigurationBlob, BuildStepType.PUSH_CONTAINER_CONFIGURATION); } } }
new Duration(2, MINUTES))); try (Query query = queryRunner.startQuery("first query will introduce a cookie")) { query.renderOutput(new PrintStream(nullOutputStream()), CSV, false); query.renderOutput(new PrintStream(nullOutputStream()), CSV, false);
.writeTo(ByteStreams.nullOutputStream()) .getDigest(); DescriptorDigest imageId = containerConfigurationBlobDescriptor.getDigest();