下面列出了java.nio.Buffer#capacity ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
static String toString(Buffer b) {
return (b.getClass().getName()
+ "[pos=" + b.position()
+ " lim=" + b.limit()
+ " cap=" + b.capacity()
+ "]");
}
static String toString(Buffer b) {
return (b.getClass().getName()
+ "[pos=" + b.position()
+ " lim=" + b.limit()
+ " cap=" + b.capacity()
+ "]");
}
private void printBuffer(Buffer buffer) {
StringBuilder sb = new StringBuilder();
sb.append(ANSI_MAGENTA);
if (buffer instanceof ByteBuffer) {
sb.append("byte");
} else if (buffer instanceof ShortBuffer) {
sb.append("short");
} else if (buffer instanceof CharBuffer) {
sb.append("char");
} else if (buffer instanceof FloatBuffer) {
sb.append("float");
} else if (buffer instanceof IntBuffer) {
sb.append("int");
} else if (buffer instanceof LongBuffer) {
sb.append("long");
} else if (buffer instanceof DoubleBuffer) {
sb.append("double");
} else {
throw new UnsupportedOperationException();
}
sb.append(ANSI_RESET);
sb.append("[");
if (buffer.position() == 0
&& buffer.limit() == buffer.capacity()) {
// Common case. Just print buffer size.
sb.append(buffer.capacity());
} else {
sb.append("pos=").append(buffer.position());
sb.append(" lim=").append(buffer.limit());
sb.append(" cap=").append(buffer.capacity());
}
sb.append("]");
print(sb.toString());
}
static String toString(Buffer b) {
return (b.getClass().getName()
+ "[pos=" + b.position()
+ " lim=" + b.limit()
+ " cap=" + b.capacity()
+ "]");
}
public void updateData(Buffer data) {
if (id != -1) {
// request to update data is okay
}
// will force renderer to call glBufferData again
if (this.data.capacity() != data.capacity()) {
dataSizeChanged = true;
}
this.data = data;
setUpdateNeeded();
}
/**
* Creates a new writer for copying bytes from the given source to the given target buffers.
* Data will be read from the current position of source buffer up to that buffer <em>limit</em>.
* Data will be written starting at position 0 of target buffer up to that buffer <em>capacity</em>.
* The position and limit of target buffer are ignored.
* The position and limit of both buffers may be modified by this method.
*
* @param source the buffer from which to copy data.
* @param target the buffer where to copy data.
* @return a writer from given source to target.
*/
public static ByteWriter create(Buffer source, final ByteBuffer target) {
if (source.limit() != source.capacity()) {
source = JDK9.slice(source);
}
if (source instanceof DoubleBuffer) return new Doubles ((DoubleBuffer) source, target);
if (source instanceof FloatBuffer) return new Floats ( (FloatBuffer) source, target);
if (source instanceof LongBuffer) return new Longs ( (LongBuffer) source, target);
if (source instanceof IntBuffer) return new Integers( (IntBuffer) source, target);
if (source instanceof ShortBuffer) return new Shorts ( (ShortBuffer) source, target);
if (source instanceof ByteBuffer) return new Bytes ( (ByteBuffer) source, target);
throw new IllegalArgumentException();
}
static String toString(Buffer b) {
return (b.getClass().getName()
+ "[pos=" + b.position()
+ " lim=" + b.limit()
+ " cap=" + b.capacity()
+ "]");
}
static String toString(Buffer b) {
return (b.getClass().getName()
+ "[pos=" + b.position()
+ " lim=" + b.limit()
+ " cap=" + b.capacity()
+ "]");
}
static String toString(Buffer b) {
return (b.getClass().getName()
+ "[pos=" + b.position()
+ " lim=" + b.limit()
+ " cap=" + b.capacity()
+ "]");
}
static String toString(Buffer b) {
return (b.getClass().getName()
+ "[pos=" + b.position()
+ " lim=" + b.limit()
+ " cap=" + b.capacity()
+ "]");
}
/**
* Utility method to log important details about buffers.
*/
public static String bufferDetailsToString(final Buffer buffer) {
return "Buffer details: " + "\ncapacity:\t" + buffer.capacity() +
"\nlimit:\t" + buffer.limit() +
"\nremaining:\t" + buffer.remaining() +
"\nposition:\t" + buffer.position() +
"\nbuffer:\t" + buffer.isReadOnly() +
"\nclass:\t" + buffer.getClass();
}
static String toString(Buffer b) {
return (b.getClass().getName()
+ "[pos=" + b.position()
+ " lim=" + b.limit()
+ " cap=" + b.capacity()
+ "]");
}
/**
* Copies the address, position, limit, and capacity of a direct NIO {@link Buffer}.
* Also keeps a reference to it to prevent its memory from getting deallocated.
*
* @param b the Buffer object to reference
*/
public Pointer(final Buffer b) {
if (b != null) {
allocate(b);
}
if (!isNull()) {
position = b.position();
limit = b.limit();
capacity = b.capacity();
deallocator = new Deallocator() { Buffer bb = b; public void deallocate() { bb = null; } };
}
}
static String toString(Buffer b) {
return (b.getClass().getName()
+ "[pos=" + b.position()
+ " lim=" + b.limit()
+ " cap=" + b.capacity()
+ "]");
}
static String toString(Buffer b) {
return (b.getClass().getName()
+ "[pos=" + b.position()
+ " lim=" + b.limit()
+ " cap=" + b.capacity()
+ "]");
}
public static void printCurrentDirectMemory(StringBuilder store) {
long totalHeld = 0;
// make a new set to hold the keys to prevent concurrency issues.
ArrayList<Buffer> bufs = new ArrayList<Buffer>(trackingHash.keySet());
int fBufs = 0, bBufs = 0, iBufs = 0, sBufs = 0, dBufs = 0;
int fBufsM = 0, bBufsM = 0, iBufsM = 0, sBufsM = 0, dBufsM = 0;
for (Buffer b : bufs) {
if (b instanceof ByteBuffer) {
totalHeld += b.capacity();
bBufsM += b.capacity();
bBufs++;
} else if (b instanceof FloatBuffer) {
totalHeld += b.capacity() * 4;
fBufsM += b.capacity() * 4;
fBufs++;
} else if (b instanceof IntBuffer) {
totalHeld += b.capacity() * 4;
iBufsM += b.capacity() * 4;
iBufs++;
} else if (b instanceof ShortBuffer) {
totalHeld += b.capacity() * 2;
sBufsM += b.capacity() * 2;
sBufs++;
} else if (b instanceof DoubleBuffer) {
totalHeld += b.capacity() * 8;
dBufsM += b.capacity() * 8;
dBufs++;
}
}
long heapMem = Runtime.getRuntime().totalMemory()
- Runtime.getRuntime().freeMemory();
boolean printStout = store == null;
if (store == null) {
store = new StringBuilder();
}
store.append("Existing buffers: ").append(bufs.size()).append("\n");
store.append("(b: ").append(bBufs).append(" f: ").append(fBufs).append(" i: ").append(iBufs).append(" s: ").append(sBufs).append(" d: ").append(dBufs).append(")").append("\n");
store.append("Total heap memory held: ").append(heapMem / 1024).append("kb\n");
store.append("Total direct memory held: ").append(totalHeld / 1024).append("kb\n");
store.append("(b: ").append(bBufsM / 1024).append("kb f: ").append(fBufsM / 1024).append("kb i: ").append(iBufsM / 1024).append("kb s: ").append(sBufsM / 1024).append("kb d: ").append(dBufsM / 1024).append("kb)").append("\n");
if (printStout) {
System.out.println(store.toString());
}
}
/** Returns the number of elements between the limit and capacity. */
private static int availableCapacity(Buffer buffer) {
return buffer.capacity() - buffer.limit();
}
/** Returns the number of elements between the limit and capacity. */
private static int availableCapacity(Buffer buffer) {
return buffer.capacity() - buffer.limit();
}
/**
* Returns the number of elements between the limit and capacity.
*/
private static int availableCapacity(Buffer buffer) {
return buffer.capacity() - buffer.limit();
}
/** Returns the number of elements between the limit and capacity. */
private static int availableCapacity(Buffer buffer) {
return buffer.capacity() - buffer.limit();
}