Link to home
Start Free TrialLog in
Avatar of phpamble
phpamble

asked on

Modified Read Procedure For An InputStream In J2ME

I had a question answered with some brilliant code from CEHJ regarding how bytes can be read (in Java) from an input stream up to a certain sequence of bytes. I am also needing to do this in J2ME.

So this question is really for CEHJ is there a similar way to do this in J2ME?
 
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

Avatar of phpamble
phpamble

ASKER

I see there is no implementation for PushbackInputStream in the CLDC configuration.
Avatar of CEHJ
Ah. In that case, you could try it using mark and reset on BufferedInputStream. Does it have that class?
Unfortunately no BufferedInputStream in CLDC.
But it does have DataInputStream that supports mark() and reset().
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank You
:-)