import java.io.OutputStream;
import be.nikiroo.utils.IOUtils;
+import be.nikiroo.utils.streams.BufferedInputStream;
import be.nikiroo.utils.streams.NextableInputStream;
import be.nikiroo.utils.streams.NextableInputStreamStep;
import be.nikiroo.utils.streams.ReplaceInputStream;
}
}
+ /** Use {@link CustomSerializer#isCustom(BufferedInputStream)}. */
+ @Deprecated
public static boolean isCustom(String encodedValue) {
int pos1 = encodedValue.indexOf('^');
int pos2 = encodedValue.indexOf('^', pos1 + 1);
return pos1 >= 0 && pos2 >= 0 && encodedValue.startsWith("custom^");
}
+ public static boolean isCustom(BufferedInputStream in) throws IOException {
+ return in.startsWiths("custom^");
+ }
+
public static String typeOf(String encodedValue) {
int pos1 = encodedValue.indexOf('^');
int pos2 = encodedValue.indexOf('^', pos1 + 1);
return type;
}
-
- public static String contentOf(String encodedValue) {
- int pos1 = encodedValue.indexOf('^');
- int pos2 = encodedValue.indexOf('^', pos1 + 1);
- String encodedContent = encodedValue.substring(pos2 + 1);
-
- return encodedContent;
- }
}
import be.nikiroo.utils.IOUtils;
import be.nikiroo.utils.streams.Base64InputStream;
+import be.nikiroo.utils.streams.BufferedInputStream;
import be.nikiroo.utils.streams.NextableInputStream;
import be.nikiroo.utils.streams.NextableInputStreamStep;
* @throws IOException
* if the content cannot be read (for instance, corrupt data)
*/
- private boolean processLine(InputStream in) throws NoSuchFieldException,
+ private boolean processLine(BufferedInputStream in) throws NoSuchFieldException,
NoSuchMethodException, ClassNotFoundException, IOException {
// Defer to latest child if any
// TODO use the stream, Luke
String line = IOUtils.readSmallStream(in);
- if (line.isEmpty()) {
- return false;
- }
-
if (line.equals("{")) { // START: new child if needed
if (link != null) {
child = new Importer(map);
this.stop = length;
}
+ /**
+ * The internal buffer size (can be useful to know for search methods).
+ *
+ * @return the size of the internal buffer, in bytes.
+ */
+ public int getInternalBufferSize() {
+ return originalBuffer.length;
+ }
+
/**
* Return this very same {@link BufferedInputStream}, but keep a counter of
* how many streams were open this way. When calling
return this;
}
+ /**
+ * Check if the current content (until eof) is equal to the given search
+ * term.
+ * <p>
+ * Note: the search term size <b>must</b> be smaller or equal the internal
+ * buffer size.
+ *
+ * @param search
+ * the term to search for
+ *
+ * @return TRUE if the content that will be read starts with it
+ *
+ * @throws IOException
+ * in case of I/O error or if the size of the search term is
+ * greater than the internal buffer
+ */
+ public boolean is(String search) throws IOException {
+ return is(StringUtils.getBytes(search));
+ }
+
+ /**
+ * Check if the current content (until eof) is equal to the given search
+ * term.
+ * <p>
+ * Note: the search term size <b>must</b> be smaller or equal the internal
+ * buffer size.
+ *
+ * @param search
+ * the term to search for
+ *
+ * @return TRUE if the content that will be read starts with it
+ *
+ * @throws IOException
+ * in case of I/O error or if the size of the search term is
+ * greater than the internal buffer
+ */
+ public boolean is(byte[] search) throws IOException {
+ if (startsWith(search)) {
+ return stop == search.length;
+ }
+
+ return false;
+ }
+
/**
* Check if the current content (what will be read next) starts with the
* given search term.
checkArrays(this, "FIRST", in, expected);
}
});
+
+ addTest(new TestCase("Byte array is(byte[])") {
+ @Override
+ public void test() throws Exception {
+ byte[] expected = new byte[] { 42, 12, 0, 127 };
+ BufferedInputStream in = new BufferedInputStream(expected);
+ assertEquals(
+ "The array should be considered identical to its source",
+ true, in.is(expected));
+ assertEquals(
+ "The array should be considered different to that one",
+ false, in.is(new byte[] { 42, 12, 0, 121 }));
+ in.close();
+ }
+ });
+
+ addTest(new TestCase("InputStream is(byte[])") {
+ @Override
+ public void test() throws Exception {
+ byte[] expected = new byte[] { 42, 12, 0, 127 };
+ BufferedInputStream in = new BufferedInputStream(
+ new ByteArrayInputStream(expected));
+ assertEquals(
+ "The array should be considered identical to its source",
+ true, in.is(expected));
+ assertEquals(
+ "The array should be considered different to that one",
+ false, in.is(new byte[] { 42, 12, 0, 121 }));
+ in.close();
+ }
+ });
+
+ addTest(new TestCase("Byte array is(String)") {
+ @Override
+ public void test() throws Exception {
+ String expected = "Testy";
+ BufferedInputStream in = new BufferedInputStream(
+ expected.getBytes("UTF-8"));
+ assertEquals(
+ "The array should be considered identical to its source",
+ true, in.is(expected));
+ assertEquals(
+ "The array should be considered different to that one",
+ false, in.is("Autre"));
+ assertEquals(
+ "The array should be considered different to that one",
+ false, in.is("Test"));
+ in.close();
+ }
+ });
+
+ addTest(new TestCase("InputStream is(String)") {
+ @Override
+ public void test() throws Exception {
+ String expected = "Testy";
+ BufferedInputStream in = new BufferedInputStream(
+ new ByteArrayInputStream(expected.getBytes("UTF-8")));
+ assertEquals(
+ "The array should be considered identical to its source",
+ true, in.is(expected));
+ assertEquals(
+ "The array should be considered different to that one",
+ false, in.is("Autre"));
+ assertEquals(
+ "The array should be considered different to that one",
+ false, in.is("Testy."));
+ in.close();
+ }
+ });
}
static void checkArrays(TestCase test, String prefix, InputStream in,