}
first = false;
- boolean zip = stream.startsWiths("ZIP:");
- boolean b64 = stream.startsWiths("B64:");
+ boolean zip = stream.startsWith("ZIP:");
+ boolean b64 = stream.startsWith("B64:");
if (zip || b64) {
stream.skip("XXX:".length());
* @throws IOException
* if the content cannot be read (for instance, corrupt data)
*/
- private boolean processLine(BufferedInputStream in) throws NoSuchFieldException,
- NoSuchMethodException, ClassNotFoundException, IOException {
+ private boolean processLine(BufferedInputStream in)
+ throws NoSuchFieldException, NoSuchMethodException,
+ ClassNotFoundException, IOException {
// Defer to latest child if any
if (child != null) {
return false;
}
- // TODO use the stream, Luke
- String line = IOUtils.readSmallStream(in);
-
- if (line.equals("{")) { // START: new child if needed
+ // Start/Stop object
+ if (in.is("{")) { // START: new child if needed
if (link != null) {
child = new Importer(map);
}
- } else if (line.equals("}")) { // STOP: report self to parent
+ in.end();
+ return false;
+ } else if (in.is("}")) { // STOP: report self to parent
+ in.end();
return true;
- } else if (line.startsWith("REF ")) { // REF: create/link self
+ }
+
+ // Custom objects
+ if (CustomSerializer.isCustom(in)) {
+ // not a field value but a direct value
+ String line = IOUtils.readSmallStream(in);
+ me = SerialUtils.decode(line);
+ return false;
+ }
+
+ // TODO use the stream, Luke
+ // .. at least for REF
+ String line = IOUtils.readSmallStream(in);
+
+ if (line.startsWith("REF ")) { // REF: create/link self
+ // TODO: here, line is REF type@999:xxx
+ // xxx is optional
+ // note: use .end() when containsKey(ref)
String[] tab = line.substring("REF ".length()).split("@");
String type = tab[0];
tab = tab[1].split(":");
// field value is compound
currentFieldName = line.substring(0, line.length() - 1);
} else if (line.startsWith(":") || !line.contains(":")
- || line.startsWith("\"") || CustomSerializer.isCustom(line)) {
+ || line.startsWith("\"")) {
// not a field value but a direct value
me = SerialUtils.decode(line);
} else {
*/
public boolean is(byte[] search) throws IOException {
if (startsWith(search)) {
- return stop == search.length;
+ return (stop - start) == search.length;
}
return false;
* in case of I/O error or if the size of the search term is
* greater than the internal buffer
*/
- public boolean startsWiths(String search) throws IOException {
+ public boolean startsWith(String search) throws IOException {
return startsWith(StringUtils.getBytes(search));
}
* Check if the current content (what will be read next) starts with the
* given search term.
* <p>
+ * An empty string will always return true (unless the stream is closed,
+ * which would throw an {@link IOException}).
+ * <p>
* Note: the search term size <b>must</b> be smaller or equal the internal
* buffer size.
*
if (available() >= search.length) {
// Easy path
return StreamUtils.startsWith(search, buffer, start, stop);
- } else if (!eof) {
+ } else if (in != null && !eof) {
// Harder path
if (buffer2 == null && buffer.length == originalBuffer.length) {
buffer2 = Arrays.copyOf(buffer, buffer.length * 2);
return !hasMoreData();
}
+ /**
+ * Read the whole {@link InputStream} until the end and return the number of
+ * bytes read.
+ *
+ * @return the number of bytes read
+ *
+ * @throws IOException
+ * in case of I/O error
+ */
+ public long end() throws IOException {
+ long skipped = 0;
+ while (hasMoreData()) {
+ skipped += skip(buffer.length);
+ }
+
+ return skipped;
+ }
+
@Override
public int read() throws IOException {
checkClose();
new NextableInputStreamStep(12));
checkNext(this, "FIRST", in, new byte[] { 42 });
- checkNextAll(this, "REST", in,
- new byte[] { 0, 127, 12, 51, 11, 12 });
- assertEquals("The stream still has some data", false,
- in.next());
+ checkNextAll(this, "REST", in, new byte[] { 0, 127, 12, 51, 11,
+ 12 });
+ assertEquals("The stream still has some data", false, in.next());
}
});
NextableInputStream in = new NextableInputStream(data, null);
in.next();
+ byte[] rest = new byte[] { 12, 51, 11, 12 };
+
in.skip(4);
- checkArrays(this, "ONLY", in, new byte[] { 12, 51, 11, 12 });
+ assertEquals("STARTS_WITH OK_1", true, in.startsWith(rest));
+ assertEquals("STARTS_WITH KO_1", false,
+ in.startsWith(new byte[] { 0 }));
+ assertEquals("STARTS_WITH KO_2", false, in.startsWith(data));
+ assertEquals("STARTS_WITH KO_3", false,
+ in.startsWith(new byte[] { 1, 2, 3 }));
+ assertEquals("STARTS_WITH OK_2", true, in.startsWith(rest));
+ assertEquals("READ REST", IOUtils.readSmallStream(in),
+ new String(rest));
+ in.close();
}
});
// no
assertEquals("It actually does not start with that", false,
in.startsWith(new byte[] { 12 }));
- assertEquals("It actually does not start with that", false,
- in.startsWith(
- new byte[] { 42, 12, 0, 127, 12, 51, 11, 11 }));
+ assertEquals(
+ "It actually does not start with that",
+ false,
+ in.startsWith(new byte[] { 42, 12, 0, 127, 12, 51, 11,
+ 11 }));
// too big
try {
- in.startsWith(
- new byte[] { 42, 12, 0, 127, 12, 51, 11, 12, 0 });
+ in.startsWith(new byte[] { 42, 12, 0, 127, 12, 51, 11, 12,
+ 0 });
fail("Searching a prefix bigger than the array should throw an IOException");
} catch (IOException e) {
}
+
+ in.close();
}
});
// yes
assertEquals("It actually starts with that", true,
- in.startsWiths("F"));
+ in.startsWith("F"));
assertEquals("It actually starts with that", true,
- in.startsWiths("Fanfan et"));
+ in.startsWith("Fanfan et"));
assertEquals("It actually is the same text", true,
- in.startsWiths(text));
+ in.startsWith(text));
// no
assertEquals("It actually does not start with that", false,
- in.startsWiths("Toto"));
+ in.startsWith("Toto"));
assertEquals("It actually does not start with that", false,
- in.startsWiths("Fanfan et Toto vont à la mee"));
+ in.startsWith("Fanfan et Toto vont à la mee"));
// too big
try {
- in.startsWiths("Fanfan et Toto vont à la mer.");
+ in.startsWith("Fanfan et Toto vont à la mer.");
fail("Searching a prefix bigger than the array should throw an IOException");
} catch (IOException e) {
}
+
+ in.close();
+ }
+ });
+
+ addTest(new TestCase("Starts With strings + steps") {
+ @Override
+ public void test() throws Exception {
+ String data = "{\nREF: fanfan\n}";
+ NextableInputStream in = new NextableInputStream(
+ data.getBytes("UTF-8"), new NextableInputStreamStep(
+ '\n'));
+ in.next();
+
+ assertEquals("STARTS_WITH OK", true, in.startsWith("{"));
+ in.skip(1);
+ assertEquals("STARTS_WITH WHEN SPENT", false,
+ in.startsWith("{"));
+
+ checkNext(this, "PARTIAL CONTENT", in,
+ "REF: fanfan".getBytes("UTF-8"));
+ }
+ });
+
+ addTest(new TestCase("InputStream is(String)") {
+ @Override
+ public void test() throws Exception {
+ String data = "{\nREF: fanfan\n}";
+ NextableInputStream in = new NextableInputStream(
+ new ByteArrayInputStream(data.getBytes("UTF-8")),
+ new NextableInputStreamStep('\n'));
+
+ in.next();
+ assertEquals("Item 1 OK", true, in.is("{"));
+ assertEquals("Item 1 KO_1", false, in.is("|"));
+ assertEquals("Item 1 KO_2", false, in.is("{}"));
+ in.skip(1);
+ in.next();
+ assertEquals("Item 2 OK", true, in.is("REF: fanfan"));
+ assertEquals("Item 2 KO", false, in.is("REF: fanfan."));
+ IOUtils.readSmallStream(in);
+ in.next();
+ assertEquals("Item 3 OK", true, in.is("}"));
+
+ in.close();
}
});
}