• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2920
  • Last Modified:

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

0
phpamble
Asked:
phpamble
  • 4
  • 3
1 Solution
 
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
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.

 
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
 
phpambleAuthor Commented:
Thank You
0
 
CEHJCommented:
:-)
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now