BufferedSink.java revision 3c938a3f6b61ce5e2dba0d039b03fe73b89fd26c
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
463c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  /** Writes byte to the end of this sink. */
473c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  BufferedSink writeByte(int b) throws IOException;
483c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller
493c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  /** Writes a Big-Endian short to the end of this sink. */
503c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  BufferedSink writeShort(int s) throws IOException;
513c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller
523c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  /** Writes a Big-Endian int to the end of this sink. */
533c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  BufferedSink writeInt(int i) throws IOException;
543c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller
553c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  /** Writes complete segments to the sink. Like {@link #flush}, but weaker. */
563c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  BufferedSink emitCompleteSegments() throws IOException;
573c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller
583c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  /** Returns an output stream that writes to this sink. */
593c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller  OutputStream outputStream();
603c938a3f6b61ce5e2dba0d039b03fe73b89fd26cNeil Fuller}
61