Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>I'm about to create a class that will take my InputStream, read through it until I find the junk, break, then take what I just wrote to, convert it back into an InputStream and pass it along like nothing happened. But I'm worried that it'll be grossly inefficient, have bugs I shouldn't have to deal with (e.g. breaking on binary values such as embedded images) and hopefully unnecessary.</p> </blockquote> <p>you could use a <a href="http://download.oracle.com/javase/6/docs/api/java/io/FilterInputStream.html" rel="nofollow">FilterStream</a> for that no need for a buffer</p> <p>best thing to do is add a delimiter to the end of the XML like <code>--theXML ends HERE --</code> or a char not found in XML like a group of 16 <a href="http://en.wikipedia.org/wiki/End-of-transmission_character" rel="nofollow"><code>\u04</code> chars</a> (you then only need to check every 16th byte) to the end of the XML and read until you find it </p> <p>implementation assuming <code>\u04</code> delim</p> <pre><code>class WebStream extends FilterInputStream { byte[] buff = new byte[1024]; int offset = 0, length = 0; public WebStream(InputStream i) { super(i); } @Override public boolean markSupported() { return false; } @Override public int read() throws IOException { if (offset == length) readNextChunk(); if (length == -1) return -1;// eof return buff[offset++]; } @Override public int read(byte[] b, int off, int len) throws IOException { if (offset == length) readNextChunk(); if (length == -1) return -1;// eof int cop = length - offset; if (len &lt; cop) cop = len; System.arraycopy(buff, offset, b, off, cop); offset += cop; return cop; } private void readNextChunk() throws IOException { if (offset &lt;= length) { System.arraycopy(buff, offset, buff, 0, length - offset); length -= offset; offset = 0; } int read = in.read(buff, length, buff.length - length); if (read &lt; 0 &amp;&amp; length &lt;= 0) { length = -1; offset = 0; return; } // note that this is assuming ascii compatible // anything like utf16 or utf32 will break here for (int i = length; i &lt; read + length; i += 16) { if (buff[i] == 0x04) { while (buff[--i] == 0x04) ;// find beginning of delim block length = i; read = 0; } } } } </code></pre> <p>note this misses throws, some error checking and needs proper debugging</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload