Solved

Searching for strings in a byte array

Posted on 2006-06-09
6
224 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
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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
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 125 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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSum5 challenge 5 84
servlet doXXX methods 3 49
pagenation logic how it is working in my code 1 29
CSV file parsing thru Java 13 28
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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 third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

770 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