|
| 1 | +package com.google.net.stubby.newtransport; |
| 2 | + |
| 3 | +import java.io.Closeable; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.OutputStream; |
| 6 | +import java.nio.ByteBuffer; |
| 7 | + |
| 8 | +/** |
| 9 | + * Interface for an abstract byte buffer. Buffers are intended to be a read-only, except for the |
| 10 | + * read position which is incremented after each read call. |
| 11 | + * |
| 12 | + * <p>Buffers may optionally expose a backing array for optimization purposes, similar to what is |
| 13 | + * done in {@link ByteBuffer}. It is not expected that callers will attempt to modify the backing |
| 14 | + * array. |
| 15 | + */ |
| 16 | +public interface Buffer extends Closeable { |
| 17 | + |
| 18 | + /** |
| 19 | + * Gets the current number of readable bytes remaining in this buffer. |
| 20 | + */ |
| 21 | + int readableBytes(); |
| 22 | + |
| 23 | + /** |
| 24 | + * Reads the next unsigned byte from this buffer and increments the read position by 1. If the |
| 25 | + * required bytes are not readable, throws {@link IndexOutOfBoundsException}. |
| 26 | + */ |
| 27 | + int readUnsignedByte(); |
| 28 | + |
| 29 | + /** |
| 30 | + * Reads a 3-byte unsigned integer from this buffer using big-endian byte ordering. Increments the |
| 31 | + * read position by 3. If the required bytes are not readable, throws |
| 32 | + * {@link IndexOutOfBoundsException}. |
| 33 | + */ |
| 34 | + int readUnsignedMedium(); |
| 35 | + |
| 36 | + /** |
| 37 | + * Reads a 2-byte unsigned integer from this buffer using big-endian byte ordering. Increments the |
| 38 | + * read position by 2. If the required bytes are not readable, throws |
| 39 | + * {@link IndexOutOfBoundsException}. |
| 40 | + */ |
| 41 | + int readUnsignedShort(); |
| 42 | + |
| 43 | + /** |
| 44 | + * Reads a 4-byte signed integer from this buffer using big-endian byte ordering. Increments the |
| 45 | + * read position by 4. If the required bytes are not readable, throws |
| 46 | + * {@link IndexOutOfBoundsException}. |
| 47 | + */ |
| 48 | + int readInt(); |
| 49 | + |
| 50 | + /** |
| 51 | + * Increments the read position by the given length. If the skipped bytes are not readable, throws |
| 52 | + * {@link IndexOutOfBoundsException}. |
| 53 | + */ |
| 54 | + void skipBytes(int length); |
| 55 | + |
| 56 | + /** |
| 57 | + * Reads {@code length} bytes from this buffer and writes them to the destination array. |
| 58 | + * Increments the read position by {@code length}. If the required bytes are not readable or |
| 59 | + * {@code dest} array is to small, throws {@link IndexOutOfBoundsException}. |
| 60 | + * |
| 61 | + * @param dest the destination array to receive the bytes. |
| 62 | + * @param destOffset the starting offset in the destination array. |
| 63 | + * @param length the number of bytes to be copied. |
| 64 | + */ |
| 65 | + void readBytes(byte[] dest, int destOffset, int length); |
| 66 | + |
| 67 | + /** |
| 68 | + * Reads from this buffer until the destination's position reaches its limit, and increases the |
| 69 | + * read position by the number of the transferred bytes. If the required bytes are not readable, |
| 70 | + * throws {@link IndexOutOfBoundsException}. |
| 71 | + * |
| 72 | + * @param dest the destination buffer to receive the bytes. |
| 73 | + */ |
| 74 | + void readBytes(ByteBuffer dest); |
| 75 | + |
| 76 | + /** |
| 77 | + * Reads {@code length} bytes from this buffer and writes them to the destination stream. |
| 78 | + * Increments the read position by {@code length}. If the required bytes are not readable, throws |
| 79 | + * {@link IndexOutOfBoundsException}. |
| 80 | + * |
| 81 | + * @param dest the destination stream to receive the bytes. |
| 82 | + * @param length the number of bytes to be copied. |
| 83 | + * @throws IOException thrown if any error was encountered while writing to the stream. |
| 84 | + */ |
| 85 | + void readBytes(OutputStream dest, int length) throws IOException; |
| 86 | + |
| 87 | + /** |
| 88 | + * Indicates whether or not this buffer exposes a backing array. |
| 89 | + */ |
| 90 | + boolean hasArray(); |
| 91 | + |
| 92 | + /** |
| 93 | + * Gets the backing array for this buffer. This is an optional method, so callers should first |
| 94 | + * check {@link #hasArray}. Buffers not supporting this method will throw |
| 95 | + * {@link UnsupportedOperationException}. |
| 96 | + */ |
| 97 | + byte[] array(); |
| 98 | + |
| 99 | + /** |
| 100 | + * Gets the offset in the backing array of the current read position. This is an optional method, |
| 101 | + * so callers should first check {@link #hasArray}. Buffers not supporting this method will throw |
| 102 | + * {@link UnsupportedOperationException}. |
| 103 | + */ |
| 104 | + int arrayOffset(); |
| 105 | + |
| 106 | + /** |
| 107 | + * Closes this buffer and releases any resources. |
| 108 | + */ |
| 109 | + @Override |
| 110 | + void close(); |
| 111 | +} |
0 commit comments