| Method from com.lowagie.text.pdf.ByteBuffer Detail: |
public ByteBuffer append(byte[] b) {
return append(b, 0, b.length);
}
Appends an array of bytes. |
public ByteBuffer append(String str) {
if (str != null)
return append(DocWriter.getISOBytes(str));
return this;
}
Appends a String to the buffer. The String is
converted according to the encoding ISO-8859-1. |
public ByteBuffer append(char c) {
return append_i(c);
}
Appends a char to the buffer. The char is
converted according to the encoding ISO-8859-1. |
public ByteBuffer append(ByteBuffer buf) {
return append(buf.buf, 0, buf.count);
}
Appends another ByteBuffer to this buffer. |
public ByteBuffer append(int i) {
return append((double)i);
}
Appends the string representation of an int. |
public ByteBuffer append(byte b) {
return append_i(b);
}
|
public ByteBuffer append(float i) {
return append((double)i);
}
Appends a string representation of a float according
to the Pdf conventions. |
public ByteBuffer append(double d) {
append(formatDouble(d, this));
return this;
}
Appends a string representation of a double according
to the Pdf conventions. |
public ByteBuffer append(byte[] b,
int off,
int len) {
if ((off < 0) || (off > b.length) || (len < 0) ||
((off + len) > b.length) || ((off + len) < 0) || len == 0)
return this;
int newcount = count + len;
if (newcount > buf.length) {
byte newbuf[] = new byte[Math.max(buf.length < < 1, newcount)];
System.arraycopy(buf, 0, newbuf, 0, count);
buf = newbuf;
}
System.arraycopy(b, off, buf, count, len);
count = newcount;
return this;
}
Appends the subarray of the byte array. The buffer will grow by
len bytes. |
public ByteBuffer appendHex(byte b) {
append(bytes[(b > > 4) & 0x0f]);
return append(bytes[b & 0x0f]);
}
|
public ByteBuffer append_i(int b) {
int newcount = count + 1;
if (newcount > buf.length) {
byte newbuf[] = new byte[Math.max(buf.length < < 1, newcount)];
System.arraycopy(buf, 0, newbuf, 0, count);
buf = newbuf;
}
buf[count] = (byte)b;
count = newcount;
return this;
}
Appends an int. The size of the array will grow by one. |
public static void fillCache(int decimals) {
int step = 1;
switch(decimals) {
case 0:
step = 100;
break;
case 1:
step = 10;
break;
}
for (int i = 1; i < byteCacheSize; i += step) {
if (byteCache[i] != null) continue;
byteCache[i] = convertToBytes(i);
}
}
You can fill the cache in advance if you want to. |
public static String formatDouble(double d) {
return formatDouble(d, null);
}
Outputs a double into a format suitable for the PDF. |
public static String formatDouble(double d,
ByteBuffer buf) {
if (HIGH_PRECISION) {
DecimalFormat dn = new DecimalFormat("0.######", dfs);
String sform = dn.format(d);
if (buf == null)
return sform;
else {
buf.append(sform);
return null;
}
}
boolean negative = false;
if (Math.abs(d) < 0.000015) {
if (buf != null) {
buf.append(ZERO);
return null;
} else {
return "0";
}
}
if (d < 0) {
negative = true;
d = -d;
}
if (d < 1.0) {
d += 0.000005;
if (d >= 1) {
if (negative) {
if (buf != null) {
buf.append((byte)'-');
buf.append((byte)'1');
return null;
} else {
return "-1";
}
} else {
if (buf != null) {
buf.append((byte)'1');
return null;
} else {
return "1";
}
}
}
if (buf != null) {
int v = (int) (d * 100000);
if (negative) buf.append((byte)'-');
buf.append((byte)'0');
buf.append((byte)'.');
buf.append( (byte)(v / 10000 + ZERO) );
if (v % 10000 != 0) {
buf.append( (byte)((v / 1000) % 10 + ZERO) );
if (v % 1000 != 0) {
buf.append( (byte)((v / 100) % 10 + ZERO) );
if (v % 100 != 0) {
buf.append((byte)((v / 10) % 10 + ZERO) );
if (v % 10 != 0) {
buf.append((byte)((v) % 10 + ZERO) );
}
}
}
}
return null;
} else {
int x = 100000;
int v = (int) (d * x);
StringBuffer res = new StringBuffer();
if (negative) res.append('-');
res.append("0.");
while( v < x/10 ) {
res.append('0');
x /= 10;
}
res.append(v);
int cut = res.length() - 1;
while (res.charAt(cut) == '0') {
--cut;
}
res.setLength(cut + 1);
return res.toString();
}
} else if (d < = 32767) {
d += 0.005;
int v = (int) (d * 100);
if (v < byteCacheSize && byteCache[v] != null) {
if (buf != null) {
if (negative) buf.append((byte)'-');
buf.append(byteCache[v]);
return null;
} else {
String tmp = PdfEncodings.convertToString(byteCache[v], null);
if (negative) tmp = "-" + tmp;
return tmp;
}
}
if (buf != null) {
if (v < byteCacheSize) {
//create the cachebyte[]
byte[] cache;
int size = 0;
if (v >= 1000000) {
//the original number is >=10000, we need 5 more bytes
size += 5;
} else if (v >= 100000) {
//the original number is >=1000, we need 4 more bytes
size += 4;
} else if (v >= 10000) {
//the original number is >=100, we need 3 more bytes
size += 3;
} else if (v >= 1000) {
//the original number is >=10, we need 2 more bytes
size += 2;
} else if (v >= 100) {
//the original number is >=1, we need 1 more bytes
size += 1;
}
//now we must check if we have a decimal number
if (v % 100 != 0) {
//yes, do not forget the "."
size += 2;
}
if (v % 10 != 0) {
size++;
}
cache = new byte[size];
int add = 0;
if (v >= 1000000) {
cache[add++] = bytes[(v / 1000000)];
}
if (v >= 100000) {
cache[add++] = bytes[(v / 100000) % 10];
}
if (v >= 10000) {
cache[add++] = bytes[(v / 10000) % 10];
}
if (v >= 1000) {
cache[add++] = bytes[(v / 1000) % 10];
}
if (v >= 100) {
cache[add++] = bytes[(v / 100) % 10];
}
if (v % 100 != 0) {
cache[add++] = (byte)'.';
cache[add++] = bytes[(v / 10) % 10];
if (v % 10 != 0) {
cache[add++] = bytes[v % 10];
}
}
byteCache[v] = cache;
}
if (negative) buf.append((byte)'-');
if (v >= 1000000) {
buf.append( bytes[(v / 1000000)] );
}
if (v >= 100000) {
buf.append( bytes[(v / 100000) % 10] );
}
if (v >= 10000) {
buf.append( bytes[(v / 10000) % 10] );
}
if (v >= 1000) {
buf.append( bytes[(v / 1000) % 10] );
}
if (v >= 100) {
buf.append( bytes[(v / 100) % 10] );
}
if (v % 100 != 0) {
buf.append((byte)'.');
buf.append( bytes[(v / 10) % 10] );
if (v % 10 != 0) {
buf.append( bytes[v % 10] );
}
}
return null;
} else {
StringBuffer res = new StringBuffer();
if (negative) res.append('-');
if (v >= 1000000) {
res.append( chars[(v / 1000000)] );
}
if (v >= 100000) {
res.append( chars[(v / 100000) % 10] );
}
if (v >= 10000) {
res.append( chars[(v / 10000) % 10] );
}
if (v >= 1000) {
res.append( chars[(v / 1000) % 10] );
}
if (v >= 100) {
res.append( chars[(v / 100) % 10] );
}
if (v % 100 != 0) {
res.append('.');
res.append( chars[(v / 10) % 10] );
if (v % 10 != 0) {
res.append( chars[v % 10] );
}
}
return res.toString();
}
} else {
StringBuffer res = new StringBuffer();
if (negative) res.append('-');
d += 0.5;
long v = (long) d;
return res.append(v).toString();
}
}
Outputs a double into a format suitable for the PDF. |
public byte[] getBuffer() {
return buf;
}
|
public void reset() {
count = 0;
}
|
public static void setCacheSize(int size) {
if (size > 3276700) size = 3276700;
if (size < = byteCacheSize) return;
byte[][] tmpCache = new byte[size][];
System.arraycopy(byteCache, 0, tmpCache, 0, byteCacheSize);
byteCache = tmpCache;
byteCacheSize = size;
}
Sets the cache size.
This can only be used to increment the size.
If the size that is passed through is smaller than the current size, nothing happens. |
public void setSize(int size) {
if (size > count || size < 0)
throw new IndexOutOfBoundsException("The new size must be positive and < = of the current size");
count = size;
}
|
public int size() {
return count;
}
Returns the current size of the buffer. |
public byte[] toByteArray() {
byte newbuf[] = new byte[count];
System.arraycopy(buf, 0, newbuf, 0, count);
return newbuf;
}
Creates a newly allocated byte array. Its size is the current
size of this output stream and the valid contents of the buffer
have been copied into it. |
public String toString() {
return new String(buf, 0, count);
}
Converts the buffer's contents into a string, translating bytes into
characters according to the platform's default character encoding. |
public String toString(String enc) throws UnsupportedEncodingException {
return new String(buf, 0, count, enc);
}
Converts the buffer's contents into a string, translating bytes into
characters according to the specified character encoding. |
public void write(int b) throws IOException {
append((byte)b);
}
|
public void write(byte[] b,
int off,
int len) {
append(b, off, len);
}
|
public void writeTo(OutputStream out) throws IOException {
out.write(buf, 0, count);
}
Writes the complete contents of this byte buffer output to
the specified output stream argument, as if by calling the output
stream's write method using out.write(buf, 0, count). |