Modified Read Procedure For An InputStream

I previously submitted a question that was excellently answered by CEHJ regarding reading bytes from an inputstream and writing them to a suitable buffer structure.

However I have one further modification I want to make namely the following:

The length of bytes I want to read from a file is unknown when I issue the request to read. The only known is that all the bytes must be read up to the start of of a sequence of 7 bytes.

Is it possible to modify your code above to do this. I have been looking at using the read method of an input stream that reads and returns a single byte at a time and then build the logic around iterations of this. Is there a smarter way to do this?
LVL 1
phpambleAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

phpambleAuthor Commented:
The code given by CEHJ and referred to above is as follows:

final int BUF_SIZE = 1 << 10 << 3;// 8 KiB buffer
int bytesRead = -1;
byte[] buffer= new byte[BUF_SIZE];
ByteArrayOutputStream out = new ByteArrayOutputStream();
while ((bytesRead = in.read(buffer)) > -1) { // read from the input stream
        out.write(buffer, 0, bytesRead);
}
in.close();
callPlayerMethod(out.toByteArray());
0
CEHJCommented:
What is that 'sequence of 7 bytes'?
0
phpambleAuthor Commented:
In hex values: 69 73 6F 6D 33 67 70
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

CEHJCommented:
The following will look for a binary sequence 'search' in a stream and collect input up to, but not including it:
        public static void main(String[] args) throws Exception  {
            byte[] search = { (byte)0x69, (byte)0x73, (byte)0x6F, (byte)0x6D, (byte)0x33, (byte)0x67, (byte)0x70 };             byte[] matcher = new byte[search.length];
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            PushbackInputStream in = new PushbackInputStream(new FileInputStream(args[0]), search.length);             int c = -1;
            while ((c = in.read()) > -1) {
                if (c == search[0]) {
                    in.unread(c);
                    in.read(matcher);
                    System.out.println(new String(matcher));
                    if (Arrays.equals(search, matcher)) {
                        // Found pattern
                        in.unread(matcher);
                        break;
                    }
                    else {
                        out.write(matcher);
                    }
                }
                out.write(c);
            }
            in.close();
            System.out.printf("'%s'", new String(out.toByteArray()));
        }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
phpambleAuthor Commented:
Stunning, Thanks, I am also posting another question for how this can be done in J2ME if this can be done?
0
phpambleAuthor Commented:
0
CEHJCommented:
:-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.