1 /* 2 * Copyright (C) 2014 Square, Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package okio; 17 18 import java.io.IOException; 19 import java.io.OutputStream; 20 import java.nio.charset.Charset; 21 22 /** 23 * A sink that keeps a buffer internally so that callers can do small writes 24 * without a performance penalty. 25 */ 26 public interface BufferedSink extends Sink { 27 /** Returns this sink's internal buffer. */ buffer()28 Buffer buffer(); 29 write(ByteString byteString)30 BufferedSink write(ByteString byteString) throws IOException; 31 32 /** 33 * Like {@link OutputStream#write(byte[])}, this writes a complete byte array to 34 * this sink. 35 */ write(byte[] source)36 BufferedSink write(byte[] source) throws IOException; 37 38 /** 39 * Like {@link OutputStream#write(byte[], int, int)}, this writes {@code byteCount} 40 * bytes of {@code source}, starting at {@code offset}. 41 */ write(byte[] source, int offset, int byteCount)42 BufferedSink write(byte[] source, int offset, int byteCount) throws IOException; 43 44 /** 45 * Removes all bytes from {@code source} and appends them to this sink. Returns the 46 * number of bytes read which will be 0 if {@code source} is exhausted. 47 */ writeAll(Source source)48 long writeAll(Source source) throws IOException; 49 50 /** Removes {@code byteCount} bytes from {@code source} and appends them to this sink. */ write(Source source, long byteCount)51 BufferedSink write(Source source, long byteCount) throws IOException; 52 53 /** Encodes {@code string} in UTF-8 and writes it to this sink. */ writeUtf8(String string)54 BufferedSink writeUtf8(String string) throws IOException; 55 56 /** 57 * Encodes the characters at {@code beginIndex} up to {@code endIndex} from {@code string} in 58 * UTF-8 and writes it to this sink. 59 */ writeUtf8(String string, int beginIndex, int endIndex)60 BufferedSink writeUtf8(String string, int beginIndex, int endIndex) throws IOException; 61 62 /** Encodes {@code codePoint} in UTF-8 and writes it to this sink. */ writeUtf8CodePoint(int codePoint)63 BufferedSink writeUtf8CodePoint(int codePoint) throws IOException; 64 65 /** Encodes {@code string} in {@code charset} and writes it to this sink. */ writeString(String string, Charset charset)66 BufferedSink writeString(String string, Charset charset) throws IOException; 67 68 /** 69 * Encodes the characters at {@code beginIndex} up to {@code endIndex} from {@code string} in 70 * {@code charset} and writes it to this sink. 71 */ writeString(String string, int beginIndex, int endIndex, Charset charset)72 BufferedSink writeString(String string, int beginIndex, int endIndex, Charset charset) 73 throws IOException; 74 75 /** Writes a byte to this sink. */ writeByte(int b)76 BufferedSink writeByte(int b) throws IOException; 77 78 /** Writes a big-endian short to this sink using two bytes. */ writeShort(int s)79 BufferedSink writeShort(int s) throws IOException; 80 81 /** Writes a little-endian short to this sink using two bytes. */ writeShortLe(int s)82 BufferedSink writeShortLe(int s) throws IOException; 83 84 /** Writes a big-endian int to this sink using four bytes. */ writeInt(int i)85 BufferedSink writeInt(int i) throws IOException; 86 87 /** Writes a little-endian int to this sink using four bytes. */ writeIntLe(int i)88 BufferedSink writeIntLe(int i) throws IOException; 89 90 /** Writes a big-endian long to this sink using eight bytes. */ writeLong(long v)91 BufferedSink writeLong(long v) throws IOException; 92 93 /** Writes a little-endian long to this sink using eight bytes. */ writeLongLe(long v)94 BufferedSink writeLongLe(long v) throws IOException; 95 96 /** Writes a long to this sink in signed decimal form (i.e., as a string in base 10). */ writeDecimalLong(long v)97 BufferedSink writeDecimalLong(long v) throws IOException; 98 99 /** Writes a long to this sink in hexadecimal form (i.e., as a string in base 16). */ writeHexadecimalUnsignedLong(long v)100 BufferedSink writeHexadecimalUnsignedLong(long v) throws IOException; 101 102 /** 103 * Writes complete segments to the underlying sink, if one exists. Like {@link #flush}, but 104 * weaker. Use this to limit the memory held in the buffer to a single segment. 105 */ emitCompleteSegments()106 BufferedSink emitCompleteSegments() throws IOException; 107 108 /** 109 * Writes all buffered data to the underlying sink, if one exists. Like {@link #flush}, but 110 * weaker. Call this before this buffered sink goes out of scope so that its data can reach its 111 * destination. 112 */ emit()113 BufferedSink emit() throws IOException; 114 115 /** Returns an output stream that writes to this sink. */ outputStream()116 OutputStream outputStream(); 117 } 118