* Copyright (C) 2010 Robert Futrell
* robert_futrell at users.sourceforge.net
* http://fifesoft.com/rsyntaxtextarea
* This library is distributed under a modified BSD license. See the included
* RSTALanguageSupport.License.txt file for details.
package org.fife.rsta.ac.java.classreader.constantpool;
import java.io.UnsupportedEncodingException;
* Class representing a <code>CONSTANT_Utf8_info</code> structure.
public class ConstantUtf8Info extends ConstantPoolInfo {
private String representedString;
public ConstantUtf8Info(byte[] bytes) {
representedString = createRepresentedString(bytes);
// public byte[] getBytes() {
private static final boolean isBitSet(int b, int bit) {
private String createRepresentedString(byte[] bytes) {
StringBuffer sb = new StringBuffer();
while (pos<bytes.length) {
// A single-byte char, '\u0001' to '\u007F'.
sb.append((char)(b&0x7f));
// null char ('\u0000') or char in range '\u0080' - '\u07FF'.
// ch = ((x & 0x1f) << 6) + (y & 0x3f)
char ch = (char)(((x&0x1f)<<6) + (y&0x3f));
// chars in range '\u0800' - '\uFFFF'.
// ch = ((x & 0xf) << 12) + ((y & 0x3f) << 6) + (z & 0x3f)
char ch = (char)(((x&0xf)<<12) + ((y&0x3f)<<6) + (z&0x3f));
// An unknown bit header.
throw new InternalError("Unknown bit header in Utf8 string: " +
representedString = sb.toString();
representedString = new String(bytes, "UTF-8");
} catch (UnsupportedEncodingException uee) { // Never happens.
return representedString;
* Returns the string represented by this info.
* @param quoted Whether to add enclosing quotation marks, and "escape"
* any quotation marks in the represented string.
* @return The string represented.
public String getRepresentedString(boolean quoted) {
return representedString;
String temp = "\"" + representedString.replaceAll("\"", "\\\"") + "\"";
* Returns a string representation of this object. Useful for debugging.
* @return A string representation of this object.
public String toString() {
return "[ConstantUtf8Info: " + representedString +