0

We need to decode long streams of data flowing.

Each piece of data is a long string comprising encoded sequences,separated by delimiters. Each sequence is a made up of encoded keys.

The mapping between the encoded 'keys' and their character equivalents is maintained separately. The idea is look up each key in the map;and replace with the corresponding alphabet.

What is the most efficient way of achieving this.

Is a substring and replace the right way to approach handling streams as this? Or do we have a higher-performing alternative?

1
  • Since every time you modify a string you make a new string, if you're going to do a LOT of tiny string operations use a StringBuilder or a char[] for performance. The map should be fine, maps are really high performance for get. Commented Apr 13, 2013 at 3:52

2 Answers 2

1

Since this looks like a homework assignment, I'll give you a general idea and let you work out the implementation yourself.

The most reasonable approach would be to have a StringBuilder in which you will construct your output. Parse your input token by token. For each token, look up it's key in your map and append the value to your StringBuilder. When you are done, output the string builder's contents.

Note: If your strings are VERY long (many MB or GB), just write directly to an output stream.

Sign up to request clarification or add additional context in comments.

1 Comment

What would writing directly to an output stream mean?How can a stringbuilder output be written so?
0

Many repeated replacements in a long string would be very inefficient. And as you say the input is "flowing", so instead of reading all the input ad processing at once, you should read and process by smaller chunks, I suppose buffer by buffer.

Process the incoming data using a StringTokenizer to split by delimiters.

For each incoming token, perform the replacement, and if possible write it to the output stream directly, or else append to a StringBuilder.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.