下面列出了java.nio.ByteBuffer#asCharBuffer ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* ByteChannelからchar配列を読み込む
* @param channel
* @return
* @throws IOException
*/
public static char[] readCharArray(@NonNull final ByteChannel channel)
throws IOException {
final int n = readInt(channel);
final ByteBuffer buf = ByteBuffer.allocate(n * 2).order(ByteOrder.BIG_ENDIAN);
final int readBytes = channel.read(buf);
if (readBytes != n * 2) throw new IOException();
buf.clear();
final CharBuffer result = buf.asCharBuffer();
if (result.hasArray()) {
return result.array();
} else {
final char[] b = new char[n];
result.get(b);
return b;
}
}
/**
* Initialize by setting OutputStream.
*/
public YosegiWriter( final OutputStream out , final Configuration config ) throws IOException {
this.out = out;
int blockSize = config.getInt( "block.size" , 1024 * 1024 * 64 );
blockMaker = FindBlockWriter.get(
config.get( "block.maker.class" , PushdownSupportedBlockWriter.class.getName() ) );
blockMaker.setup( blockSize , config );
String blockMakerClassName = BlockReaderNameShortCut
.getShortCutName( blockMaker.getReaderClassName() );
int classNameLength = blockMakerClassName.length() * Character.BYTES;
byte[] header = new byte[MAGIC.length + Integer.BYTES + Integer.BYTES + classNameLength ];
ByteBuffer wrapBuffer = ByteBuffer.wrap( header );
final CharBuffer viewCharBuffer = wrapBuffer.asCharBuffer();
int offset = 0;
wrapBuffer.put( MAGIC , 0 , MAGIC.length );
offset += MAGIC.length;
wrapBuffer.putInt( offset , blockSize );
offset += Integer.BYTES;
wrapBuffer.putInt( offset , classNameLength );
offset += Integer.BYTES;
viewCharBuffer.position( offset / Character.BYTES );
viewCharBuffer.put( blockMakerClassName.toCharArray() );
blockMaker.appendHeader( header );
}
public static void main(String[] args) {
char[] data = "UsingBuffers".toCharArray();
// 一个字符两个字节
ByteBuffer bb = ByteBuffer.allocate(data.length * 2);
CharBuffer cb = bb.asCharBuffer();
cb.put(data);
// 重绕此缓冲区
System.out.println(cb.rewind());
symmetricScaramble(cb);
// 重绕此缓冲区
System.out.println(cb.rewind());
symmetricScaramble(cb);
System.out.println(cb.rewind());
}
private char[] getGaspTable() {
if (gaspTable != null) {
return gaspTable;
}
ByteBuffer buffer = getTableBuffer(gaspTag);
if (buffer == null) {
return gaspTable = new char[0];
}
CharBuffer cbuffer = buffer.asCharBuffer();
char format = cbuffer.get();
/* format "1" has appeared for some Windows Vista fonts.
* Its presently undocumented but the existing values
* seem to be still valid so we can use it.
*/
if (format > 1) { // unrecognised format
return gaspTable = new char[0];
}
char numRanges = cbuffer.get();
if (4+numRanges*4 > getTableSize(gaspTag)) { // sanity check
return gaspTable = new char[0];
}
gaspTable = new char[2*numRanges];
cbuffer.get(gaspTable);
return gaspTable;
}
CMapFormat6(ByteBuffer bbuffer, int offset, char[] xlat) {
bbuffer.position(offset+6);
CharBuffer buffer = bbuffer.asCharBuffer();
firstCode = buffer.get();
entryCount = buffer.get();
glyphIdArray = new char[entryCount];
for (int i=0; i< entryCount; i++) {
glyphIdArray[i] = buffer.get();
}
}
public static void main(String[] args) {
char[] data="UsingBuffers".toCharArray();
ByteBuffer bb=ByteBuffer.allocate(data.length*2);
CharBuffer cb=bb.asCharBuffer();
cb.put(data);
print(cb.rewind());
symmetricScramble(cb);
print(cb.rewind());
symmetricScramble(cb);
print(cb.rewind());
}
CMapFormat10(ByteBuffer bbuffer, int offset, char[] xlat) {
firstCode = bbuffer.getInt() & INTMASK;
entryCount = bbuffer.getInt() & INTMASK;
bbuffer.position(offset+20);
CharBuffer buffer = bbuffer.asCharBuffer();
glyphIdArray = new char[entryCount];
for (int i=0; i< entryCount; i++) {
glyphIdArray[i] = buffer.get();
}
}
CMapFormat10(ByteBuffer bbuffer, int offset, char[] xlat) {
bbuffer.position(offset+12);
firstCode = bbuffer.getInt() & INTMASK;
entryCount = bbuffer.getInt() & INTMASK;
// each glyph is a uint16, so 2 bytes per value.
if (bbuffer.remaining() < (2 * (long)entryCount)) {
throw new RuntimeException("Format 10 table exceeded");
}
CharBuffer buffer = bbuffer.asCharBuffer();
glyphIdArray = new char[entryCount];
for (int i=0; i< entryCount; i++) {
glyphIdArray[i] = buffer.get();
}
}
CMapFormat10(ByteBuffer bbuffer, int offset, char[] xlat) {
firstCode = bbuffer.getInt() & INTMASK;
entryCount = bbuffer.getInt() & INTMASK;
bbuffer.position(offset+20);
CharBuffer buffer = bbuffer.asCharBuffer();
glyphIdArray = new char[entryCount];
for (int i=0; i< entryCount; i++) {
glyphIdArray[i] = buffer.get();
}
}
CMapFormat10(ByteBuffer bbuffer, int offset, char[] xlat) {
bbuffer.position(offset+12);
firstCode = bbuffer.getInt() & INTMASK;
entryCount = bbuffer.getInt() & INTMASK;
// each glyph is a uint16, so 2 bytes per value.
if (bbuffer.remaining() < (2 * (long)entryCount)) {
throw new RuntimeException("Format 10 table exceeded");
}
CharBuffer buffer = bbuffer.asCharBuffer();
glyphIdArray = new char[entryCount];
for (int i=0; i< entryCount; i++) {
glyphIdArray[i] = buffer.get();
}
}
private char[] getGaspTable() {
if (gaspTable != null) {
return gaspTable;
}
ByteBuffer buffer = getTableBuffer(gaspTag);
if (buffer == null) {
return gaspTable = new char[0];
}
CharBuffer cbuffer = buffer.asCharBuffer();
char format = cbuffer.get();
/* format "1" has appeared for some Windows Vista fonts.
* Its presently undocumented but the existing values
* seem to be still valid so we can use it.
*/
if (format > 1) { // unrecognised format
return gaspTable = new char[0];
}
char numRanges = cbuffer.get();
if (4+numRanges*4 > getTableSize(gaspTag)) { // sanity check
return gaspTable = new char[0];
}
gaspTable = new char[2*numRanges];
cbuffer.get(gaspTable);
return gaspTable;
}
public void write(OutputStream output) throws IOException {
baseBuffer.rewind();
checkBuffer.rewind();
tailBuffer.rewind();
int baseCheckSize = Math.min(maxBaseCheckIndex + 64, baseBuffer.capacity());
int tailSize = Math.min(tailIndex - TAIL_OFFSET + 64, tailBuffer.capacity());
DataOutputStream dataOutput = new DataOutputStream(new BufferedOutputStream(output));
dataOutput.writeBoolean(compact);
dataOutput.writeInt(baseCheckSize);
dataOutput.writeInt(tailSize);
WritableByteChannel channel = Channels.newChannel(dataOutput);
ByteBuffer tmpBuffer = ByteBuffer.allocate(baseCheckSize * 4);
IntBuffer tmpIntBuffer = tmpBuffer.asIntBuffer();
tmpIntBuffer.put(baseBuffer.array(), 0, baseCheckSize);
tmpBuffer.rewind();
channel.write(tmpBuffer);
tmpBuffer = ByteBuffer.allocate(baseCheckSize * 4);
tmpIntBuffer = tmpBuffer.asIntBuffer();
tmpIntBuffer.put(checkBuffer.array(), 0, baseCheckSize);
tmpBuffer.rewind();
channel.write(tmpBuffer);
tmpBuffer = ByteBuffer.allocate(tailSize * 2);
CharBuffer tmpCharBuffer = tmpBuffer.asCharBuffer();
tmpCharBuffer.put(tailBuffer.array(), 0, tailSize);
tmpBuffer.rewind();
channel.write(tmpBuffer);
dataOutput.flush();
}
public void testCharBufferSubSequence() throws Exception {
ByteBuffer b = ByteBuffer.allocateDirect(10).order(ByteOrder.nativeOrder());
b.putChar('H');
b.putChar('e');
b.putChar('l');
b.putChar('l');
b.putChar('o');
b.flip();
assertEquals("Hello", b.asCharBuffer().toString());
CharBuffer cb = b.asCharBuffer();
CharSequence cs = cb.subSequence(0, cb.length());
assertEquals("Hello", cs.toString());
}
private void testBuffersIndependentLimit(ByteBuffer b) {
CharBuffer c = b.asCharBuffer();
b.limit(b.capacity()); c.put(1, (char)1);
b.limit(0); c.put(1, (char)1);
b.limit(b.capacity());
ShortBuffer s = b.asShortBuffer();
b.limit(b.capacity()); s.put(1, (short)1);
b.limit(0); s.put(1, (short)1);
b.limit(b.capacity());
IntBuffer i = b.asIntBuffer();
i.put(1, (int)1);
b.limit(0); i.put(1, (int)1);
b.limit(b.capacity());
LongBuffer l = b.asLongBuffer();
l.put(1, (long)1);
b.limit(0); l.put(1, (long)1);
b.limit(b.capacity());
FloatBuffer f = b.asFloatBuffer();
f.put(1, (float)1);
b.limit(0); f.put(1, (float)1);
b.limit(b.capacity());
DoubleBuffer d = b.asDoubleBuffer();
d.put(1, (double)1);
b.limit(0); d.put(1, (double)1);
}
CMapFormat6(ByteBuffer bbuffer, int offset, char[] xlat) {
bbuffer.position(offset+6);
CharBuffer buffer = bbuffer.asCharBuffer();
firstCode = buffer.get();
entryCount = buffer.get();
glyphIdArray = new char[entryCount];
for (int i=0; i< entryCount; i++) {
glyphIdArray[i] = buffer.get();
}
}
CMapFormat2(ByteBuffer buffer, int offset, char[] xlat) {
this.xlat = xlat;
int tableLen = buffer.getChar(offset+2);
buffer.position(offset+6);
CharBuffer cBuffer = buffer.asCharBuffer();
char maxSubHeader = 0;
for (int i=0;i<256;i++) {
subHeaderKey[i] = cBuffer.get();
if (subHeaderKey[i] > maxSubHeader) {
maxSubHeader = subHeaderKey[i];
}
}
/* The value of the subHeaderKey is 8 * the subHeader index,
* so the number of subHeaders can be obtained by dividing
* this value bv 8 and adding 1.
*/
int numSubHeaders = (maxSubHeader >> 3) +1;
firstCodeArray = new char[numSubHeaders];
entryCountArray = new char[numSubHeaders];
idDeltaArray = new short[numSubHeaders];
idRangeOffSetArray = new char[numSubHeaders];
for (int i=0; i<numSubHeaders; i++) {
firstCodeArray[i] = cBuffer.get();
entryCountArray[i] = cBuffer.get();
idDeltaArray[i] = (short)cBuffer.get();
idRangeOffSetArray[i] = cBuffer.get();
// System.out.println("sh["+i+"]:fc="+(int)firstCodeArray[i]+
// " ec="+(int)entryCountArray[i]+
// " delta="+(int)idDeltaArray[i]+
// " offset="+(int)idRangeOffSetArray[i]);
}
int glyphIndexArrSize = (tableLen-518-numSubHeaders*8)/2;
glyphIndexArray = new char[glyphIndexArrSize];
for (int i=0; i<glyphIndexArrSize;i++) {
glyphIndexArray[i] = cBuffer.get();
}
}
CMapFormat10(ByteBuffer bbuffer, int offset, char[] xlat) {
firstCode = bbuffer.getInt() & INTMASK;
entryCount = bbuffer.getInt() & INTMASK;
bbuffer.position(offset+20);
CharBuffer buffer = bbuffer.asCharBuffer();
glyphIdArray = new char[entryCount];
for (int i=0; i< entryCount; i++) {
glyphIdArray[i] = buffer.get();
}
}
private char[] getGaspTable() {
if (gaspTable != null) {
return gaspTable;
}
ByteBuffer buffer = getTableBuffer(gaspTag);
if (buffer == null) {
return gaspTable = new char[0];
}
CharBuffer cbuffer = buffer.asCharBuffer();
char format = cbuffer.get();
/* format "1" has appeared for some Windows Vista fonts.
* Its presently undocumented but the existing values
* seem to be still valid so we can use it.
*/
if (format > 1) { // unrecognised format
return gaspTable = new char[0];
}
char numRanges = cbuffer.get();
if (4+numRanges*4 > getTableSize(gaspTag)) { // sanity check
return gaspTable = new char[0];
}
gaspTable = new char[2*numRanges];
cbuffer.get(gaspTable);
return gaspTable;
}
@Override
public Uint16ArrayData createArrayData(final ByteBuffer nb, final int start, final int end) {
return new Uint16ArrayData(nb.asCharBuffer(), start, end);
}
CMapFormat4(ByteBuffer bbuffer, int offset, char[] xlat) {
this.xlat = xlat;
bbuffer.position(offset);
CharBuffer buffer = bbuffer.asCharBuffer();
buffer.get(); // skip, we already know format=4
int subtableLength = buffer.get();
/* Try to recover from some bad fonts which specify a subtable
* length that would overflow the byte buffer holding the whole
* cmap table. If this isn't a recoverable situation an exception
* may be thrown which is caught higher up the call stack.
* Whilst this may seem lenient, in practice, unless the "bad"
* subtable we are using is the last one in the cmap table we
* would have no way of knowing about this problem anyway.
*/
if (offset+subtableLength > bbuffer.capacity()) {
subtableLength = bbuffer.capacity() - offset;
}
buffer.get(); // skip language
segCount = buffer.get()/2;
int searchRange = buffer.get();
entrySelector = buffer.get();
rangeShift = buffer.get()/2;
startCount = new char[segCount];
endCount = new char[segCount];
idDelta = new short[segCount];
idRangeOffset = new char[segCount];
for (int i=0; i<segCount; i++) {
endCount[i] = buffer.get();
}
buffer.get(); // 2 bytes for reserved pad
for (int i=0; i<segCount; i++) {
startCount[i] = buffer.get();
}
for (int i=0; i<segCount; i++) {
idDelta[i] = (short)buffer.get();
}
for (int i=0; i<segCount; i++) {
char ctmp = buffer.get();
idRangeOffset[i] = (char)((ctmp>>1)&0xffff);
}
/* Can calculate the number of glyph IDs by subtracting
* "pos" from the length of the cmap
*/
int pos = (segCount*8+16)/2;
buffer.position(pos);
int numGlyphIds = (subtableLength/2 - pos);
glyphIds = new char[numGlyphIds];
for (int i=0;i<numGlyphIds;i++) {
glyphIds[i] = buffer.get();
}
/*
System.err.println("segcount="+segCount);
System.err.println("entrySelector="+entrySelector);
System.err.println("rangeShift="+rangeShift);
for (int j=0;j<segCount;j++) {
System.err.println("j="+j+ " sc="+(int)(startCount[j]&0xffff)+
" ec="+(int)(endCount[j]&0xffff)+
" delta="+idDelta[j] +
" ro="+(int)idRangeOffset[j]);
}
//System.err.println("numglyphs="+glyphIds.length);
for (int i=0;i<numGlyphIds;i++) {
System.err.println("gid["+i+"]="+(int)glyphIds[i]);
}
*/
}