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 051@ElementTypesAreNonnullByDefault 052public abstract class ByteSink { 053 054 /** Constructor for use by subclasses. */ 055 protected ByteSink() {} 056 057 /** 058 * Returns a {@link CharSink} view of this {@code ByteSink} that writes characters to this sink as 059 * bytes encoded with the given {@link Charset charset}. 060 */ 061 public CharSink asCharSink(Charset charset) { 062 return new AsCharSink(charset); 063 } 064 065 /** 066 * Opens a new {@link OutputStream} for writing to this sink. This method returns a new, 067 * independent stream each time it is called. 068 * 069 * <p>The caller is responsible for ensuring that the returned stream is closed. 070 * 071 * @throws IOException if an I/O error occurs while opening the stream 072 */ 073 public abstract OutputStream openStream() throws IOException; 074 075 /** 076 * Opens a new buffered {@link OutputStream} for writing to this sink. The returned stream is not 077 * required to be a {@link BufferedOutputStream} in order to allow implementations to simply 078 * delegate to {@link #openStream()} when the stream returned by that method does not benefit from 079 * additional buffering (for example, a {@code ByteArrayOutputStream}). This method returns a new, 080 * independent stream each time it is called. 081 * 082 * <p>The caller is responsible for ensuring that the returned stream is closed. 083 * 084 * @throws IOException if an I/O error occurs while opening the stream 085 * @since 15.0 (in 14.0 with return type {@link BufferedOutputStream}) 086 */ 087 public OutputStream openBufferedStream() throws IOException { 088 OutputStream out = openStream(); 089 return (out instanceof BufferedOutputStream) 090 ? (BufferedOutputStream) out 091 : new BufferedOutputStream(out); 092 } 093 094 /** 095 * Writes all the given bytes to this sink. 096 * 097 * @throws IOException if an I/O occurs while writing to this sink 098 */ 099 public void write(byte[] bytes) throws IOException { 100 checkNotNull(bytes); 101 102 Closer closer = Closer.create(); 103 try { 104 OutputStream out = closer.register(openStream()); 105 out.write(bytes); 106 out.flush(); // https://code.google.com/p/guava-libraries/issues/detail?id=1330 107 } catch (Throwable e) { 108 throw closer.rethrow(e); 109 } finally { 110 closer.close(); 111 } 112 } 113 114 /** 115 * Writes all the bytes from the given {@code InputStream} to this sink. Does not close {@code 116 * input}. 117 * 118 * @return the number of bytes written 119 * @throws IOException if an I/O occurs while reading from {@code input} or writing to this sink 120 */ 121 @CanIgnoreReturnValue 122 public long writeFrom(InputStream input) throws IOException { 123 checkNotNull(input); 124 125 Closer closer = Closer.create(); 126 try { 127 OutputStream out = closer.register(openStream()); 128 long written = ByteStreams.copy(input, out); 129 out.flush(); // https://code.google.com/p/guava-libraries/issues/detail?id=1330 130 return written; 131 } catch (Throwable e) { 132 throw closer.rethrow(e); 133 } finally { 134 closer.close(); 135 } 136 } 137 138 /** 139 * A char sink that encodes written characters with a charset and writes resulting bytes to this 140 * byte sink. 141 */ 142 private final class AsCharSink extends CharSink { 143 144 private final Charset charset; 145 146 private AsCharSink(Charset charset) { 147 this.charset = checkNotNull(charset); 148 } 149 150 @Override 151 public Writer openStream() throws IOException { 152 return new OutputStreamWriter(ByteSink.this.openStream(), charset); 153 } 154 155 @Override 156 public String toString() { 157 return ByteSink.this.toString() + ".asCharSink(" + charset + ")"; 158 } 159 } 160}