Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 1 | package eu.mulk.jgvariant.ostree; |
| 2 | |
| 3 | import eu.mulk.jgvariant.core.Decoder; |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 4 | import java.nio.ByteBuffer; |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 5 | |
| 6 | /** |
| 7 | * A wrapper for {@link ByteString} that refers to a content-addressed object in an OSTree |
| 8 | * repository. |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 9 | * |
| 10 | * @param byteString the bytes that make up this {@link Checksum}. |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 11 | */ |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 12 | public record Checksum(ByteString byteString) { |
| 13 | |
| 14 | private static final int SIZE = 32; |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 15 | |
| 16 | private static final Decoder<Checksum> DECODER = ByteString.decoder().map(Checksum::new); |
| 17 | |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 18 | public Checksum { |
Matthias Andreas Benkard | f96d0e3 | 2021-12-29 21:53:50 +0100 | [diff] [blame] | 19 | if (byteString.size() == 0) { |
| 20 | byteString = zero().byteString; |
| 21 | } |
| 22 | |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 23 | if (byteString.size() != SIZE) { |
| 24 | throw new IllegalArgumentException( |
| 25 | "attempted to construct Checksum of length %d (expected: %d)" |
| 26 | .formatted(byteString.size(), SIZE)); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * A decoder for a {@code byte[]} that wraps the result in a {@link Checksum}. |
| 32 | * |
| 33 | * @return a possibly shared {@link Decoder}. |
| 34 | */ |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 35 | public static Decoder<Checksum> decoder() { |
| 36 | return DECODER; |
| 37 | } |
| 38 | |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 39 | /** |
| 40 | * Returns an empty checksum. |
| 41 | * |
| 42 | * @return a checksum whose bits are all zero. |
| 43 | */ |
| 44 | public static Checksum zero() { |
| 45 | return new Checksum(new ByteString(new byte[SIZE])); |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 46 | } |
| 47 | |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 48 | /** |
| 49 | * Checks whether the checksum contains only zero bits. |
| 50 | * |
| 51 | * @return {@code true} if the byte string is equal to {@link #zero()}, {@code false} otherwise. |
| 52 | */ |
| 53 | public boolean isEmpty() { |
| 54 | return equals(zero()); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Converts the contained byte array into a hex string. |
| 59 | * |
| 60 | * <p>Useful for printing. |
| 61 | * |
| 62 | * @return a hex string representation of the bytes making up this checksum. |
| 63 | */ |
| 64 | public String hex() { |
| 65 | return byteString.hex(); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Parses a hex string into a {@link Checksum}. |
| 70 | * |
| 71 | * @param hex a hex string. |
| 72 | * @return a {@link Checksum} corresponding to the given hex string. |
| 73 | */ |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 74 | public static Checksum ofHex(String hex) { |
| 75 | return new Checksum(ByteString.ofHex(hex)); |
| 76 | } |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 77 | |
| 78 | /** |
Matthias Andreas Benkard | c7aa2b6 | 2022-01-23 18:10:03 +0100 | [diff] [blame^] | 79 | * Converts the contained byte array into modified Base64 (with {@code "/"} replaced with {@code |
| 80 | * "-"}). |
| 81 | * |
| 82 | * <p>Modified Base64 is Base64 with {@code "/"} replaced with {@code "_"}. It is used to address |
| 83 | * static deltas in an OSTree repository. |
| 84 | * |
| 85 | * <p>Useful for printing. |
| 86 | * |
| 87 | * @return a modified Base64 representation of the bytes making up this checksum. |
| 88 | */ |
| 89 | public String modifiedBase64() { |
| 90 | return byteString.modifiedBase64(); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Parses a modified Base64 string into a {@link Checksum}. |
| 95 | * |
| 96 | * <p>Modified Base64 is Base64 with {@code "/"} replaced with {@code "_"}. It is used to address |
| 97 | * static deltas in an OSTree repository. |
| 98 | * |
| 99 | * @param mbase64 a hex string. |
| 100 | * @return a {@link Checksum} corresponding to the given modified Base64 string. |
| 101 | */ |
| 102 | public static Checksum ofModifiedBase64(String mbase64) { |
| 103 | return new Checksum(ByteString.ofModifiedBase64(mbase64)); |
| 104 | } |
| 105 | |
| 106 | /** |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 107 | * Reads a Checksum for a {@link ByteBuffer}. |
| 108 | * |
| 109 | * @param byteBuffer the byte buffer to read from. |
| 110 | * @return a checksum. |
| 111 | */ |
| 112 | public static Checksum readFrom(ByteBuffer byteBuffer) { |
| 113 | var bytes = new byte[SIZE]; |
| 114 | byteBuffer.get(bytes); |
| 115 | return new Checksum(new ByteString(bytes)); |
| 116 | } |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 117 | } |