use of net.openhft.chronicle.core.Memory in project Chronicle-Bytes by OpenHFT.
the class MappedBytes method appendUtf8.
@NotNull
@Override
public Bytes<Void> appendUtf8(CharSequence cs, int start, int length) throws BufferOverflowException, IllegalArgumentException {
assert singleThreadedAccess();
// check the start.
long pos = writePosition();
writeCheckOffset(pos, 0);
if (!(cs instanceof String) || pos + length * 3 + 5 >= safeLimit()) {
super.appendUtf8(cs, start, length);
return this;
}
if (Jvm.isJava9Plus()) {
byte[] bytes = extractBytes((String) cs);
long address = addressForWrite(pos);
Memory memory = OS.memory();
int i = 0;
non_ascii: {
for (; i < length; i++) {
byte c = bytes[i + start];
if (c > 127) {
writeSkip(i);
break non_ascii;
}
memory.writeByte(address++, c);
}
writeSkip(length);
return this;
}
for (; i < length; i++) {
byte c = bytes[i + start];
appendUtf8(c);
}
} else {
char[] chars = extractChars((String) cs);
long address = addressForWrite(pos);
Memory memory = OS.memory();
int i = 0;
non_ascii: {
for (; i < length; i++) {
char c = chars[i + start];
if (c > 127) {
writeSkip(i);
break non_ascii;
}
memory.writeByte(address++, (byte) c);
}
writeSkip(length);
return this;
}
for (; i < length; i++) {
char c = chars[i + start];
appendUtf8(c);
}
}
return this;
}
use of net.openhft.chronicle.core.Memory in project Chronicle-Bytes by OpenHFT.
the class NativeBytesStore method of.
@NotNull
private static NativeBytesStore<Void> of(long capacity, boolean zeroOut, boolean elastic) throws IllegalArgumentException {
Memory memory = OS.memory();
long address = memory.allocate(capacity);
if (zeroOut || capacity < MEMORY_MAPPED_SIZE) {
memory.setMemory(address, capacity, (byte) 0);
memory.storeFence();
}
@NotNull Deallocator deallocator = new Deallocator(address, capacity);
return new NativeBytesStore<>(address, capacity, deallocator, elastic);
}
use of net.openhft.chronicle.core.Memory in project Chronicle-Bytes by OpenHFT.
the class NativeBytesStore method byteCheckSum.
public int byteCheckSum(long position, long limit) {
@Nullable Memory memory = this.memory;
assert memory != null;
int b = 0;
long ptr = address + position;
long end = address + limit;
for (; ptr < end - 7; ptr += 8) {
b += memory.readByte(ptr) + memory.readByte(ptr + 1) + memory.readByte(ptr + 2) + memory.readByte(ptr + 3) + memory.readByte(ptr + 4) + memory.readByte(ptr + 5) + memory.readByte(ptr + 6) + memory.readByte(ptr + 7);
}
for (; ptr < end; ptr++) {
b += memory.readByte(ptr);
}
return b & 0xFF;
}
use of net.openhft.chronicle.core.Memory in project Chronicle-Bytes by OpenHFT.
the class NativeBytesStore method peekUnsignedByte.
@Override
public int peekUnsignedByte(long offset) {
final long address = this.address;
@Nullable final Memory memory = this.memory;
final long translate = translate(offset);
// assert translate >= 0;
final long address2 = address + translate;
// last.writeLong(8, Thread.currentThread().getId());
// last.writeLong(0, offset);
// last.writeLong(16, translate);
// last.writeLong(32, maximumLimit);
// last.writeLong(48, addressForRead);
// last.writeLong(64, address2);
// last.writeBoolean(80, memory != null);
// last.writeVolatileByte(88, (byte) 1);
int ret = translate >= maximumLimit ? -1 : memory.readByte(address2) & 0xFF;
// last.writeLong(24, Thread.currentThread().getId());
return ret;
}
use of net.openhft.chronicle.core.Memory in project Chronicle-Bytes by OpenHFT.
the class UncheckedNativeBytes method append8bit.
@Override
@NotNull
public Bytes<Underlying> append8bit(@NotNull CharSequence cs) throws BufferOverflowException, BufferUnderflowException {
if (cs instanceof BytesStore) {
return write((BytesStore) cs);
}
int length = cs.length();
long offset = writeOffsetPositionMoved(length);
long address = bytesStore.addressForWrite(offset);
@Nullable Memory memory = bytesStore.memory;
assert memory != null;
try {
int i = 0;
for (; i < length - 1; i += 2) {
char c = cs.charAt(i);
char c2 = cs.charAt(i + 1);
memory.writeByte(address + i, (byte) c);
memory.writeByte(address + i + 1, (byte) c2);
}
for (; i < length; i++) {
char c = cs.charAt(i);
memory.writeByte(address + i, (byte) c);
}
return this;
} catch (IndexOutOfBoundsException e) {
throw new AssertionError(e);
}
}
Aggregations