开发者

Creating a rolling window on a byte stream in java

I have a byte stream that comes into my app (its actually reading from a file, but this could change).

The data is of the form <tag><value>. Im looking for a specific tag, and wish to discard all others.

My thinking was to have a 'window' of length (taglength + valuelength), and push data into it, with old data dropping off the end. Then as soon as the first (taglength) bytes match the tag im interested in I can just get the whole window and process it.

I dont think there's anything in the SDK that matches my needs, but would love to be proven wrong. Any ideas?

EDIT: To clarify,the data isnt xml - ive just used angled brackets to s开发者_开发技巧how the delimitation. The data is actually a binary stream.


Using a byte stream to parse an XML file is calling for trouble.

I would strongly suggest to use a streaming XML parser (e.g. a SAXParser) that does not buffer the whole document in memory (or create a complete DOM representation). The SAX parser that is built into the JDK is quite efficient in terms of memory usage.


The closest thing in the JDK is to use ByteBuffer.compact() which allows you to shuffle along data in a buffer. One way to avoid too many calls to compact is to make the buffer fairly large and call it only when you are running out of space.

I would only use a ByteBuffer if your data format is simple, i.e. not full XML standard.


Use a ring buffer, i.e., a circular fixed-length buffer (array). You keep two pointers, one pointing to the start of the data (i.e., the oldest bytes in the ring), and another pointing to the end of the data (to the newest bytes).

You scan the ring from the start pointer to the end pointer. At certain times, such as when you've exhausted the contents of the ring, you read in fresh data into the ring, overwriting the old data, and then adjust the pointers appropriately. Since it's a ring, when you increment a pointer past the end of the array, you wrap around back to the beginning of the array.

UPDATE

An example of using a ring buffer in Java is at:
http://david.tribble.com/src/java/tribble/util/FifoQueue.java

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜