From: Niki Roo Date: Mon, 29 Apr 2019 20:51:33 +0000 (+0200) Subject: code cleanup + BufInputStream.is() X-Git-Url: http://git.nikiroo.be/?p=nikiroo-utils.git;a=commitdiff_plain;h=dce410fec00030c0affde7b19bcde6de847fae13 code cleanup + BufInputStream.is() --- diff --git a/src/be/nikiroo/utils/serial/CustomSerializer.java b/src/be/nikiroo/utils/serial/CustomSerializer.java index c5876ce..fe63f71 100644 --- a/src/be/nikiroo/utils/serial/CustomSerializer.java +++ b/src/be/nikiroo/utils/serial/CustomSerializer.java @@ -5,6 +5,7 @@ import java.io.InputStream; 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; @@ -160,6 +161,8 @@ public abstract class CustomSerializer { } } + /** Use {@link CustomSerializer#isCustom(BufferedInputStream)}. */ + @Deprecated public static boolean isCustom(String encodedValue) { int pos1 = encodedValue.indexOf('^'); int pos2 = encodedValue.indexOf('^', pos1 + 1); @@ -167,6 +170,10 @@ public abstract class CustomSerializer { 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); @@ -174,12 +181,4 @@ public abstract class CustomSerializer { 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; - } } diff --git a/src/be/nikiroo/utils/serial/Importer.java b/src/be/nikiroo/utils/serial/Importer.java index 80a8684..5b75d31 100644 --- a/src/be/nikiroo/utils/serial/Importer.java +++ b/src/be/nikiroo/utils/serial/Importer.java @@ -9,6 +9,7 @@ import java.util.zip.GZIPInputStream; 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; @@ -136,7 +137,7 @@ public class Importer { * @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 @@ -155,10 +156,6 @@ public class Importer { // 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); diff --git a/src/be/nikiroo/utils/streams/BufferedInputStream.java b/src/be/nikiroo/utils/streams/BufferedInputStream.java index da862e3..336dba4 100644 --- a/src/be/nikiroo/utils/streams/BufferedInputStream.java +++ b/src/be/nikiroo/utils/streams/BufferedInputStream.java @@ -105,6 +105,15 @@ public class BufferedInputStream extends InputStream { 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 @@ -127,6 +136,50 @@ public class BufferedInputStream extends InputStream { return this; } + /** + * Check if the current content (until eof) is equal to the given search + * term. + *

+ * Note: the search term size must 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. + *

+ * Note: the search term size must 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. diff --git a/src/be/nikiroo/utils/test_code/BufferedInputStreamTest.java b/src/be/nikiroo/utils/test_code/BufferedInputStreamTest.java index 3ba7be8..c715585 100644 --- a/src/be/nikiroo/utils/test_code/BufferedInputStreamTest.java +++ b/src/be/nikiroo/utils/test_code/BufferedInputStreamTest.java @@ -30,6 +30,75 @@ class BufferedInputStreamTest extends TestLauncher { 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,