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

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:
I see there is no implementation for PushbackInputStream in the CLDC configuration.
0
CEHJCommented:
Ah. In that case, you could try it using mark and reset on BufferedInputStream. Does it have that class?
0
phpambleAuthor Commented:
Unfortunately no BufferedInputStream in CLDC.
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

phpambleAuthor Commented:
But it does have DataInputStream that supports mark() and reset().
0
CEHJCommented:
Try something like the following. Bear in mind that this is only tested briefly and could easily throw an exception due to attempting to read beyond the end of the stream:
	private static byte[] readUpTo(InputStream in) throws IOException  {
		if (!in.markSupported()) {
			throw new RuntimeException(
				"readUpTo won't work - mark not supported on input stream");
		}
 
		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();
		int c = -1;
 
		while ((c = in.read()) > -1) {
			if (c == search[0]) {
				in.mark(search.length);
				in.read(matcher, 1, matcher.length - 1);
				System.out.println(new String(matcher));
				matcher[0] = (byte)c;
 
				if (Arrays.equals(search, matcher)) {
					// Found pattern
					break;
				} else {
					in.reset();
					out.write(c);
				}
			} else {
				out.write(c);
			}
		}
 
		in.close();
 
		return 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:
Thank You
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.