|
| 1 | +package com.maxmind.db; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.nio.channels.FileChannel; |
| 5 | +import java.nio.charset.CharacterCodingException; |
| 6 | +import java.nio.charset.CharsetDecoder; |
| 7 | + |
| 8 | +/** |
| 9 | + * A generic buffer abstraction that supports sequential and random access |
| 10 | + * to binary data. Implementations may be backed by a single {@link |
| 11 | + * java.nio.ByteBuffer} or multiple buffers for larger capacities. |
| 12 | + * |
| 13 | + * <p>This interface is designed to provide a long-based API while |
| 14 | + * remaining compatible with the limitations of underlying storage. |
| 15 | + */ |
| 16 | +interface Buffer { |
| 17 | + /** |
| 18 | + * Returns the total capacity of this buffer in bytes. |
| 19 | + * |
| 20 | + * @return the capacity |
| 21 | + */ |
| 22 | + long capacity(); |
| 23 | + |
| 24 | + /** |
| 25 | + * Returns the current position of this buffer. |
| 26 | + * |
| 27 | + * @return the position |
| 28 | + */ |
| 29 | + long position(); |
| 30 | + |
| 31 | + /** |
| 32 | + * Sets the buffer's position. |
| 33 | + * |
| 34 | + * @param newPosition the new position |
| 35 | + * @return this buffer |
| 36 | + */ |
| 37 | + Buffer position(long newPosition); |
| 38 | + |
| 39 | + /** |
| 40 | + * Returns the current limit of this buffer. |
| 41 | + * |
| 42 | + * @return the limit |
| 43 | + */ |
| 44 | + long limit(); |
| 45 | + |
| 46 | + /** |
| 47 | + * Sets the buffer's limit. |
| 48 | + * |
| 49 | + * @param newLimit the new limit |
| 50 | + * @return this buffer |
| 51 | + */ |
| 52 | + Buffer limit(long newLimit); |
| 53 | + |
| 54 | + /** |
| 55 | + * Reads the next byte at the current position and advances the position. |
| 56 | + * |
| 57 | + * @return the byte value |
| 58 | + */ |
| 59 | + byte get(); |
| 60 | + |
| 61 | + /** |
| 62 | + * Reads bytes into the given array and advances the position. |
| 63 | + * |
| 64 | + * @param dst the destination array |
| 65 | + * @return this buffer |
| 66 | + */ |
| 67 | + Buffer get(byte[] dst); |
| 68 | + |
| 69 | + /** |
| 70 | + * Reads a byte at the given absolute index without changing the position. |
| 71 | + * |
| 72 | + * @param index the index to read from |
| 73 | + * @return the byte value |
| 74 | + */ |
| 75 | + byte get(long index); |
| 76 | + |
| 77 | + /** |
| 78 | + * Reads the next 8 bytes as a double and advances the position. |
| 79 | + * |
| 80 | + * @return the double value |
| 81 | + */ |
| 82 | + double getDouble(); |
| 83 | + |
| 84 | + /** |
| 85 | + * Reads the next 4 bytes as a float and advances the position. |
| 86 | + * |
| 87 | + * @return the float value |
| 88 | + */ |
| 89 | + float getFloat(); |
| 90 | + |
| 91 | + /** |
| 92 | + * Creates a new buffer that shares the same content but has independent |
| 93 | + * position, limit, and mark values. |
| 94 | + * |
| 95 | + * @return a duplicate buffer |
| 96 | + */ |
| 97 | + Buffer duplicate(); |
| 98 | + |
| 99 | + /** |
| 100 | + * Reads data from the given channel into this buffer starting at the |
| 101 | + * current position. |
| 102 | + * |
| 103 | + * @param channel the file channel |
| 104 | + * @return the number of bytes read |
| 105 | + * @throws IOException if an I/O error occurs |
| 106 | + */ |
| 107 | + long readFrom(FileChannel channel) throws IOException; |
| 108 | + |
| 109 | + /** |
| 110 | + * Decodes the buffer's content into a string using the given decoder. |
| 111 | + * |
| 112 | + * @param decoder the charset decoder |
| 113 | + * @return the decoded string |
| 114 | + * @throws CharacterCodingException if decoding fails |
| 115 | + */ |
| 116 | + String decode(CharsetDecoder decoder) throws CharacterCodingException; |
| 117 | + |
| 118 | + /** |
| 119 | + * Creates a read-only view of this buffer. |
| 120 | + * |
| 121 | + * @return a read-only buffer |
| 122 | + */ |
| 123 | + Buffer asReadOnlyBuffer(); |
| 124 | +} |
0 commit comments