How should I handle searching through byte arrays in Java?
Preliminary: I am writting my own httpclient in Java. I am trying to parse out the contents of chunked encoding.
Here is my dilema: Since I am trying to parse out chunked http transfer encoding with a gzip payload there is a mix of ascii and binary. I can't just take the http resp content and convert it to a string and make use of StringUtils since the binary data can easily contain nil charact开发者_开发问答ers. So what I need to do is some basic things for parsing out each chunk and its chunk length (as per chunked transfer/HTTP/1.1 spec).
Are there any helpful ways of searching through byte arrays of binary/part ascii data for certain patterns (like a CR
LF
) (instead of just a single byte) ? Or must I write the for loops for this?
Thus, you basically need a ChunkedInputStream
. Google gives enough hints. Apache HttpCore's variant is pretty concise. I'd suggest you to just use it rather than reinventing your own client.
If you really, really want to implement your own HTTP client stack from the ground up, try a Google search for "+java +bytestring". One of the first hits that I got was this ByteString class which would seem to do provide the kind of functionality you are asking for.
But I think @BalusC's approach is better.
精彩评论