public final class KeyListEncoder extends Object
byte[]
arrays, compressing consecutive common prefixes.
Keys are encoded/decoded by read()
and write()
in one of two forms:
total-length bytes...
-prefix-length suffix-length suffix-bytes ...
total-length
or negative prefix-length
) is encoded using LongEncoder
.
The suffix-length
, if present, is encoded using UnsignedIntEncoder
.
Support for encoding and decoding an entire iteration of key/value pairs is supported via
readPairs()
and writePairs()
.
Modifier and Type | Method and Description |
---|---|
static byte[] |
read(InputStream input,
byte[] prev)
Read the next key.
|
static Iterator<KVPair> |
readPairs(InputStream input)
Decode an iteration of key/value pairs previously encoded by
writePairs() . |
static void |
write(OutputStream out,
byte[] key,
byte[] prev)
Write the next key, compressing its common prefix with the previous key (if any).
|
static int |
writeLength(byte[] key,
byte[] prev)
Calculate the number of bytes that would be required to write the next key via
write() . |
static void |
writePairs(Iterator<KVPair> kvpairs,
OutputStream output)
Encode an iteration of key/value pairs.
|
static long |
writePairsLength(Iterator<KVPair> kvpairs)
Determine the number of bytes that would be written by
writePairs() . |
public static void write(OutputStream out, byte[] key, byte[] prev) throws IOException
out
- output streamkey
- key to writeprev
- previous key, or null for noneIOException
- if an I/O error occursIllegalArgumentException
- if out
or key
is nullpublic static int writeLength(byte[] key, byte[] prev)
write()
.key
- key to writeprev
- previous key, or null for nonewrite(out, key, prev)
IllegalArgumentException
- if key
is nullpublic static byte[] read(InputStream input, byte[] prev) throws IOException
input
- input streamprev
- previous key, or null for noneIOException
- if an I/O error occursEOFException
- if an unexpected EOF is encounteredIllegalArgumentException
- if input
is nullIllegalArgumentException
- if input
contains invalid datapublic static void writePairs(Iterator<KVPair> kvpairs, OutputStream output) throws IOException
kvpairs
- key/value pair iterationoutput
- encoded outputIOException
- if an I/O error occursIllegalArgumentException
- if either parameter is nullpublic static long writePairsLength(Iterator<KVPair> kvpairs)
writePairs()
.kvpairs
- key/value pair iterationIllegalArgumentException
- if kvpairs
is nullpublic static Iterator<KVPair> readPairs(InputStream input)
writePairs()
.
If an IOException
occurs during iteration, the returned Iterator
wraps it in a RuntimeException
.
If invalid input is encountered during iteration, the returned Iterator
will throw an IllegalArgumentException
.
input
- encoded inputIllegalArgumentException
- if input
is nullCopyright © 2022. All rights reserved.