Java: la forma más eficiente de leer desde inputStream y escribir en un outputStream (más algunas modificaciones)

I am reading from an InputStream. and writing what I read into an outputStream.

I also check a few things. Like if I read an

& (ampersand)

Necesito escribir

"& amp;"

My code works. But now I wonder if I have written the most efficient way (which I doubt).

I read byte by byte. (but this is because I need to do odd modifications)

Can somebody who's done this suggest the fastest way ?

Muchas Gracias

preguntado el 02 de febrero de 12 a las 10:02

You should concentrate on correctness rather than fractions of microseconds you could maybe save in such a dull program. -

@Ingo just because I ask a question about speed it does not necessarily mean that I don't care about correctness. -

3 Respuestas

Si está utilizando BufferedInputStream y BufferedOutputStream then it is hard to make it faster.

BTW if you are processing the input as characters as opposed to bytes, you should use readers/writers with BufferedReader y BufferedWriter.

Respondido 02 Feb 12, 14:02

can you elaborate a bit more ? - Zo72

@Xnzo72: about buffering or readers? I think buffering is quite obvious. When it comes to readers vs. input streams - try your program with UTF-16/32 encoded input or try to produce such. Currently it will only work with ASCII. - Tomasz Nurkiewicz

@TomaszNurkiewicz Your answer is better because accepted answer link has been expired. - Nikhil Agrawal

The code should be reading/writing characters with Readers and Writers. For example, if its in the middle of a UTF-8 sequence, or it gets the second half of a UCS-2 character and it happens to read the equivalent byte value of an ampersand, then its going to damage the data that its attempting to copy. Code usually lives longer than you would expect it to, and somebody might try to pick it up later and use it in a situation where this could really matter.

As far as being faster or slower, using a BufferedReader will probably help the most. If you're writing to the file system, a BufferedWriter won't make much of a difference, because the operating system will buffer writes for you and it does a good job. If you're writing to a StringWriter, then buffering will make no difference (may even make it slower), but otherwise buffering your writes ought to help.

You could rewrite it to process arrays; and that might make it faster. You can still do that with arrays. You will have to write more complicated code to handle boundary conditions. That also needs to be a factor in the decision.

Measure, don't guess, and be wary of opinions from people who aren't informed of all the details. Ultimately, its up to you ot figure out if its fast enough for this situation. There is no single answer, because all situations are different.

Respondido 02 Feb 12, 16:02

Preferiría usar BufferedReader for reading input and BufferedWriter for output. Using Regular Expressions for matching your input can make your code short and also improve your time complexity.

Respondido el 23 de Septiembre de 15 a las 13:09

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.