enhanced template engine

git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@7783 6c8d7289-2bf4-0310-a012-ef5d649a1542
pull/1/head
orbiter 14 years ago
parent aa6c32d753
commit f30d36b101

@ -147,6 +147,13 @@ public final class TemplateEngine {
public final static byte[] ul = "_".getBytes(); public final static byte[] ul = "_".getBytes();
private final static byte[] alternative_which = ASCII.getBytes(" type=\"alternative\" which=\"");
private final static byte[] multi_num = ASCII.getBytes(" type=\"multi\" num=\"");
private final static byte[] open_endtag = ASCII.getBytes("</");
private final static byte[] close_quotetagn = ASCII.getBytes("\">\n");
private final static byte[] close_tagn = ASCII.getBytes(">\n");
/** /**
* transfer until a specified pattern is found; everything but the pattern is transfered so far * transfer until a specified pattern is found; everything but the pattern is transfered so far
* the function returns true, if the pattern is found * the function returns true, if the pattern is found
@ -237,16 +244,16 @@ public final class TemplateEngine {
structure.append('<') structure.append('<')
.append(multi_key) .append(multi_key)
.append(UTF8.getBytes(" type=\"multi\" num=\"")) .append(multi_num)
.append(UTF8.getBytes(Integer.toString(num))) .append(ASCII.getBytes(Integer.toString(num)))
.append(UTF8.getBytes("\">\n")); .append(close_quotetagn);
for(int i=0;i < num;i++) { for(int i=0;i < num;i++) {
final PushbackInputStream pis2 = new PushbackInputStream(new ByteArrayInputStream(text)); final PushbackInputStream pis2 = new PushbackInputStream(new ByteArrayInputStream(text));
//System.out.println("recursing with text(prefix="+ multi_key + "_" + i + "_" +"):"); //DEBUG //System.out.println("recursing with text(prefix="+ multi_key + "_" + i + "_" +"):"); //DEBUG
//System.out.println(text); //System.out.println(text);
structure.append(writeTemplate(pis2, out, pattern, dflt, newPrefix(prefix,multi_key,i))); structure.append(writeTemplate(pis2, out, pattern, dflt, newPrefix(prefix,multi_key,i)));
}//for }//for
structure.append(UTF8.getBytes("</")).append(multi_key).append(UTF8.getBytes(">\n")); structure.append(open_endtag).append(multi_key).append(close_tagn);
} else {//transferUntil } else {//transferUntil
Log.logSevere("TEMPLATE", "No Close Key found for #{"+UTF8.String(multi_key)+"}#"); //prefix here? Log.logSevere("TEMPLATE", "No Close Key found for #{"+UTF8.String(multi_key)+"}#"); //prefix here?
} }
@ -283,7 +290,7 @@ public final class TemplateEngine {
PushbackInputStream pis2; PushbackInputStream pis2;
if (byName) { if (byName) {
//TODO: better Error Handling //TODO: better Error Handling
transferUntil(pis, keyStream, appendBytes(UTF8.getBytes("%%"), patternName, null, null)); transferUntil(pis, keyStream, appendBytes(ASCII.getBytes("%%"), patternName, null, null));
if(pis.available()==0){ if(pis.available()==0){
Log.logSevere("TEMPLATE", "No such Template: %%" + UTF8.String(patternName)); Log.logSevere("TEMPLATE", "No such Template: %%" + UTF8.String(patternName));
return structure.getBytes(); return structure.getBytes();
@ -292,7 +299,7 @@ public final class TemplateEngine {
transferUntil(pis, keyStream, dpdpa); transferUntil(pis, keyStream, dpdpa);
pis2 = new PushbackInputStream(new ByteArrayInputStream(keyStream.toByteArray())); pis2 = new PushbackInputStream(new ByteArrayInputStream(keyStream.toByteArray()));
structure.append(writeTemplate(pis2, out, pattern, dflt, newPrefix(prefix,key))); structure.append(writeTemplate(pis2, out, pattern, dflt, newPrefix(prefix,key)));
transferUntil(pis, keyStream, appendBytes(UTF8.getBytes("#(/"), key, UTF8.getBytes(")#"), null)); transferUntil(pis, keyStream, appendBytes(ASCII.getBytes("#(/"), key, ASCII.getBytes(")#"), null));
if(pis.available()==0){ if(pis.available()==0){
Log.logSevere("TEMPLATE", "No Close Key found for #("+UTF8.String(key)+")# (by Name)"); Log.logSevere("TEMPLATE", "No Close Key found for #("+UTF8.String(key)+")# (by Name)");
} }
@ -308,16 +315,16 @@ public final class TemplateEngine {
if (java.util.Arrays.equals(keyStream.toByteArray(),appendBytes(slashChar, key, null,null))) { if (java.util.Arrays.equals(keyStream.toByteArray(),appendBytes(slashChar, key, null,null))) {
pis2 = new PushbackInputStream(new ByteArrayInputStream(text.getBytes())); pis2 = new PushbackInputStream(new ByteArrayInputStream(text.getBytes()));
//this maybe the wrong, but its the last //this maybe the wrong, but its the last
structure.append('<').append(key).append(UTF8.getBytes(" type=\"alternative\" which=\"")).append(UTF8.getBytes(Integer.toString(whichPattern))).append(UTF8.getBytes("\" found=\"0\">\n")); structure.append('<').append(key).append(alternative_which).append(ASCII.getBytes(Integer.toString(whichPattern))).append(ASCII.getBytes("\" found=\"0\">\n"));
structure.append(writeTemplate(pis2, out, pattern, dflt, newPrefix(prefix,key))); structure.append(writeTemplate(pis2, out, pattern, dflt, newPrefix(prefix,key)));
structure.append(UTF8.getBytes("</")).append(key).append(UTF8.getBytes(">\n")); structure.append(open_endtag).append(key).append(close_tagn);
found=true; found=true;
}else if(others >0 && keyStream.toString().startsWith("/")){ //close nested }else if(others >0 && keyStream.toString().startsWith("/")){ //close nested
others--; others--;
text.append(aOpen).append(keyStream.toByteArray()).append(UTF8.getBytes(")#")); text.append(aOpen).append(keyStream.toByteArray()).append(ASCII.getBytes(")#"));
} else { //nested } else { //nested
others++; others++;
text.append(aOpen).append(keyStream.toByteArray()).append(UTF8.getBytes(")#")); text.append(aOpen).append(keyStream.toByteArray()).append(ASCII.getBytes(")#"));
} }
keyStream.reset(); //reset stream keyStream.reset(); //reset stream
continue; continue;
@ -330,11 +337,11 @@ public final class TemplateEngine {
if ((bb & 0xFF) == ':'){ if ((bb & 0xFF) == ':'){
if(currentPattern == whichPattern){ //found the pattern if(currentPattern == whichPattern){ //found the pattern
pis2 = new PushbackInputStream(new ByteArrayInputStream(text.getBytes())); pis2 = new PushbackInputStream(new ByteArrayInputStream(text.getBytes()));
structure.append('<').append(key).append(UTF8.getBytes(" type=\"alternative\" which=\"")).append(UTF8.getBytes(Integer.toString(whichPattern))).append(UTF8.getBytes("\" found=\"0\">\n")); structure.append('<').append(key).append(alternative_which).append(ASCII.getBytes(Integer.toString(whichPattern))).append(ASCII.getBytes("\" found=\"0\">\n"));
structure.append(writeTemplate(pis2, out, pattern, dflt, newPrefix(prefix,key))); structure.append(writeTemplate(pis2, out, pattern, dflt, newPrefix(prefix,key)));
structure.append(UTF8.getBytes("</")).append(key).append(UTF8.getBytes(">\n")); structure.append(open_endtag).append(key).append(close_tagn);
transferUntil(pis, keyStream, appendBytes(UTF8.getBytes("#(/"),key,UTF8.getBytes(")#"),null));//to #(/key)#. transferUntil(pis, keyStream, appendBytes(ASCII.getBytes("#(/"),key,ASCII.getBytes(")#"),null));//to #(/key)#.
found=true; found=true;
} }
@ -342,7 +349,7 @@ public final class TemplateEngine {
text.clear(); text.clear();
continue; continue;
} }
text.append(UTF8.getBytes(":")); text.append(':');
} }
if(!found){ if(!found){
text.append((byte)bb);/* text.append((byte)bb);/*
@ -361,11 +368,11 @@ public final class TemplateEngine {
key = keyStream.toByteArray(); key = keyStream.toByteArray();
final String patternKey = getPatternKey(prefix, key); final String patternKey = getPatternKey(prefix, key);
replacement = replacePattern(patternKey, pattern, dflt); //replace replacement = replacePattern(patternKey, pattern, dflt); //replace
structure.append(ASCII.getBytes("<")).append(key) structure.append('<').append(key)
.append(ASCII.getBytes(" type=\"normal\">\n")); .append(ASCII.getBytes(" type=\"normal\">\n"));
structure.append(replacement); structure.append(replacement);
structure.append(ASCII.getBytes("</")).append(key) structure.append(ASCII.getBytes("</")).append(key)
.append(ASCII.getBytes(">\n")); .append(close_tagn);
FileUtils.copy(replacement, out); FileUtils.copy(replacement, out);
} else { } else {
@ -395,7 +402,7 @@ public final class TemplateEngine {
//Read the Include //Read the Include
String line = ""; String line = "";
while ((line = br.readLine()) != null) { while ((line = br.readLine()) != null) {
include.append(UTF8.getBytes(line)).append(UTF8.getBytes(de.anomic.server.serverCore.CRLF_STRING)); include.append(UTF8.getBytes(line)).append(ASCII.getBytes(de.anomic.server.serverCore.CRLF_STRING));
} }
} catch (final IOException e) { } catch (final IOException e) {
//file not found? //file not found?
@ -404,9 +411,9 @@ public final class TemplateEngine {
if (br != null) try { br.close(); br=null; } catch (final Exception e) {} if (br != null) try { br.close(); br=null; } catch (final Exception e) {}
} }
final PushbackInputStream pis2 = new PushbackInputStream(new ByteArrayInputStream(include.getBytes())); final PushbackInputStream pis2 = new PushbackInputStream(new ByteArrayInputStream(include.getBytes()));
structure.append(UTF8.getBytes("<fileinclude file=\"")).append(filename).append(UTF8.getBytes(">\n")); structure.append(ASCII.getBytes("<fileinclude file=\"")).append(filename).append(close_tagn);
structure.append(writeTemplate(pis2, out, pattern, dflt, prefix)); structure.append(writeTemplate(pis2, out, pattern, dflt, prefix));
structure.append(UTF8.getBytes("</fileinclude>\n")); structure.append(ASCII.getBytes("</fileinclude>\n"));
} }
} }
@ -453,7 +460,7 @@ public final class TemplateEngine {
private final static byte[] newPrefix(final byte[] oldPrefix, final byte[] multi_key, final int i) { private final static byte[] newPrefix(final byte[] oldPrefix, final byte[] multi_key, final int i) {
final ByteBuffer newPrefix = new ByteBuffer(oldPrefix.length + multi_key.length + 8); final ByteBuffer newPrefix = new ByteBuffer(oldPrefix.length + multi_key.length + 8);
newPrefix.append(oldPrefix).append(multi_key).append(ul).append(UTF8.getBytes(Integer.toString(i))).append(ul); newPrefix.append(oldPrefix).append(multi_key).append(ul).append(ASCII.getBytes(Integer.toString(i))).append(ul);
try { try {
newPrefix.close(); newPrefix.close();
} catch (final IOException e) { } catch (final IOException e) {

@ -1,4 +1,4 @@
// ByteBuffer.java // ByteBuffer.java
// --------------------------- // ---------------------------
// (C) by Michael Peter Christen; mc@yacy.net // (C) by Michael Peter Christen; mc@yacy.net
// first published on http://www.anomic.de // first published on http://www.anomic.de
@ -38,73 +38,73 @@ import java.util.Properties;
import net.yacy.cora.document.UTF8; import net.yacy.cora.document.UTF8;
public final class ByteBuffer extends OutputStream { public final class ByteBuffer extends OutputStream {
public static final byte singlequote = (byte) 39; public static final byte singlequote = (byte) 39;
public static final byte doublequote = (byte) 34; public static final byte doublequote = (byte) 34;
public static final byte equal = (byte) '='; public static final byte equal = (byte) '=';
private byte[] buffer; private byte[] buffer;
private int offset; private int offset;
private int length; private int length;
public ByteBuffer() { public ByteBuffer() {
buffer = new byte[10]; this.buffer = new byte[10];
length = 0; this.length = 0;
offset = 0; this.offset = 0;
} }
public ByteBuffer(final int initLength) { public ByteBuffer(final int initLength) {
this.buffer = new byte[initLength]; this.buffer = new byte[initLength];
this.length = 0; this.length = 0;
this.offset = 0; this.offset = 0;
} }
public ByteBuffer(final byte[] bb) { public ByteBuffer(final byte[] bb) {
buffer = bb; this.buffer = bb;
length = bb.length; this.length = bb.length;
offset = 0; this.offset = 0;
} }
public ByteBuffer(final String s) { public ByteBuffer(final String s) {
buffer = s.getBytes(UTF8.charset); this.buffer = s.getBytes(UTF8.charset);
length = buffer.length; this.length = this.buffer.length;
offset = 0; this.offset = 0;
} }
public ByteBuffer(final byte[] bb, final int initLength) { public ByteBuffer(final byte[] bb, final int initLength) {
this.buffer = new byte[initLength]; this.buffer = new byte[initLength];
System.arraycopy(bb, 0, buffer, 0, bb.length); System.arraycopy(bb, 0, this.buffer, 0, bb.length);
length = bb.length; this.length = bb.length;
offset = 0; this.offset = 0;
} }
public ByteBuffer(final byte[] bb, final int of, final int le) { public ByteBuffer(final byte[] bb, final int of, final int le) {
if (of * 2 > bb.length) { if (of * 2 > bb.length) {
buffer = new byte[le]; this.buffer = new byte[le];
System.arraycopy(bb, of, buffer, 0, le); System.arraycopy(bb, of, this.buffer, 0, le);
length = le; this.length = le;
offset = 0; this.offset = 0;
} else { } else {
buffer = bb; this.buffer = bb;
length = le; this.length = le;
offset = of; this.offset = of;
} }
} }
public ByteBuffer(final ByteBuffer bb) { public ByteBuffer(final ByteBuffer bb) {
buffer = bb.buffer; this.buffer = bb.buffer;
length = bb.length; this.length = bb.length;
offset = bb.offset; this.offset = bb.offset;
} }
public ByteBuffer(final File f) throws IOException { public ByteBuffer(final File f) throws IOException {
// initially fill the byte buffer with the content of a file // initially fill the byte buffer with the content of a file
if (f.length() > Integer.MAX_VALUE) throw new IOException("file is too large for buffering"); if (f.length() > Integer.MAX_VALUE) throw new IOException("file is too large for buffering");
length = (int) f.length(); this.length = (int) f.length();
buffer = new byte[length]; this.buffer = new byte[this.length];
offset = 0; this.offset = 0;
try { try {
final FileInputStream fis = new FileInputStream(f); final FileInputStream fis = new FileInputStream(f);
@ -113,7 +113,7 @@ public final class ByteBuffer extends OutputStream {
// while ((l = fis.read(buf)) > 0) { // while ((l = fis.read(buf)) > 0) {
// System.arraycopy(buf, 0, buffer, p, l); // System.arraycopy(buf, 0, buffer, p, l);
// p += l; // p += l;
/*int l =*/ fis.read(buffer); /*int l =*/ fis.read(this.buffer);
// } // }
fis.close(); fis.close();
} catch (final FileNotFoundException e) { } catch (final FileNotFoundException e) {
@ -126,68 +126,77 @@ public final class ByteBuffer extends OutputStream {
this.length = 0; this.length = 0;
this.offset = 0; this.offset = 0;
} }
public int length() { public int length() {
return length; return this.length;
} }
private void grow() { private void grow() {
int newsize = buffer.length * 2 + 1; int newsize = this.buffer.length * 2 + 1;
if (newsize < 256) newsize = 256; if (newsize < 256) newsize = 256;
byte[] tmp = new byte[newsize]; final byte[] tmp = new byte[newsize];
System.arraycopy(buffer, offset, tmp, 0, length); System.arraycopy(this.buffer, this.offset, tmp, 0, this.length);
buffer = tmp; this.buffer = tmp;
offset = 0; this.offset = 0;
} }
public void write(final int b) { public void write(final int b) {
write((byte) (b & 0xff)); write((byte) (b & 0xff));
} }
public void write(final char b) {
write((byte) b);
}
public void write(final byte b) { public void write(final byte b) {
if (offset + length + 1 > buffer.length) grow(); if (this.offset + this.length + 1 > this.buffer.length) grow();
buffer[offset + length++] = b; this.buffer[this.offset + this.length++] = b;
} }
@Override @Override
public void write(final byte[] bb) { public void write(final byte[] bb) {
write(bb, 0, bb.length); write(bb, 0, bb.length);
} }
@Override @Override
public void write(final byte[] bb, final int of, final int le) { public void write(final byte[] bb, final int of, final int le) {
while (offset + length + le > buffer.length) grow(); while (this.offset + this.length + le > this.buffer.length) grow();
System.arraycopy(bb, of, buffer, offset + length, le); System.arraycopy(bb, of, this.buffer, this.offset + this.length, le);
length += le; this.length += le;
} }
// overwrite does not increase the 'length' write position pointer! // overwrite does not increase the 'length' write position pointer!
public void overwrite(final int pos, final int b) { public void overwrite(final int pos, final int b) {
overwrite(pos, (byte) (b & 0xff)); overwrite(pos, (byte) (b & 0xff));
} }
public void overwrite(final int pos, final byte b) { public void overwrite(final int pos, final byte b) {
if (offset + pos + 1 > buffer.length) grow(); if (this.offset + pos + 1 > this.buffer.length) grow();
buffer[offset + pos] = b; this.buffer[this.offset + pos] = b;
if (pos >= length) length = pos + 1; if (pos >= this.length) this.length = pos + 1;
} }
public void overwrite(final int pos, final byte[] bb) { public void overwrite(final int pos, final byte[] bb) {
overwrite(pos, bb, 0, bb.length); overwrite(pos, bb, 0, bb.length);
} }
public void overwrite(final int pos, final byte[] bb, final int of, final int le) { public void overwrite(final int pos, final byte[] bb, final int of, final int le) {
while (offset + pos + le > buffer.length) grow(); while (this.offset + pos + le > this.buffer.length) grow();
System.arraycopy(bb, of, buffer, offset + pos, le); System.arraycopy(bb, of, this.buffer, this.offset + pos, le);
if (pos + le > length) length = pos + le; if (pos + le > this.length) this.length = pos + le;
} }
public ByteBuffer append(final byte b) { public ByteBuffer append(final byte b) {
write(b); write(b);
return this; return this;
} }
public ByteBuffer append(final char b) {
write(b);
return this;
}
public ByteBuffer append(final int i) { public ByteBuffer append(final int i) {
write((byte) (i & 0xFF)); write((byte) (i & 0xFF));
return this; return this;
@ -206,10 +215,10 @@ public final class ByteBuffer extends OutputStream {
public ByteBuffer append(final String s) { public ByteBuffer append(final String s) {
return append(UTF8.getBytes(s)); return append(UTF8.getBytes(s));
} }
public ByteBuffer append(final String s, final String charset) throws UnsupportedEncodingException { public ByteBuffer append(final String s, final String charset) throws UnsupportedEncodingException {
return append(s.getBytes(charset)); return append(s.getBytes(charset));
} }
public ByteBuffer append(final ByteBuffer bb) { public ByteBuffer append(final ByteBuffer bb) {
return append(bb.buffer, bb.offset, bb.length); return append(bb.buffer, bb.offset, bb.length);
@ -220,22 +229,22 @@ public final class ByteBuffer extends OutputStream {
if (o instanceof byte[]) return append((byte[]) o); if (o instanceof byte[]) return append((byte[]) o);
return null; return null;
} }
public byte byteAt(final int pos) { public byte byteAt(final int pos) {
if (pos > length) return -1; if (pos > this.length) return -1;
return buffer[offset + pos]; return this.buffer[this.offset + pos];
} }
public void deleteByteAt(final int pos) { public void deleteByteAt(final int pos) {
if (pos < 0) return; if (pos < 0) return;
if (pos >= length) return; if (pos >= this.length) return;
if (pos == length - 1) { if (pos == this.length - 1) {
length--; this.length--;
} else { } else {
System.arraycopy(buffer, offset + pos + 1, buffer, offset + pos, length - pos - 1); System.arraycopy(this.buffer, this.offset + pos + 1, this.buffer, this.offset + pos, this.length - pos - 1);
} }
} }
public int indexOf(final byte b) { public int indexOf(final byte b) {
return indexOf(b, 0); return indexOf(b, 0);
} }
@ -245,22 +254,22 @@ public final class ByteBuffer extends OutputStream {
} }
public int indexOf(final byte b, final int start) { public int indexOf(final byte b, final int start) {
if (start >= length) return -1; if (start >= this.length) return -1;
for (int i = start; i < length; i++) if (buffer[offset + i] == b) return i; for (int i = start; i < this.length; i++) if (this.buffer[this.offset + i] == b) return i;
return -1; return -1;
} }
public int indexOf(final byte[] bs, final int start) { public int indexOf(final byte[] bs, final int start) {
if (start + bs.length > length) return -1; if (start + bs.length > this.length) return -1;
loop: for (int i = start; i <= length - bs.length; i++) { loop: for (int i = start; i <= this.length - bs.length; i++) {
// first test only first byte // first test only first byte
if (buffer[offset + i] != bs[0]) continue loop; if (this.buffer[this.offset + i] != bs[0]) continue loop;
// then test all remaining bytes // then test all remaining bytes
for (int j = 1; j < bs.length; j++) { for (int j = 1; j < bs.length; j++) {
if (buffer[offset + i + j] != bs[j]) continue loop; if (this.buffer[this.offset + i + j] != bs[j]) continue loop;
} }
// found hit // found hit
return i; return i;
} }
@ -268,22 +277,22 @@ public final class ByteBuffer extends OutputStream {
} }
public int lastIndexOf(final byte b) { public int lastIndexOf(final byte b) {
for (int i = length - 1; i >= 0; i--) if (buffer[offset + i] == b) return i; for (int i = this.length - 1; i >= 0; i--) if (this.buffer[this.offset + i] == b) return i;
return -1; return -1;
} }
public boolean startsWith(final byte[] bs) { public boolean startsWith(final byte[] bs) {
return startsWith(bs, 0); return startsWith(bs, 0);
} }
public boolean startsWith(final byte[] bs, final int start) { public boolean startsWith(final byte[] bs, final int start) {
if (length - start < bs.length) return false; if (this.length - start < bs.length) return false;
for (int i = 0; i < bs.length; i++) { for (int i = 0; i < bs.length; i++) {
if (buffer[offset + i + start] != bs[i]) return false; if (this.buffer[this.offset + i + start] != bs[i]) return false;
} }
return true; return true;
} }
public byte[] getBytes() { public byte[] getBytes() {
return getBytes(0); return getBytes(0);
} }
@ -291,17 +300,17 @@ public final class ByteBuffer extends OutputStream {
public byte[] getBytes(final int start) { public byte[] getBytes(final int start) {
return getBytes(start, this.length); return getBytes(start, this.length);
} }
public byte[] getBytes(final int start, final int len) { public byte[] getBytes(final int start, final int len) {
// start is inclusive, end is exclusive // start is inclusive, end is exclusive
if (len > length) throw new IndexOutOfBoundsException("getBytes: len > length"); if (len > this.length) throw new IndexOutOfBoundsException("getBytes: len > length");
if (start > length) throw new IndexOutOfBoundsException("getBytes: start > length"); if (start > this.length) throw new IndexOutOfBoundsException("getBytes: start > length");
if ((start == 0) && (len == length) && (len == buffer.length)) return buffer; if ((start == 0) && (len == this.length) && (len == this.buffer.length)) return this.buffer;
final byte[] tmp = new byte[len]; final byte[] tmp = new byte[len];
System.arraycopy(buffer, offset + start, tmp, 0, len); System.arraycopy(this.buffer, this.offset + start, tmp, 0, len);
return tmp; return tmp;
} }
public ByteBuffer trim(final int start) { public ByteBuffer trim(final int start) {
this.offset += start; this.offset += start;
this.length -= start; this.length -= start;
@ -317,14 +326,14 @@ public final class ByteBuffer extends OutputStream {
public ByteBuffer trim() { public ByteBuffer trim() {
int l = 0; int l = 0;
while ((l < length) && (buffer[offset + l] <= 32)) { while ((l < this.length) && (this.buffer[this.offset + l] <= 32)) {
l++; l++;
} }
int r = length - 1; int r = this.length - 1;
while ((r > l) && (buffer[offset + r] <= 32)) r--; while ((r > l) && (this.buffer[this.offset + r] <= 32)) r--;
return trim(l, r - l + 1); return trim(l, r - l + 1);
} }
public int isUTF8char(final int start) { public int isUTF8char(final int start) {
// a sequence of bytes is a utf-8 character, if one of the following 4 conditions is true: // a sequence of bytes is a utf-8 character, if one of the following 4 conditions is true:
// - ASCII equivalence range; (first) byte begins with zero // - ASCII equivalence range; (first) byte begins with zero
@ -332,20 +341,20 @@ public final class ByteBuffer extends OutputStream {
// - first byte begins with 1110, the following two bytes begin with 10 // - first byte begins with 1110, the following two bytes begin with 10
// - First byte begins with 11110, the following three bytes begin with 10 // - First byte begins with 11110, the following three bytes begin with 10
// if an utf-8 sequence is detected, the length of the sequence is returned. -1 otherwise // if an utf-8 sequence is detected, the length of the sequence is returned. -1 otherwise
if ((start < length) && if ((start < this.length) &&
((buffer[offset + start] & 0x80) != 0)) return 1; ((this.buffer[this.offset + start] & 0x80) != 0)) return 1;
if ((start < length - 1) && if ((start < this.length - 1) &&
((buffer[offset + start ] & 0xE0) == 0xC0) && ((this.buffer[this.offset + start ] & 0xE0) == 0xC0) &&
((buffer[offset + start + 1] & 0xC0) == 0x80)) return 2; ((this.buffer[this.offset + start + 1] & 0xC0) == 0x80)) return 2;
if ((start < length - 2) && if ((start < this.length - 2) &&
((buffer[offset + start ] & 0xF0) == 0xE0) && ((this.buffer[this.offset + start ] & 0xF0) == 0xE0) &&
((buffer[offset + start + 1] & 0xC0) == 0x80) && ((this.buffer[this.offset + start + 1] & 0xC0) == 0x80) &&
((buffer[offset + start + 2] & 0xC0) == 0x80)) return 3; ((this.buffer[this.offset + start + 2] & 0xC0) == 0x80)) return 3;
if ((start < length - 3) && if ((start < this.length - 3) &&
((buffer[offset + start ] & 0xF8) == 0xF0) && ((this.buffer[this.offset + start ] & 0xF8) == 0xF0) &&
((buffer[offset + start + 1] & 0xC0) == 0x80) && ((this.buffer[this.offset + start + 1] & 0xC0) == 0x80) &&
((buffer[offset + start + 2] & 0xC0) == 0x80) && ((this.buffer[this.offset + start + 2] & 0xC0) == 0x80) &&
((buffer[offset + start + 3] & 0xC0) == 0x80)) return 4; ((this.buffer[this.offset + start + 3] & 0xC0) == 0x80)) return 4;
return -1; return -1;
} }
@ -353,52 +362,52 @@ public final class ByteBuffer extends OutputStream {
// returns true, if trim() would result in an empty serverByteBuffer // returns true, if trim() would result in an empty serverByteBuffer
if (includeNonLetterBytes) { if (includeNonLetterBytes) {
byte b; byte b;
for (int i = 0; i < length; i++) { for (int i = 0; i < this.length; i++) {
b = buffer[offset + i]; b = this.buffer[this.offset + i];
if (((b >= '0') && (b <= '9')) || ((b >= 'A') && (b <= 'Z')) || ((b >= 'a') && (b <= 'z'))) return false; if (((b >= '0') && (b <= '9')) || ((b >= 'A') && (b <= 'Z')) || ((b >= 'a') && (b <= 'z'))) return false;
} }
} else { } else {
for (int i = 0; i < length; i++) if (buffer[offset + i] > 32) return false; for (int i = 0; i < this.length; i++) if (this.buffer[this.offset + i] > 32) return false;
} }
return true; return true;
} }
public int whitespaceStart(final boolean includeNonLetterBytes) { public int whitespaceStart(final boolean includeNonLetterBytes) {
// returns number of whitespace bytes at the beginning of text // returns number of whitespace bytes at the beginning of text
if (includeNonLetterBytes) { if (includeNonLetterBytes) {
byte b; byte b;
for (int i = 0; i < length; i++) { for (int i = 0; i < this.length; i++) {
b = buffer[offset + i]; b = this.buffer[this.offset + i];
if (((b >= '0') && (b <= '9')) || ((b >= 'A') && (b <= 'Z')) || ((b >= 'a') && (b <= 'z'))) return i; if (((b >= '0') && (b <= '9')) || ((b >= 'A') && (b <= 'Z')) || ((b >= 'a') && (b <= 'z'))) return i;
} }
} else { } else {
for (int i = 0; i < length; i++) if (buffer[offset + i] > 32) return i; for (int i = 0; i < this.length; i++) if (this.buffer[this.offset + i] > 32) return i;
} }
return length; return this.length;
} }
public int whitespaceEnd(final boolean includeNonLetterBytes) { public int whitespaceEnd(final boolean includeNonLetterBytes) {
// returns position of whitespace at the end of text // returns position of whitespace at the end of text
if (includeNonLetterBytes) { if (includeNonLetterBytes) {
byte b; byte b;
for (int i = length - 1; i >= 0; i--) { for (int i = this.length - 1; i >= 0; i--) {
b = buffer[offset + i]; b = this.buffer[this.offset + i];
if (((b >= '0') && (b <= '9')) || ((b >= 'A') && (b <= 'Z')) || ((b >= 'a') && (b <= 'z'))) return i + 1; if (((b >= '0') && (b <= '9')) || ((b >= 'A') && (b <= 'Z')) || ((b >= 'a') && (b <= 'z'))) return i + 1;
} }
} else { } else {
for (int i = length - 1; i >= 0; i--) if (buffer[offset + i] > 32) return i + 1; for (int i = this.length - 1; i >= 0; i--) if (this.buffer[this.offset + i] > 32) return i + 1;
} }
return 0; return 0;
} }
@Override @Override
public String toString() { public String toString() {
return UTF8.String(buffer, offset, length); return UTF8.String(this.buffer, this.offset, this.length);
} }
public String toString(final int left, final int length) { public String toString(final int left, final int length) {
return UTF8.String(buffer, offset + left, length); return UTF8.String(this.buffer, this.offset + left, length);
} }
public StringBuilder toStringBuilder(final int left, final int length, final int sblength) { public StringBuilder toStringBuilder(final int left, final int length, final int sblength) {
@ -406,85 +415,85 @@ public final class ByteBuffer extends OutputStream {
final StringBuilder sb = new StringBuilder(sblength); final StringBuilder sb = new StringBuilder(sblength);
int i = 0; int i = 0;
sb.setLength(length); sb.setLength(length);
for (int j = left; j < left + length; j++) sb.setCharAt(i++, (char) buffer[j]); for (int j = left; j < left + length; j++) sb.setCharAt(i++, (char) this.buffer[j]);
return sb; return sb;
} }
public Properties propParser(final String charset) { public Properties propParser(final String charset) {
// extract a=b or a="b" - relations from the buffer // extract a=b or a="b" - relations from the buffer
int pos = offset; int pos = this.offset;
int start; int start;
String key; String key;
final Properties p = new Properties(); final Properties p = new Properties();
// eat up spaces at beginning // eat up spaces at beginning
while ((pos < length) && (buffer[pos] <= 32)) pos++; while ((pos < this.length) && (this.buffer[pos] <= 32)) pos++;
while (pos < length) { while (pos < this.length) {
// pos is at start of next key // pos is at start of next key
start = pos; start = pos;
while ((pos < length) && (buffer[pos] != equal)) pos++; while ((pos < this.length) && (this.buffer[pos] != equal)) pos++;
if (pos >= length) break; // this is the case if we found no equal if (pos >= this.length) break; // this is the case if we found no equal
try { try {
key = new String(buffer, start, pos - start, charset).trim().toLowerCase(); key = new String(this.buffer, start, pos - start, charset).trim().toLowerCase();
} catch (final UnsupportedEncodingException e1) { } catch (final UnsupportedEncodingException e1) {
key = UTF8.String(buffer, start, pos - start).trim().toLowerCase(); key = UTF8.String(this.buffer, start, pos - start).trim().toLowerCase();
} }
// we have a key // we have a key
pos++; pos++;
// find start of value // find start of value
while ((pos < length) && (buffer[pos] <= 32)) pos++; while ((pos < this.length) && (this.buffer[pos] <= 32)) pos++;
// doublequotes are obligatory. However, we want to be fuzzy if they // doublequotes are obligatory. However, we want to be fuzzy if they
// are ommittet // are ommittet
if (pos >= length) { if (pos >= this.length) {
// error case: input ended too early // error case: input ended too early
break; break;
} else if (buffer[pos] == doublequote) { } else if (this.buffer[pos] == doublequote) {
// search next doublequote // search next doublequote
pos++; pos++;
start = pos; start = pos;
while ((pos < length) && (buffer[pos] != doublequote)) pos++; while ((pos < this.length) && (this.buffer[pos] != doublequote)) pos++;
if (pos >= length) break; // this is the case if we found no parent doublequote if (pos >= this.length) break; // this is the case if we found no parent doublequote
try { try {
p.setProperty(key, new String(buffer, start, pos - start,charset).trim()); p.setProperty(key, new String(this.buffer, start, pos - start,charset).trim());
} catch (final UnsupportedEncodingException e) { } catch (final UnsupportedEncodingException e) {
p.setProperty(key, UTF8.String(buffer, start, pos - start).trim()); p.setProperty(key, UTF8.String(this.buffer, start, pos - start).trim());
} }
pos++; pos++;
} else if (buffer[pos] == singlequote) { } else if (this.buffer[pos] == singlequote) {
// search next singlequote // search next singlequote
pos++; pos++;
start = pos; start = pos;
while ((pos < length) && (buffer[pos] != singlequote)) pos++; while ((pos < this.length) && (this.buffer[pos] != singlequote)) pos++;
if (pos >= length) break; // this is the case if we found no parent singlequote if (pos >= this.length) break; // this is the case if we found no parent singlequote
try { try {
p.setProperty(key, new String(buffer, start, pos - start,charset).trim()); p.setProperty(key, new String(this.buffer, start, pos - start,charset).trim());
} catch (final UnsupportedEncodingException e) { } catch (final UnsupportedEncodingException e) {
p.setProperty(key, UTF8.String(buffer, start, pos - start).trim()); p.setProperty(key, UTF8.String(this.buffer, start, pos - start).trim());
} }
pos++; pos++;
} else { } else {
// search next whitespace // search next whitespace
start = pos; start = pos;
while ((pos < length) && (buffer[pos] > 32)) pos++; while ((pos < this.length) && (this.buffer[pos] > 32)) pos++;
try { try {
p.setProperty(key, new String(buffer, start, pos - start,charset).trim()); p.setProperty(key, new String(this.buffer, start, pos - start,charset).trim());
} catch (final UnsupportedEncodingException e) { } catch (final UnsupportedEncodingException e) {
p.setProperty(key, UTF8.String(buffer, start, pos - start).trim()); p.setProperty(key, UTF8.String(this.buffer, start, pos - start).trim());
} }
} }
// pos should point now to a whitespace: eat up spaces // pos should point now to a whitespace: eat up spaces
while ((pos < length) && (buffer[pos] <= 32)) pos++; while ((pos < this.length) && (this.buffer[pos] <= 32)) pos++;
// go on with next loop // go on with next loop
} }
return p; return p;
} }
public static boolean equals(final byte[] buffer, final byte[] pattern) { public static boolean equals(final byte[] buffer, final byte[] pattern) {
// compares two byte arrays: true, if pattern appears completely at offset position // compares two byte arrays: true, if pattern appears completely at offset position
if (buffer.length < pattern.length) return false; if (buffer.length < pattern.length) return false;
for (int i = 0; i < pattern.length; i++) if (buffer[i] != pattern[i]) return false; for (int i = 0; i < pattern.length; i++) if (buffer[i] != pattern[i]) return false;
return true; return true;
} }
public static boolean equals(final byte[] buffer, final int offset, final byte[] pattern) { public static boolean equals(final byte[] buffer, final int offset, final byte[] pattern) {
// compares two byte arrays: true, if pattern appears completely at offset position // compares two byte arrays: true, if pattern appears completely at offset position
if (buffer.length < offset + pattern.length) return false; if (buffer.length < offset + pattern.length) return false;
@ -495,34 +504,34 @@ public final class ByteBuffer extends OutputStream {
public void reset() { public void reset() {
this.length = 0; this.length = 0;
this.offset = 0; this.offset = 0;
} }
public void reset(final int newSize) { public void reset(final int newSize) {
this.resize(newSize); resize(newSize);
this.reset(); this.reset();
} }
public void resize(final int newSize) { public void resize(final int newSize) {
if(newSize < 0) throw new IllegalArgumentException("Illegal array size: " + newSize); if(newSize < 0) throw new IllegalArgumentException("Illegal array size: " + newSize);
final byte[] v = new byte[newSize]; final byte[] v = new byte[newSize];
System.arraycopy(this.buffer,0,v,0,newSize > this.buffer.length ? this.buffer.length : newSize); System.arraycopy(this.buffer,0,v,0,newSize > this.buffer.length ? this.buffer.length : newSize);
this.buffer = v; this.buffer = v;
} }
public void writeTo(final OutputStream dest) throws IOException { public void writeTo(final OutputStream dest) throws IOException {
dest.write(this.buffer, this.offset, this.length); dest.write(this.buffer, this.offset, this.length);
dest.flush(); dest.flush();
} }
public static boolean contains(Collection<byte[]> collection, byte[] key) { public static boolean contains(final Collection<byte[]> collection, final byte[] key) {
for (byte[] v: collection) { for (final byte[] v: collection) {
if (equals(v, key)) return true; if (equals(v, key)) return true;
} }
return false; return false;
} }
public static List<byte[]> split(byte[] b, byte s) { public static List<byte[]> split(final byte[] b, final byte s) {
ArrayList<byte[]> a = new ArrayList<byte[]>(); final ArrayList<byte[]> a = new ArrayList<byte[]>();
int c = 0; int c = 0;
loop: while (c < b.length) { loop: while (c < b.length) {
int i = c; int i = c;
@ -532,13 +541,13 @@ public final class ByteBuffer extends OutputStream {
} }
if (i >= b.length) { if (i >= b.length) {
// nothing found; this is the end of the search // nothing found; this is the end of the search
byte[] bb = new byte[b.length - c]; final byte[] bb = new byte[b.length - c];
System.arraycopy(b, c, bb, 0, bb.length); System.arraycopy(b, c, bb, 0, bb.length);
a.add(bb); a.add(bb);
break loop; break loop;
} else { } else {
// found a separator // found a separator
byte[] bb = new byte[i - c]; final byte[] bb = new byte[i - c];
System.arraycopy(b, c, bb, 0, bb.length); System.arraycopy(b, c, bb, 0, bb.length);
a.add(bb); a.add(bb);
c = i + 1; c = i + 1;
@ -546,5 +555,5 @@ public final class ByteBuffer extends OutputStream {
} }
return a; return a;
} }
} }
Loading…
Cancel
Save