• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2015 The gRPC Authors
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 
17 package io.grpc.internal;
18 
19 /**
20  * An interface for a byte buffer that can only be written to.
21  * {@link WritableBuffer}s are a generic way to transfer bytes to
22  * the concrete network transports, like Netty and OkHttp.
23  */
24 public interface WritableBuffer {
25 
26   /**
27    * Appends {@code length} bytes to the buffer from the source
28    * array starting at {@code srcIndex}.
29    *
30    * @throws IndexOutOfBoundsException
31    *         if the specified {@code srcIndex} is less than {@code 0},
32    *         if {@code srcIndex + length} is greater than
33    *            {@code src.length}, or
34    *         if {@code length} is greater than {@link #writableBytes()}
35    */
write(byte[] src, int srcIndex, int length)36   void write(byte[] src, int srcIndex, int length);
37 
38   /**
39    * Appends a single byte to the buffer.  This is slow so don't call it.
40    */
write(byte b)41   void write(byte b);
42 
43   /**
44    * Returns the number of bytes one can write to the buffer.
45    */
writableBytes()46   int writableBytes();
47 
48   /**
49    * Returns the number of bytes one can read from the buffer.
50    */
readableBytes()51   int readableBytes();
52 
53   /**
54    * Releases the buffer, indicating to the {@link WritableBufferAllocator} that
55    * this buffer is no longer used and its resources can be reused.
56    */
release()57   void release();
58 }
59