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 */
16package okio;
17
18import java.io.IOException;
19import java.io.OutputStream;
20import 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 */
26public interface BufferedSink extends Sink {
27  /** Returns this sink's internal buffer. */
28  Buffer buffer();
29
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   */
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   */
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   */
48  long writeAll(Source source) throws IOException;
49
50  /** Removes {@code byteCount} bytes from {@code source} and appends them to this sink. */
51  BufferedSink write(Source source, long byteCount) throws IOException;
52
53  /** Encodes {@code string} in UTF-8 and writes it to this sink. */
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   */
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. */
63  BufferedSink writeUtf8CodePoint(int codePoint) throws IOException;
64
65  /** Encodes {@code string} in {@code charset} and writes it to this sink. */
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   */
72  BufferedSink writeString(String string, int beginIndex, int endIndex, Charset charset)
73      throws IOException;
74
75  /** Writes a byte to this sink. */
76  BufferedSink writeByte(int b) throws IOException;
77
78  /** Writes a big-endian short to this sink using two bytes. */
79  BufferedSink writeShort(int s) throws IOException;
80
81  /** Writes a little-endian short to this sink using two bytes. */
82  BufferedSink writeShortLe(int s) throws IOException;
83
84  /** Writes a big-endian int to this sink using four bytes. */
85  BufferedSink writeInt(int i) throws IOException;
86
87  /** Writes a little-endian int to this sink using four bytes. */
88  BufferedSink writeIntLe(int i) throws IOException;
89
90  /** Writes a big-endian long to this sink using eight bytes. */
91  BufferedSink writeLong(long v) throws IOException;
92
93  /** Writes a little-endian long to this sink using eight bytes. */
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). */
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). */
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   */
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   */
113  BufferedSink emit() throws IOException;
114
115  /** Returns an output stream that writes to this sink. */
116  OutputStream outputStream();
117}
118