code cleanup + BufInputStream.is()
authorNiki Roo <niki@nikiroo.be>
Mon, 29 Apr 2019 20:51:33 +0000 (22:51 +0200)
committerNiki Roo <niki@nikiroo.be>
Mon, 29 Apr 2019 20:51:33 +0000 (22:51 +0200)
src/be/nikiroo/utils/serial/CustomSerializer.java
src/be/nikiroo/utils/serial/Importer.java
src/be/nikiroo/utils/streams/BufferedInputStream.java
src/be/nikiroo/utils/test_code/BufferedInputStreamTest.java

index c5876ce401b622788f53ca073ced5e89d7ca56c2..fe63f71903619c6ae49f7fa8d765ddf0b2858f0a 100644 (file)
@@ -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;
-       }
 }
index 80a8684bd92f97433faf9d6ece457e0b41fa29be..5b75d313bc465918feb18786f90badb163ece6be 100644 (file)
@@ -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);
index da862e3f6802aa22f47586196a680d53d89d302f..336dba4000c54a26b25e33d0e84a2d8f46b2a01d 100644 (file)
@@ -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.
+        * <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.
index 3ba7be8c738d2308215f33b59dbeff02a69a4606..c715585f7da7afb93633c937672342f5b6ec3fc7 100644 (file)
@@ -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,