BufferedSink.java revision e78f117bcbd6b57d783737107f445ef75ecb474a
13c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller/*
23c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * Copyright (C) 2014 Square, Inc.
33c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller *
43c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * Licensed under the Apache License, Version 2.0 (the "License");
53c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * you may not use this file except in compliance with the License.
63c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * You may obtain a copy of the License at
73c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller *
83c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller *      http://www.apache.org/licenses/LICENSE-2.0
93c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller *
103c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * Unless required by applicable law or agreed to in writing, software
113c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * distributed under the License is distributed on an "AS IS" BASIS,
123c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
133c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * See the License for the specific language governing permissions and
143c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * limitations under the License.
153c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller */
163c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerpackage okio;
173c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller
183c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport java.io.IOException;
193c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerimport java.io.OutputStream;
20e78f117bcbd6b57d783737107f445ef75ecb474aNeil Fullerimport java.nio.charset.Charset;
213c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller
223c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller/**
233c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * A sink that keeps a buffer internally so that callers can do small writes
243c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * without a performance penalty.
253c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller */
263c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerpublic interface BufferedSink extends Sink {
273c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  /** Returns this sink's internal buffer. */
28e78f117bcbd6b57d783737107f445ef75ecb474aNeil Fuller  Buffer buffer();
293c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller
303c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  BufferedSink write(ByteString byteString) throws IOException;
313c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller
323c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  /**
33e78f117bcbd6b57d783737107f445ef75ecb474aNeil Fuller   * Like {@link OutputStream#write(byte[])}, this writes a complete byte array to
34e78f117bcbd6b57d783737107f445ef75ecb474aNeil Fuller   * this sink.
353c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller   */
363c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  BufferedSink write(byte[] source) throws IOException;
373c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller
383c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  /**
39e78f117bcbd6b57d783737107f445ef75ecb474aNeil Fuller   * Like {@link OutputStream#write(byte[], int, int)}, this writes {@code byteCount}
40e78f117bcbd6b57d783737107f445ef75ecb474aNeil Fuller   * bytes of {@code source}, starting at {@code offset}.
413c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller   */
423c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  BufferedSink write(byte[] source, int offset, int byteCount) throws IOException;
433c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller
44e78f117bcbd6b57d783737107f445ef75ecb474aNeil Fuller  /**
45e78f117bcbd6b57d783737107f445ef75ecb474aNeil Fuller   * Removes all bytes from {@code source} and appends them to this sink. Returns the
46e78f117bcbd6b57d783737107f445ef75ecb474aNeil Fuller   * number of bytes read which will be 0 if {@code source} is exhausted.
47e78f117bcbd6b57d783737107f445ef75ecb474aNeil Fuller   */
48e78f117bcbd6b57d783737107f445ef75ecb474aNeil Fuller  long writeAll(Source source) throws IOException;
49e78f117bcbd6b57d783737107f445ef75ecb474aNeil Fuller
50e78f117bcbd6b57d783737107f445ef75ecb474aNeil Fuller  /** Removes {@code byteCount} bytes from {@code source} and appends them to this sink. */
51e78f117bcbd6b57d783737107f445ef75ecb474aNeil Fuller  BufferedSink write(Source source, long byteCount) throws IOException;
52e78f117bcbd6b57d783737107f445ef75ecb474aNeil Fuller
533c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  /** Encodes {@code string} in UTF-8 and writes it to this sink. */
543c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  BufferedSink writeUtf8(String string) throws IOException;
553c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller
56e78f117bcbd6b57d783737107f445ef75ecb474aNeil Fuller  /** Encodes {@code string} in {@code charset} and writes it to this sink. */
57e78f117bcbd6b57d783737107f445ef75ecb474aNeil Fuller  BufferedSink writeString(String string, Charset charset) throws IOException;
58e78f117bcbd6b57d783737107f445ef75ecb474aNeil Fuller
59c6bd683320121544811f481709b3fdbcbe9b3866Neil Fuller  /** Writes a byte to this sink. */
603c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  BufferedSink writeByte(int b) throws IOException;
613c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller
62c6bd683320121544811f481709b3fdbcbe9b3866Neil Fuller  /** Writes a big-endian short to this sink using two bytes. */
633c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  BufferedSink writeShort(int s) throws IOException;
643c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller
65c6bd683320121544811f481709b3fdbcbe9b3866Neil Fuller  /** Writes a little-endian short to this sink using two bytes. */
66c6bd683320121544811f481709b3fdbcbe9b3866Neil Fuller  BufferedSink writeShortLe(int s) throws IOException;
67c6bd683320121544811f481709b3fdbcbe9b3866Neil Fuller
68c6bd683320121544811f481709b3fdbcbe9b3866Neil Fuller  /** Writes a big-endian int to this sink using four bytes. */
693c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  BufferedSink writeInt(int i) throws IOException;
703c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller
71c6bd683320121544811f481709b3fdbcbe9b3866Neil Fuller  /** Writes a little-endian int to this sink using four bytes. */
72c6bd683320121544811f481709b3fdbcbe9b3866Neil Fuller  BufferedSink writeIntLe(int i) throws IOException;
73c6bd683320121544811f481709b3fdbcbe9b3866Neil Fuller
74c6bd683320121544811f481709b3fdbcbe9b3866Neil Fuller  /** Writes a big-endian long to this sink using eight bytes. */
75c6bd683320121544811f481709b3fdbcbe9b3866Neil Fuller  BufferedSink writeLong(long v) throws IOException;
76c6bd683320121544811f481709b3fdbcbe9b3866Neil Fuller
77c6bd683320121544811f481709b3fdbcbe9b3866Neil Fuller  /** Writes a little-endian long to this sink using eight bytes. */
78c6bd683320121544811f481709b3fdbcbe9b3866Neil Fuller  BufferedSink writeLongLe(long v) throws IOException;
79c6bd683320121544811f481709b3fdbcbe9b3866Neil Fuller
80e78f117bcbd6b57d783737107f445ef75ecb474aNeil Fuller  /**
81e78f117bcbd6b57d783737107f445ef75ecb474aNeil Fuller   * Writes complete segments to the underlying sink, if one exists. Like {@link #flush}, but
82e78f117bcbd6b57d783737107f445ef75ecb474aNeil Fuller   * weaker. Use this to limit the memory held in the buffer to a single segment.
83e78f117bcbd6b57d783737107f445ef75ecb474aNeil Fuller   */
843c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  BufferedSink emitCompleteSegments() throws IOException;
853c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller
86e78f117bcbd6b57d783737107f445ef75ecb474aNeil Fuller  /**
87e78f117bcbd6b57d783737107f445ef75ecb474aNeil Fuller   * Writes all buffered data to the underlying sink, if one exists. Like {@link #flush}, but
88e78f117bcbd6b57d783737107f445ef75ecb474aNeil Fuller   * weaker. Call this before this buffered sink goes out of scope so that its data can reach its
89e78f117bcbd6b57d783737107f445ef75ecb474aNeil Fuller   * destination.
90e78f117bcbd6b57d783737107f445ef75ecb474aNeil Fuller   */
91e78f117bcbd6b57d783737107f445ef75ecb474aNeil Fuller  BufferedSink emit() throws IOException;
92e78f117bcbd6b57d783737107f445ef75ecb474aNeil Fuller
933c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  /** Returns an output stream that writes to this sink. */
943c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  OutputStream outputStream();
953c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller}
96