Reputation: 152
On the Client Side I'm trying to read until a custom termination bytes appear . thaw is (/ffff)
now readline() reads until buffer received /r , /n or both ,
but what if i want a custom termination bytes ?
Upvotes: 1
Views: 271
Reputation: 1602
There is no standard way to redefine line terminator.
As you can see in the source code of BufferedReader, '\r' '\n' line terminator is hardcoded.
You can take BufferedReader source, make your own class and replace line terminators in it.
Another solution is to create helper class, as i've made in one of my projects:
public class StreamSplitter {
private InputStream st;
private Charset charset;
/**
* Construct new stream splitter.
* @param st input stream
* @param charset input stream charset
*/
public StreamSplitter(InputStream st, Charset charset) {
this.st = st;
this.charset = charset;
}
// ... skip
/**
* Read stream until the specified marker is found.
* @param marker marker
* @return
* @throws IOException
*/
public String readUntil(String marker) throws IOException {
StringBuilder res = new StringBuilder();
if (!readUntil(marker, res)) {
return null;
}
return res.toString();
}
/**
* Read stream until the specified marker is found.
* @param marker marker
* @param res output
* @return <code>true</code> if marker is found, <code>false</code> if end of stream is occurred
* @throws IOException
*/
public boolean readUntil(String marker, StringBuilder res) throws IOException {
byte[] markerBytes = marker.getBytes(charset);
int b;
int n = 0;
while ((b = st.read()) != -1) {
byte bb = (byte)b;
if (markerBytes[n] == bb) {
n++;
if (n == markerBytes.length) {
return true;
}
}
else {
if (n != 0 && res != null) {
for (int nn = 0; nn < n; nn++)
res.append((char)markerBytes[nn]);
}
if (res != null)
res.append((char)bb);
n = 0;
}
}
return false;
}
// ... skip
}
Upvotes: 1