?
Solved

Searching for strings in a byte array

Posted on 2006-06-09
6
Medium Priority
?
228 Views
Last Modified: 2010-05-18
I want to do some fairly crude processing on data read by an InputStreamReader. I have performance issues with character processing and the text is ASCII, so I am working with bytes.

In this quick'n'dirty processing, I'd like to be able to search for strings in a character array read by the InputStreamReader. Google enticed me with http://www.goodserver.com/imap4-ssdk/apidocs/com/goodserver/util/ByteArrayHelper.html#indexOf%28byte%5b%5c,%20byte%5b%5c%29 but that would require buying into a library with a load of whistles and bells that aren't needed.

Can anyone suggest a  more suitable jar I could use for searching for strings in a byte array?
0
Comment
Question by:rstaveley
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 14

Expert Comment

by:hoomanv
ID: 16873562
you can write it yourself, by learning how it is implemented in String class using char, you can change it to use byte
java.lang.String source code: http://www.docjar.com/html/api/java/lang/String.java.html
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 16873607
> I'd like to be able to search for strings in a character array
does it really decreases performance if u use    new String(charArray).indexOf()   ?
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 16873874
My suggestion would be to use the BufferedReader to increase stream performance. Secondly, as far as the algorithm goes, my recommendation depends on the type search you're doing. Which strings are you searching for? Whole words? Partial words? Across line endings or not?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Author Comment

by:rstaveley
ID: 16874093
> BufferedReader

This code needs to process large text files quickly. I've taken note of the comparisons at: http://www.javaworld.com/jw-11-2000/jw-1117-performance.html and because I don't need characters so I'd rather work with bytes and buffer locally in a byte array read into by InputStreamReader.

Here's the sum total of the processing I need to do:

(1) Junk any UUEncoded blocks
   (a) Look for \nbegin
   (b) Ignore characters until \nend
(2) Junk any lines which are longer than (say) 512 bytes
   (a) Look for \n
   (b) If it comes after > 512 bytes
(3) Copy all unjunked text into a temporary file.

The processing in (2) is very simple and I guess I could look for individual bytes for "begin" and "end" to handle (1), but I'd prefer not to write the code, if I can avoid it :-)
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 16874136
> I want to do some fairly crude processing on data read by an InputStreamReader. I

if you are only interested in the bytes then don't use InputStreamReader and instead read from the InputStream directly.

You search would basically loop thru the bytes looking for the first byte in the string(s) to match.
When you find it check next matches 2ns char etc.
When you find a mismatch go back to looking for 1st char.
0
 
LVL 17

Author Comment

by:rstaveley
ID: 16875684
Thanks, objects. I suspected as much though I had a mental block about using the reader.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month10 days, 6 hours left to enroll

762 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