# codec

`software.visionary.cuneiform:codec:2026.10.13` · https://codeberg.org/cuneiform/codec

Visionary Software Solutions Codec: the contract of a binary-to-text codec, whose text is typed by its symbols

The contract of a binary-to-text codec: bits in, text out, and back. The text is typed by the symbols it holds, so one codec's text is never another's. Base16, base32, base45 and base64 implement it.

## Key Concepts

### Codec contract

**Role.** <code>Codec&lt;S&gt;</code> encodes bits as <code>Encoded&lt;S&gt;</code> and decodes that back to bits. It takes any <code>software.visionary.wheeler.Bits</code>, whatever holds them. <code>read</code> takes characters to encoded data, and its <code>toString()</code> writes them. A codec is also a <code>SerializationStrategy&lt;Encoded&lt;S&gt;&gt;</code>: <code>deserialize</code> encodes a byte array and <code>serialize</code> decodes to one.

### Symbols and alphabets

**Role.** A <code>Symbol</code> is one element of a codec's text, written as a glyph of <code>software.visionary.cuneiform.graphic</code>. It answers the character it is written as, and a constant of the alphabet it belongs to, which answers for the whole of it. An <code>Alphabet</code> is an enum of symbols: each constant picks its glyph, its place in the enum is its value, and any constant reads a glyph as the constant that writes it. Both are open, so a codec may seal the symbols its own text holds.

### Encoded data

**Role.** <code>Encoded&lt;S&gt;</code> is an immutable sequence of the symbols <code>S</code>. <code>EncodedFactory</code> makes it over a source of symbols: the data holds the source, and walks it anew for each walk, comparison and <code>toString()</code>.

### Views

**Role.** <code>encode</code>, <code>decode</code> and <code>read</code> answer with views of what they were given. A view computes as it is walked and holds nothing per bit or per symbol, so only its source bounds its length. The byte-array methods hold octets packed, a byte each, and read a bit by a position a <code>long</code> counts. Packed octets are <code>software.visionary.wheeler.tets.Segmented</code> and hand over an octet at a time.

### Conventions

**Role.** <code>Conventions</code> states once what the codecs do alike, and a codec's implementation mixes it in: an octet is eight bits, a group of bits holds a number most significant bit first, and text is read one graphic character at a time. <code>octets</code> gives any bits as the bytes of their octets: segmented bits are taken a segment at a time, so a whole octet passes without its bits being walked, and other bits are walked.

### Claims

**Role.** <code>Claims</code> states what a codec's specification claims of it: that it writes octets as a text, reads a text as octets, refuses a text, decodes what it encodes, and decodes only the text it writes.

## Usage

Write octets as text with any codec, and read them back

```java
import software.visionary.cuneiform.codec.Codec;
import software.visionary.cuneiform.codec.Symbol;

class Wire {
    static <S extends Symbol<?>> String written(final Codec<S> codec, final byte[] octets) {
        return codec.deserialize(octets).toString();
    }

    static <S extends Symbol<?>> byte[] octets(final Codec<S> codec, final String written) {
        return codec.serialize(codec.read(written));
    }
}

```

## Dependencies

- `software.visionary.cuneiform:graphic:2026.10.13` at Compile
- `software.visionary.serialization:api:2026.10.13` at Compile
- `software.visionary.wheeler:tets:2026.10.13` at Compile
- `software.visionary:factory:2026.10.13` at Compile
- `software.visionary.contrax:processor:2026.10.13` at Processor
- `software.visionary:spex-closure:2026.10.13` at Testing

## Caveats

- The bits walked from <code>decode</code> before a refusal have already been handed over. Use <code>serialize</code> where a text must be accepted whole or not at all.
- A byte array and a <code>String</code> each hold at most 2<sup>31</sup>−1 elements, so <code>serialize</code>, <code>deserialize</code> and <code>toString()</code> stop there. <code>encode</code> and <code>decode</code> do not.

