Read and write user-defined tags.

This commit is contained in:
akwizgran
2011-07-18 16:46:03 +01:00
parent 8fc97157d3
commit 427142ae51
7 changed files with 83 additions and 21 deletions

View File

@@ -459,4 +459,23 @@ class ReaderImpl implements Reader {
if(!hasNull()) throw new FormatException();
readNext(true);
}
public boolean hasUserDefinedTag() throws IOException {
if(!started) readNext(true);
if(eof) return false;
return next == Tag.USER ||
(next & Tag.SHORT_USER_MASK) == Tag.SHORT_USER;
}
public int readUserDefinedTag() throws IOException {
if(!hasUserDefinedTag()) throw new FormatException();
if(next == Tag.USER) {
readNext(false);
return readLength();
} else {
int tag = 0xFF & next ^ Tag.SHORT_USER;
readNext(true);
return tag;
}
}
}

View File

@@ -211,4 +211,14 @@ class WriterImpl implements Writer {
out.write(Tag.NULL);
bytesWritten++;
}
public void writeUserDefinedTag(int tag) throws IOException {
if(tag < 0) throw new IllegalArgumentException();
if(tag < 32) out.write((byte) (Tag.SHORT_USER | tag));
else {
out.write(Tag.USER);
writeLength(tag);
}
bytesWritten++;
}
}