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