Solved

Searching for strings in a byte array

Posted on 2006-06-09
6
222 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
countHi2 challenge 7 44
topping3 challenge 14 49
java set up 1 46
java 8 lambda expresssions exception handling 3 35
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

707 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now