BufferedSink.java revision c6bd683320121544811f481709b3fdbcbe9b3866
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;
203c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller
213c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller/**
223c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * A sink that keeps a buffer internally so that callers can do small writes
233c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller * without a performance penalty.
243c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller */
253c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fullerpublic interface BufferedSink extends Sink {
263c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  /** Returns this sink's internal buffer. */
273c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  OkBuffer buffer();
283c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller
293c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  BufferedSink write(ByteString byteString) throws IOException;
303c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller
313c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  /**
323c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller   * Like {@link OutputStream#write}, this writes a complete byte array to this
333c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller   * sink.
343c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller   */
353c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  BufferedSink write(byte[] source) throws IOException;
363c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller
373c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  /**
383c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller   * Like {@link OutputStream#write}, this writes {@code byteCount} bytes
393c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller   * of {@code source}, starting at {@code offset}.
403c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller   */
413c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  BufferedSink write(byte[] source, int offset, int byteCount) throws IOException;
423c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller
433c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  /** Encodes {@code string} in UTF-8 and writes it to this sink. */
443c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  BufferedSink writeUtf8(String string) throws IOException;
453c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller
46c6bd683320121544811f481709b3fdbcbe9b3866Neil Fuller  /** Writes a byte to this sink. */
473c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  BufferedSink writeByte(int b) throws IOException;
483c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller
49c6bd683320121544811f481709b3fdbcbe9b3866Neil Fuller  /** Writes a big-endian short to this sink using two bytes. */
503c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  BufferedSink writeShort(int s) throws IOException;
513c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller
52c6bd683320121544811f481709b3fdbcbe9b3866Neil Fuller  /** Writes a little-endian short to this sink using two bytes. */
53c6bd683320121544811f481709b3fdbcbe9b3866Neil Fuller  BufferedSink writeShortLe(int s) throws IOException;
54c6bd683320121544811f481709b3fdbcbe9b3866Neil Fuller
55c6bd683320121544811f481709b3fdbcbe9b3866Neil Fuller  /** Writes a big-endian int to this sink using four bytes. */
563c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  BufferedSink writeInt(int i) throws IOException;
573c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller
58c6bd683320121544811f481709b3fdbcbe9b3866Neil Fuller  /** Writes a little-endian int to this sink using four bytes. */
59c6bd683320121544811f481709b3fdbcbe9b3866Neil Fuller  BufferedSink writeIntLe(int i) throws IOException;
60c6bd683320121544811f481709b3fdbcbe9b3866Neil Fuller
61c6bd683320121544811f481709b3fdbcbe9b3866Neil Fuller  /** Writes a big-endian long to this sink using eight bytes. */
62c6bd683320121544811f481709b3fdbcbe9b3866Neil Fuller  BufferedSink writeLong(long v) throws IOException;
63c6bd683320121544811f481709b3fdbcbe9b3866Neil Fuller
64c6bd683320121544811f481709b3fdbcbe9b3866Neil Fuller  /** Writes a little-endian long to this sink using eight bytes. */
65c6bd683320121544811f481709b3fdbcbe9b3866Neil Fuller  BufferedSink writeLongLe(long v) throws IOException;
66c6bd683320121544811f481709b3fdbcbe9b3866Neil Fuller
67c6bd683320121544811f481709b3fdbcbe9b3866Neil Fuller  /** Writes complete segments to this sink. Like {@link #flush}, but weaker. */
683c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  BufferedSink emitCompleteSegments() throws IOException;
693c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller
703c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  /** Returns an output stream that writes to this sink. */
713c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  OutputStream outputStream();
723c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller}
73