下面列出了java.nio.ByteBuffer#getChar ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public TrueTypeGlyphMapper(TrueTypeFont font) {
this.font = font;
try {
cmap = CMap.initialize(font);
} catch (Exception e) {
cmap = null;
}
if (cmap == null) {
handleBadCMAP();
}
missingGlyph = 0; /* standard for TrueType fonts */
ByteBuffer buffer = font.getTableBuffer(TrueTypeFont.maxpTag);
numGlyphs = buffer.getChar(4); // offset 4 bytes in MAXP table.
if (FontUtilities.isSolaris && isJAlocale && font.supportsJA()) {
needsJAremapping = true;
if (FontUtilities.isSolaris8 &&
getGlyphFromCMAP(JA_WAVE_DASH_CHAR) == missingGlyph) {
remapJAWaveDash = true;
}
} else {
needsJAremapping = false;
}
}
CMapFormat0(ByteBuffer buffer, int offset) {
/* skip 6 bytes of format, length, and version */
int len = buffer.getChar(offset+2);
cmap = new byte[len-6];
buffer.position(offset+6);
buffer.get(cmap);
}
CMapFormat0(ByteBuffer buffer, int offset) {
/* skip 6 bytes of format, length, and version */
int len = buffer.getChar(offset+2);
cmap = new byte[len-6];
buffer.position(offset+6);
buffer.get(cmap);
}
private void setStyle(ByteBuffer os_2Table) {
/* fsSelection is unsigned short at buffer offset 62 */
if (os_2Table == null || os_2Table.capacity() < 64) {
super.setStyle();
return;
}
int fsSelection = os_2Table.getChar(62) & 0xffff;
int italic = fsSelection & fsSelectionItalicBit;
int bold = fsSelection & fsSelectionBoldBit;
int regular = fsSelection & fsSelectionRegularBit;
// System.out.println("platname="+platName+" font="+fullName+
// " family="+familyName+
// " R="+regular+" I="+italic+" B="+bold);
if (regular!=0 && ((italic|bold)!=0)) {
/* This is inconsistent. Try using the font name algorithm */
super.setStyle();
return;
} else if ((regular|italic|bold) == 0) {
/* No style specified. Try using the font name algorithm */
super.setStyle();
return;
}
switch (bold|italic) {
case fsSelectionItalicBit:
style = Font.ITALIC;
break;
case fsSelectionBoldBit:
if (FontUtilities.isSolaris && platName.endsWith("HG-GothicB.ttf")) {
/* Workaround for Solaris's use of a JA font that's marked as
* being designed bold, but is used as a PLAIN font.
*/
style = Font.PLAIN;
} else {
style = Font.BOLD;
}
break;
case fsSelectionBoldBit|fsSelectionItalicBit:
style = Font.BOLD|Font.ITALIC;
}
}
public TrueTypeGlyphMapper(TrueTypeFont font) {
this.font = font;
try {
cmap = CMap.initialize(font);
} catch (Exception e) {
cmap = null;
}
if (cmap == null) {
handleBadCMAP();
}
missingGlyph = 0; /* standard for TrueType fonts */
ByteBuffer buffer = font.getTableBuffer(TrueTypeFont.maxpTag);
if (buffer != null && buffer.capacity() >= 6) {
numGlyphs = buffer.getChar(4); // offset 4 bytes in MAXP table.
} else {
handleBadCMAP();
}
if (FontUtilities.isSolaris && isJAlocale && font.supportsJA()) {
needsJAremapping = true;
if (FontUtilities.isSolaris8 &&
getGlyphFromCMAP(JA_WAVE_DASH_CHAR) == missingGlyph) {
remapJAWaveDash = true;
}
} else {
needsJAremapping = false;
}
}
static CMap createCMap(ByteBuffer buffer, int offset, char[] xlat) {
/* First do a sanity check that this cmap subtable is contained
* within the cmap table.
*/
int subtableFormat = buffer.getChar(offset);
long subtableLength;
if (subtableFormat < 8) {
subtableLength = buffer.getChar(offset+2);
} else {
subtableLength = buffer.getInt(offset+4) & INTMASK;
}
if (offset+subtableLength > buffer.capacity()) {
if (FontUtilities.isLogging()) {
FontUtilities.getLogger().warning("Cmap subtable overflows buffer.");
}
}
switch (subtableFormat) {
case 0: return new CMapFormat0(buffer, offset);
case 2: return new CMapFormat2(buffer, offset, xlat);
case 4: return new CMapFormat4(buffer, offset, xlat);
case 6: return new CMapFormat6(buffer, offset, xlat);
case 8: return new CMapFormat8(buffer, offset, xlat);
case 10: return new CMapFormat10(buffer, offset, xlat);
case 12: return new CMapFormat12(buffer, offset, xlat);
default: throw new RuntimeException("Cmap format unimplemented: " +
(int)buffer.getChar(offset));
}
}
@Override
public void unpack(ByteBuffer buf)
throws IOException {
buf.order(ByteOrder.LITTLE_ENDIAN);
this.v1 = buf.getChar();
this.v2 = buf.getChar();
this.angle=buf.getChar();
this.linedef=buf.getChar();
this.side=buf.getChar();
this.offset=buf.getChar();
}
@Override
public int readDataFromBuffer(ObjectBuilder<String, char[]> objectBuilder,
int currentBufferLocation, DataBuffer dataBuffer) {
int totalDataLength = objectBuilder.getTotalSize();
int startIndex = objectBuilder.getCompletedSize();
int unitSize = Character.BYTES;
startIndex = startIndex / unitSize;
char[] val = objectBuilder.getPartialDataHolder();
//deserializing
int noOfElements = totalDataLength / unitSize;
int bufferPosition = currentBufferLocation;
int bytesRead = 0;
for (int i = startIndex; i < noOfElements; i++) {
ByteBuffer byteBuffer = dataBuffer.getByteBuffer();
int remaining = dataBuffer.getSize() - bufferPosition;
if (remaining >= unitSize) {
val[i] = byteBuffer.getChar(bufferPosition);
bytesRead += unitSize;
bufferPosition += unitSize;
} else {
break;
}
}
if (totalDataLength == bytesRead + startIndex) {
objectBuilder.setFinalObject(new String(val));
}
return bytesRead;
}
@Override
public void unpack(ByteBuffer buf)
throws IOException {
buf.order(ByteOrder.LITTLE_ENDIAN);
this.v1 = buf.getInt();
this.v2 = buf.getInt();
this.angle=buf.getChar();
this.linedef=buf.getChar();
this.side=buf.getChar();
this.offset=buf.getChar();
}
CMapFormat0(ByteBuffer buffer, int offset) {
/* skip 6 bytes of format, length, and version */
int len = buffer.getChar(offset+2);
cmap = new byte[len-6];
buffer.position(offset+6);
buffer.get(cmap);
}
CMapFormat0(ByteBuffer buffer, int offset) {
/* skip 6 bytes of format, length, and version */
int len = buffer.getChar(offset+2);
cmap = new byte[len-6];
buffer.position(offset+6);
buffer.get(cmap);
}
public TrueTypeGlyphMapper(TrueTypeFont font) {
this.font = font;
try {
cmap = CMap.initialize(font);
} catch (Exception e) {
cmap = null;
}
if (cmap == null) {
handleBadCMAP();
}
missingGlyph = 0; /* standard for TrueType fonts */
ByteBuffer buffer = font.getTableBuffer(TrueTypeFont.maxpTag);
if (buffer != null && buffer.capacity() >= 6) {
numGlyphs = buffer.getChar(4); // offset 4 bytes in MAXP table.
} else {
handleBadCMAP();
}
if (FontUtilities.isSolaris && isJAlocale && font.supportsJA()) {
needsJAremapping = true;
if (FontUtilities.isSolaris8 &&
getGlyphFromCMAP(JA_WAVE_DASH_CHAR) == missingGlyph) {
remapJAWaveDash = true;
}
} else {
needsJAremapping = false;
}
}
@Override
public void unpack(ByteBuffer buf)
throws IOException {
buf.order(ByteOrder.LITTLE_ENDIAN);
this.v1 = buf.getChar();
this.v2 = buf.getChar();
this.flags = buf.getShort();
this.special = buf.getShort();
this.tag = buf.getShort();
DoomBuffer.readCharArray(buf, this.sidenum, 2);
}
@Override
public void unpack(ByteBuffer buf)
throws IOException {
buf.order(ByteOrder.LITTLE_ENDIAN);
this.numsegs = buf.getChar();
this.firstseg = buf.getInt();
}
public static void main(String[] args) {
ByteBuffer bb=ByteBuffer.allocate(BSIZE);
int i=0;
while (i++<bb.limit()){
if(bb.get()!=0){
print("nonzero");
}
print("i="+i);
}
bb.rewind();
bb.asCharBuffer().put("Howdy!");
char c;
while ((c=bb.getChar())!=0){
printnb(c+"");
}
print();
bb.rewind();
bb.asShortBuffer().put((short)471142);
print(bb.getShort());
bb.rewind();
bb.asIntBuffer().put(99471142);
print(bb.getShort());
bb.rewind();
bb.asLongBuffer().put(99471142);
print(bb.getLong());
bb.rewind();
bb.asFloatBuffer().put(99471142);
print(bb.getFloat());
bb.rewind();
bb.asDoubleBuffer().put(99471142);
print(bb.getDouble());
bb.rewind();
}
private char[] toChars(byte[] bytes) {
ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
char[] chars = new char[bytes.length / 2];
for (int i = 0; i < chars.length; i++) {
chars[i] = byteBuffer.getChar();
}
return chars;
}
/**
* Parses a positive response to NetBIOS name request query.
* https://tools.ietf.org/html/rfc1002
* Section 4.2.13
*/
static List<String> extractServers(byte[] data, int expectedTransId) throws BrowsingException {
try {
ByteBuffer buffer = ByteBuffer.wrap(data).order(ByteOrder.BIG_ENDIAN);
int transId = buffer.getChar();
if (transId != expectedTransId) {
// This response is not to our broadcast.
if (BuildConfig.DEBUG) Log.d(TAG, "Irrelevant broadcast response");
return Collections.emptyList();
}
skipBytes(buffer, 2); // Skip flags.
skipBytes(buffer, 2); // No questions.
skipBytes(buffer, 2); // Skip answers count.
skipBytes(buffer, 2); // No authority resources.
skipBytes(buffer, 2); // No additional resources.
int nameLength = buffer.get();
skipBytes(buffer, nameLength);
skipBytes(buffer, 1);
int nodeStatus = buffer.getChar();
if (nodeStatus != 0x20 && nodeStatus != 0x21) {
throw new BrowsingException("Received negative response for the broadcast");
}
skipBytes(buffer, 2);
skipBytes(buffer, 4);
skipBytes(buffer, 2);
int addressListEntryCount = buffer.get();
List<String> servers = new ArrayList<>();
for (int i = 0; i < addressListEntryCount; i++) {
byte[] nameArray = new byte[SERVER_NAME_LENGTH];
buffer.get(nameArray, 0, SERVER_NAME_LENGTH);
final String serverName = new String(nameArray, Charset.forName(SERVER_NAME_CHARSET));
final int type = buffer.get();
if (type == FILE_SERVER_NODE_TYPE) {
servers.add(serverName.trim());
}
skipBytes(buffer, 2);
}
return servers;
} catch (BufferUnderflowException e) {
Log.e(TAG, "Malformed incoming packet");
return Collections.emptyList();
}
}
private void setStyle(ByteBuffer os_2Table) {
if (os_2Table == null) {
return;
}
if (os_2Table.capacity() >= 8) {
fontWeight = os_2Table.getChar(4) & 0xffff;
fontWidth = os_2Table.getChar(6) & 0xffff;
}
/* fsSelection is unsigned short at buffer offset 62 */
if (os_2Table.capacity() < 64) {
super.setStyle();
return;
}
int fsSelection = os_2Table.getChar(62) & 0xffff;
int italic = fsSelection & fsSelectionItalicBit;
int bold = fsSelection & fsSelectionBoldBit;
int regular = fsSelection & fsSelectionRegularBit;
// System.out.println("platname="+platName+" font="+fullName+
// " family="+familyName+
// " R="+regular+" I="+italic+" B="+bold);
if (regular!=0 && ((italic|bold)!=0)) {
/* This is inconsistent. Try using the font name algorithm */
super.setStyle();
return;
} else if ((regular|italic|bold) == 0) {
/* No style specified. Try using the font name algorithm */
super.setStyle();
return;
}
switch (bold|italic) {
case fsSelectionItalicBit:
style = Font.ITALIC;
break;
case fsSelectionBoldBit:
if (FontUtilities.isSolaris && platName.endsWith("HG-GothicB.ttf")) {
/* Workaround for Solaris's use of a JA font that's marked as
* being designed bold, but is used as a PLAIN font.
*/
style = Font.PLAIN;
} else {
style = Font.BOLD;
}
break;
case fsSelectionBoldBit|fsSelectionItalicBit:
style = Font.BOLD|Font.ITALIC;
}
}
private void setStyle(ByteBuffer os_2Table) {
if (os_2Table == null) {
return;
}
if (os_2Table.capacity() >= 8) {
fontWeight = os_2Table.getChar(4) & 0xffff;
fontWidth = os_2Table.getChar(6) & 0xffff;
}
/* fsSelection is unsigned short at buffer offset 62 */
if (os_2Table.capacity() < 64) {
super.setStyle();
return;
}
int fsSelection = os_2Table.getChar(62) & 0xffff;
int italic = fsSelection & fsSelectionItalicBit;
int bold = fsSelection & fsSelectionBoldBit;
int regular = fsSelection & fsSelectionRegularBit;
// System.out.println("platname="+platName+" font="+fullName+
// " family="+familyName+
// " R="+regular+" I="+italic+" B="+bold);
if (regular!=0 && ((italic|bold)!=0)) {
/* This is inconsistent. Try using the font name algorithm */
super.setStyle();
return;
} else if ((regular|italic|bold) == 0) {
/* No style specified. Try using the font name algorithm */
super.setStyle();
return;
}
switch (bold|italic) {
case fsSelectionItalicBit:
style = Font.ITALIC;
break;
case fsSelectionBoldBit:
if (FontUtilities.isSolaris && platName.endsWith("HG-GothicB.ttf")) {
/* Workaround for Solaris's use of a JA font that's marked as
* being designed bold, but is used as a PLAIN font.
*/
style = Font.PLAIN;
} else {
style = Font.BOLD;
}
break;
case fsSelectionBoldBit|fsSelectionItalicBit:
style = Font.BOLD|Font.ITALIC;
}
}
@Override
public void readFromBufferAndSet(ByteBuffer byteBuffer, int offset, char[] array, int index) {
array[index] = byteBuffer.getChar(offset);
}