@Override public int uncheckedReadUnsignedByte() { try { int unsignedByte = bytesStore.readUnsignedByte(readPosition); readPosition++; return unsignedByte; } catch (BufferUnderflowException e) { return -1; } }
@Override public long get(BytesStore bytes, long index) { int i = bytes.readUnsignedByte(index >> 1); return (index & 1) != 0 ? (i >> 4) : (i & 0xF); }
@Override public long get(BytesStore bytes, long index) { return bytes.readUnsignedByte(index); }
long fetch8(BytesStore bytes, long off) { return bytes.readUnsignedByte(off); }
@Override public int readUnsignedByte() { try { long offset = readOffsetPositionMoved(1); return bytesStore.readUnsignedByte(offset); } catch (BufferUnderflowException e) { return -1; } }
/** * Perform a <i>not</i> atomic add and get operation for an unsigned byte value. This method * <i>does not</i> check for unsigned byte overflow. * * @param offset to add and get * @param adding value to add, can be 1 * @return the sum */ default int addAndGetUnsignedByteNotAtomic(long offset, int adding) throws BufferUnderflowException { try { int r = (readUnsignedByte(offset) + adding) & 0xFF; writeByte(offset, (byte) r); return r; } catch (BufferOverflowException e) { throw new AssertionError(e); } }
@Override @NotNull protected String getValue(@NotNull BytesStore cs, int length) { StringBuilder sb = SBP.acquireStringBuilder(); for (int i = 0; i < length; i++) sb.append((char) cs.readUnsignedByte(cs.readPosition() + i)); return sb.toString(); } }
/** * Does the BytesStore end with a character? * * @param c to look for * @return true if its the last character. */ default boolean endsWith(char c) { try { return readRemaining() > 0 && readUnsignedByte(readLimit() - 1) == c; } catch (BufferUnderflowException e) { throw new AssertionError(e); } }
/** * Does the BytesStore start with a character? * * @param c to look for * @return true if its the last character. */ default boolean startsWith(char c) { try { return readRemaining() > 0 && readUnsignedByte(readPosition()) == c; } catch (BufferUnderflowException e) { throw new AssertionError(e); } }
@Override public void set(BytesStore bytes, long index, long value) { int i = bytes.readUnsignedByte(index >> 1); int i2 = (int) ((index & 1) != 0 ? (i & 0xF0) | (value & 0xF) : (i & 0xF) | ((value & 0xf) << 4)); bytes.writeByte(index, Maths.toUInt8(value)); }
/** * Assume ISO-8859-1 encoding, subclasses can override this. */ @Override default char charAt(int index) throws IndexOutOfBoundsException { try { return (char) readUnsignedByte(readPosition() + index); } catch (BufferUnderflowException e) { throw new IndexOutOfBoundsException((readPosition() + index) + " >= " + readLimit()); } }
public static String to8bitString(@org.jetbrains.annotations.NotNull @NotNull BytesStore bytes) throws IllegalArgumentException { long pos = bytes.readPosition(); int len = Maths.toInt32(bytes.readRemaining()); @org.jetbrains.annotations.NotNull char[] chars = new char[len]; if (bytes instanceof VanillaBytes) { ((VanillaBytes) bytes).read8Bit(chars, len); } else { for (int i = 0; i < len; i++) try { chars[i] = (char) bytes.readUnsignedByte(pos + i); } catch (Exception e) { return new String(chars, 0, len) + ' ' + e; } } return StringUtils.newString(chars); }