下面列出了java.nio.CharBuffer#capacity ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Return the char at the specified offset within the buffer.
*/
@Override
public char charAt(int where) {
if(VERBOSE_LOG) Logger.debug(TAG + ": in charAt");
CharBuffer cb = _sb.getCharBuffer();
if(cb == null)
return ' ';
cb.clear();
int len = cb.capacity() - mGapLength;
if (where < 0) {
throw new IndexOutOfBoundsException("charAt: " + where + " < 0");
} else if (where >= len) {
throw new IndexOutOfBoundsException("charAt: " + where + " >= length " + len);
}
if (where >= mGapStart)
return cb.charAt(where + mGapLength);
else
return cb.charAt(where);
}
static CharBuffer put(CharBuffer buffer, CharArraySequence data) {
final int length = data.length();
if (buffer == null || buffer.capacity() < length) {
buffer = CharBuffer.allocate(length);
}
buffer.clear();
if (length > 0) {
data.putToBuffer(buffer);
}
buffer.flip();
return buffer;
}
/**
* Randomize the char buffer's position and limit.
*/
static CharBuffer randomizeRange(CharBuffer cb) {
int mid = cb.capacity() >>> 1;
int start = RAND.nextInt(mid + 1); // from 0 to mid
int end = mid + RAND.nextInt(cb.capacity() - mid + 1); // from mid to capacity
cb.position(start);
cb.limit(end);
return cb;
}
/**
* Randomize the char buffer's position and limit.
*/
static CharBuffer randomizeRange(CharBuffer cb) {
int mid = cb.capacity() >>> 1;
int start = RAND.nextInt(mid + 1); // from 0 to mid
int end = mid + RAND.nextInt(cb.capacity() - mid + 1); // from mid to capacity
cb.position(start);
cb.limit(end);
return cb;
}
/**
* Randomize the char buffer's position and limit.
*/
static CharBuffer randomizeRange(CharBuffer cb) {
int mid = cb.capacity() >>> 1;
int start = RAND.nextInt(mid);
int end = mid + RAND.nextInt(mid);
cb.position(start);
cb.limit(end);
return cb;
}
/**
* getJWT should return the user JWT associated with this connection.
* This can return null for challenge only authentication, but for account/user
* JWT-based authentication you need to return the JWT bytes here.
*
* @return the user JWT
*/
public char[] getJWT() {
try {
char[] jwtChars = null;
String fileToUse = this.jwtFile;
if (this.credsFile != null) {
fileToUse = this.credsFile;
}
// If no file is provided, assume this is challenge only authentication
// and simply return null here.
if (fileToUse == null) {
return null;
}
byte[] data = Files.readAllBytes(Paths.get(fileToUse));
ByteBuffer bb = ByteBuffer.wrap(data);
CharBuffer chars = StandardCharsets.UTF_8.decode(bb);
jwtChars = this.extract(chars, 1); // jwt is always first
// Clear things up as best we can
chars.clear();
for (int i=0; i<chars.capacity();i++) {
chars.put('\0');
}
bb.clear();
for (int i=0;i<data.length;i++) {
data[i] = 0;
}
return jwtChars;
} catch (Exception exp) {
throw new IllegalStateException("problem reading jwt", exp);
}
}
/**
* Randomize the char buffer's position and limit.
*/
static CharBuffer randomizeRange(CharBuffer cb) {
int mid = cb.capacity() >>> 1;
int start = RAND.nextInt(mid);
int end = mid + RAND.nextInt(mid);
cb.position(start);
cb.limit(end);
return cb;
}