001/* 002 * Copyright (C) 2012 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 005 * in compliance with the License. You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software distributed under the License 010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 011 * or implied. See the License for the specific language governing permissions and limitations under 012 * the License. 013 */ 014 015package com.google.common.io; 016 017import static com.google.common.base.Preconditions.checkNotNull; 018 019import com.google.common.annotations.GwtIncompatible; 020import com.google.common.annotations.J2ktIncompatible; 021import com.google.errorprone.annotations.CanIgnoreReturnValue; 022import java.io.BufferedOutputStream; 023import java.io.IOException; 024import java.io.InputStream; 025import java.io.OutputStream; 026import java.io.OutputStreamWriter; 027import java.io.Writer; 028import java.nio.charset.Charset; 029 030/** 031 * A destination to which bytes can be written, such as a file. Unlike an {@link OutputStream}, a 032 * {@code ByteSink} is not an open, stateful stream that can be written to and closed. Instead, it 033 * is an immutable <i>supplier</i> of {@code OutputStream} instances. 034 * 035 * <p>{@code ByteSink} provides two kinds of methods: 036 * 037 * <ul> 038 * <li><b>Methods that return a stream:</b> These methods should return a <i>new</i>, independent 039 * instance each time they are called. The caller is responsible for ensuring that the 040 * returned stream is closed. 041 * <li><b>Convenience methods:</b> These are implementations of common operations that are 042 * typically implemented by opening a stream using one of the methods in the first category, 043 * doing something and finally closing the stream or channel that was opened. 044 * </ul> 045 * 046 * @since 14.0 047 * @author Colin Decker 048 */ 049@J2ktIncompatible 050@GwtIncompatible 051public abstract class ByteSink { 052 053 /** Constructor for use by subclasses. */ 054 protected ByteSink() {} 055 056 /** 057 * Returns a {@link CharSink} view of this {@code ByteSink} that writes characters to this sink as 058 * bytes encoded with the given {@link Charset charset}. 059 */ 060 public CharSink asCharSink(Charset charset) { 061 return new AsCharSink(charset); 062 } 063 064 /** 065 * Opens a new {@link OutputStream} for writing to this sink. This method returns a new, 066 * independent stream each time it is called. 067 * 068 * <p>The caller is responsible for ensuring that the returned stream is closed. 069 * 070 * @throws IOException if an I/O error occurs while opening the stream 071 */ 072 public abstract OutputStream openStream() throws IOException; 073 074 /** 075 * Opens a new buffered {@link OutputStream} for writing to this sink. The returned stream is not 076 * required to be a {@link BufferedOutputStream} in order to allow implementations to simply 077 * delegate to {@link #openStream()} when the stream returned by that method does not benefit from 078 * additional buffering (for example, a {@code ByteArrayOutputStream}). This method returns a new, 079 * independent stream each time it is called. 080 * 081 * <p>The caller is responsible for ensuring that the returned stream is closed. 082 * 083 * @throws IOException if an I/O error occurs while opening the stream 084 * @since 15.0 (in 14.0 with return type {@link BufferedOutputStream}) 085 */ 086 public OutputStream openBufferedStream() throws IOException { 087 OutputStream out = openStream(); 088 return (out instanceof BufferedOutputStream) 089 ? (BufferedOutputStream) out 090 : new BufferedOutputStream(out); 091 } 092 093 /** 094 * Writes all the given bytes to this sink. 095 * 096 * @throws IOException if an I/O occurs while writing to this sink 097 */ 098 public void write(byte[] bytes) throws IOException { 099 checkNotNull(bytes); 100 101 try (OutputStream out = openStream()) { 102 out.write(bytes); 103 } 104 } 105 106 /** 107 * Writes all the bytes from the given {@code InputStream} to this sink. Does not close {@code 108 * input}. 109 * 110 * @return the number of bytes written 111 * @throws IOException if an I/O occurs while reading from {@code input} or writing to this sink 112 */ 113 @CanIgnoreReturnValue 114 public long writeFrom(InputStream input) throws IOException { 115 checkNotNull(input); 116 117 try (OutputStream out = openStream()) { 118 return ByteStreams.copy(input, out); 119 } 120 } 121 122 /** 123 * A char sink that encodes written characters with a charset and writes resulting bytes to this 124 * byte sink. 125 */ 126 private final class AsCharSink extends CharSink { 127 128 private final Charset charset; 129 130 private AsCharSink(Charset charset) { 131 this.charset = checkNotNull(charset); 132 } 133 134 @Override 135 public Writer openStream() throws IOException { 136 return new OutputStreamWriter(ByteSink.this.openStream(), charset); 137 } 138 139 @Override 140 public String toString() { 141 return ByteSink.this.toString() + ".asCharSink(" + charset + ")"; 142 } 143 } 144}