[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Modified Read Procedure For An InputStream

Posted on 2008-02-05
7
Medium Priority
?
283 Views
Last Modified: 2013-11-24
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?
0
Comment
Question by:phpamble
  • 4
  • 3
7 Comments
 
LVL 1

Author Comment

by:phpamble
ID: 20821765
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 20821786
What is that 'sequence of 7 bytes'?
0
 
LVL 1

Author Comment

by:phpamble
ID: 20821818
In hex values: 69 73 6F 6D 33 67 70
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 20822042
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
 
LVL 1

Author Comment

by:phpamble
ID: 20822183
Stunning, Thanks, I am also posting another question for how this can be done in J2ME if this can be done?
0
 
LVL 1

Author Comment

by:phpamble
ID: 20822233
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20822345
:-)
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month9 days, 15 hours left to enroll

591 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question