package java.nio;
import java.io.FileDescriptor;
import java.lang.ref.Reference;
import java.util.Objects;
import jdk.internal.access.foreign.MemorySegmentProxy;
import jdk.internal.misc.ScopedMemoryAccess.Scope;
import jdk.internal.misc.VM;
import jdk.internal.ref.Cleaner;
import sun.nio.ch.DirectBuffer;
class DirectCharBufferS
extends CharBuffer
implements DirectBuffer
{
private static final long ARRAY_BASE_OFFSET = UNSAFE.arrayBaseOffset(char[].class);
protected static final boolean UNALIGNED = Bits.unaligned();
private final Object att;
public Object attachment() {
return att;
}
public Cleaner cleaner() { return null; }
DirectCharBufferS(DirectBuffer db,
int mark, int pos, int lim, int cap,
int off, MemorySegmentProxy segment)
{
super(mark, pos, lim, cap, segment);
address = ((Buffer)db).address + off;
Object attachment = db.attachment();
att = (attachment == null ? db : attachment);
}
@Override
Object base() {
return null;
}
public CharBuffer slice() {
int pos = this.position();
int lim = this.limit();
int rem = (pos <= lim ? lim - pos : 0);
int off = (pos << 1);
assert (off >= 0);
return new DirectCharBufferS(this, -1, 0, rem, rem, off, segment);
}
@Override
public CharBuffer slice(int index, int length) {
Objects.checkFromIndexSize(index, length, limit());
return new DirectCharBufferS(this,
-1,
0,
length,
length,
index << 1,
segment);
}
public CharBuffer duplicate() {
return new DirectCharBufferS(this,
this.markValue(),
this.position(),
this.limit(),
this.capacity(),
0, segment);
}
public CharBuffer asReadOnlyBuffer() {
return new DirectCharBufferRS(this,
this.markValue(),
this.position(),
this.limit(),
this.capacity(),
0, segment);
}
public long address() {
Scope scope = scope();
if (scope != null) {
if (scope.ownerThread() == null) {
throw new UnsupportedOperationException("ByteBuffer derived from shared segments not supported");
}
try {
scope.checkValidState();
} catch (Scope.ScopedAccessError e) {
throw new IllegalStateException("This segment is already closed");
}
}
return address;
}
private long ix(int i) {
return address + ((long)i << 1);
}
public char get() {
try {
return (Bits.swap(SCOPED_MEMORY_ACCESS.getChar(scope(), null, ix(nextGetIndex()))));
} finally {
Reference.reachabilityFence(this);
}
}
public char get(int i) {
try {
return (Bits.swap(SCOPED_MEMORY_ACCESS.getChar(scope(), null, ix(checkIndex(i)))));
} finally {
Reference.reachabilityFence(this);
}
}
char getUnchecked(int i) {
try {
return (Bits.swap(SCOPED_MEMORY_ACCESS.getChar(null, null, ix(i))));
} finally {
Reference.reachabilityFence(this);
}
}
public CharBuffer get(char[] dst, int offset, int length) {
if (((long)length << 1) > Bits.JNI_COPY_TO_ARRAY_THRESHOLD) {
Objects.checkFromIndexSize(offset, length, dst.length);
int pos = position();
int lim = limit();
assert (pos <= lim);
int rem = (pos <= lim ? lim - pos : 0);
if (length > rem)
throw new BufferUnderflowException();
long dstOffset = ARRAY_BASE_OFFSET + ((long)offset << 1);
try {
if (order() != ByteOrder.nativeOrder())
SCOPED_MEMORY_ACCESS.copySwapMemory(scope(), null, null,
ix(pos),
dst,
dstOffset,
(long)length << 1,
(long)1 << 1);
else
SCOPED_MEMORY_ACCESS.copyMemory(scope(), null, null,
ix(pos),
dst,
dstOffset,
(long)length << 1);
} finally {
Reference.reachabilityFence(this);
}
position(pos + length);
} else {
super.get(dst, offset, length);
}
return this;
}
public CharBuffer get(int index, char[] dst, int offset, int length) {
if (((long)length << 1) > Bits.JNI_COPY_TO_ARRAY_THRESHOLD) {
Objects.checkFromIndexSize(index, length, limit());
Objects.checkFromIndexSize(offset, length, dst.length);
long dstOffset = ARRAY_BASE_OFFSET + ((long)offset << 1);
try {
if (order() != ByteOrder.nativeOrder())
SCOPED_MEMORY_ACCESS.copySwapMemory(scope(), null, null,
ix(index),
dst,
dstOffset,
(long)length << 1,
(long)1 << 1);
else
SCOPED_MEMORY_ACCESS.copyMemory(scope(), null, null,
ix(index),
dst,
dstOffset,
(long)length << 1);
} finally {
Reference.reachabilityFence(this);
}
} else {
super.get(index, dst, offset, length);
}
return this;
}
public CharBuffer put(char x) {
try {
SCOPED_MEMORY_ACCESS.putChar(scope(), null, ix(nextPutIndex()), Bits.swap((x)));
} finally {
Reference.reachabilityFence(this);
}
return this;
}
public CharBuffer put(int i, char x) {
try {
SCOPED_MEMORY_ACCESS.putChar(scope(), null, ix(checkIndex(i)), Bits.swap((x)));
} finally {
Reference.reachabilityFence(this);
}
return this;
}
public CharBuffer put(CharBuffer src) {
super.put(src);
return this;
}
public CharBuffer put(int index, CharBuffer src, int offset, int length) {
super.put(index, src, offset, length);
return this;
}
public CharBuffer put(char[] src, int offset, int length) {
if (((long)length << 1) > Bits.JNI_COPY_FROM_ARRAY_THRESHOLD) {
Objects.checkFromIndexSize(offset, length, src.length);
int pos = position();
int lim = limit();
assert (pos <= lim);
int rem = (pos <= lim ? lim - pos : 0);
if (length > rem)
throw new BufferOverflowException();
long srcOffset = ARRAY_BASE_OFFSET + ((long)offset << 1);
try {
if (order() != ByteOrder.nativeOrder())
SCOPED_MEMORY_ACCESS.copySwapMemory(scope(), null, src,
srcOffset,
null,
ix(pos),
(long)length << 1,
(long)1 << 1);
else
SCOPED_MEMORY_ACCESS.copyMemory(scope(), null, src,
srcOffset,
null,
ix(pos),
(long)length << 1);
} finally {
Reference.reachabilityFence(this);
}
position(pos + length);
} else {
super.put(src, offset, length);
}
return this;
}
public CharBuffer put(int index, char[] src, int offset, int length) {
if (((long)length << 1) > Bits.JNI_COPY_FROM_ARRAY_THRESHOLD) {
Objects.checkFromIndexSize(index, length, limit());
Objects.checkFromIndexSize(offset, length, src.length);
long srcOffset = ARRAY_BASE_OFFSET + ((long)offset << 1);
try {
if (order() != ByteOrder.nativeOrder())
SCOPED_MEMORY_ACCESS.copySwapMemory(scope(), null, src,
srcOffset,
null,
ix(index),
(long)length << 1,
(long)1 << 1);
else
SCOPED_MEMORY_ACCESS.copyMemory(
scope(), null, src,
srcOffset, null, ix(index), (long)length << 1);
} finally {
Reference.reachabilityFence(this);
}
} else {
super.put(index, src, offset, length);
}
return this;
}
public CharBuffer compact() {
int pos = position();
int lim = limit();
assert (pos <= lim);
int rem = (pos <= lim ? lim - pos : 0);
try {
SCOPED_MEMORY_ACCESS.copyMemory(scope(), null, null,
ix(pos), null, ix(0), (long)rem << 1);
} finally {
Reference.reachabilityFence(this);
}
position(rem);
limit(capacity());
discardMark();
return this;
}
public boolean isDirect() {
return true;
}
public boolean isReadOnly() {
return false;
}
public String toString(int start, int end) {
Objects.checkFromToIndex(start, end, limit());
try {
int len = end - start;
char[] ca = new char[len];
CharBuffer cb = CharBuffer.wrap(ca);
CharBuffer db = this.duplicate();
db.position(start);
db.limit(end);
cb.put(db);
return new String(ca);
} catch (StringIndexOutOfBoundsException x) {
throw new IndexOutOfBoundsException();
}
}
public CharBuffer subSequence(int start, int end) {
int pos = position();
int lim = limit();
assert (pos <= lim);
pos = (pos <= lim ? pos : lim);
int len = lim - pos;
Objects.checkFromToIndex(start, end, len);
return new DirectCharBufferS(this,
-1,
pos + start,
pos + end,
capacity(),
offset, segment);
}
public ByteOrder order() {
return ((ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN)
? ByteOrder.LITTLE_ENDIAN : ByteOrder.BIG_ENDIAN);
}
ByteOrder charRegionOrder() {
return order();
}
}