Not sure about WTF, but...



  • Just found this in an application I'm working on. The context is, it's a big servlet which is designed to spoof the responses of a production system (sending and receiving XML).

    ObjectOutputStream outStream = new ObjectOutputStream(response.getOutputStream());
    outStream.writeBytes(xmlresponse);
    outStream.flush();
    outStream.close();

    Why would you use an ObjectOutputStream to write an XML response? That's not it's purpose, as far as I can gather!

    What's wrong with my replacement:

    response.getWriter().write(xmlresponse);

    The whole reason I was looking at this in the first place was because reading the XML response was coming up with a strange sun.io.MalformedInputException.

    If anyone knows why the ObjectOutputStream was used for this purpose please let me know...
     



  • Possibly because that's what the writer knew, and they performed "Intelligent Design" by copy/pasting stuff from elsewhere. If your stuff is big on serialization, it's not an entirely unreasonable that he decided to just do this. Frankly, from just a quick glance at the docs, this looks like it'd work just fine, but upon reflection it probably also pops the length of the string in there.

    Honestly, this looks like an honest (albiet stupid) mistake, and you should just throw something soft at the party responsible (say, a Wee Ninja), and get on with your job.



  • In this case, they're likely the same. But you have to be careful with "writers" because they'll attempt to decode binary data, which breaks a lot of things. If it's just text, however, that's no problem, and probably even a good thing.

     



  • It could just be that the programmer knew of only one type of output stream that had a function to directly write a string, and knew nothing about writers in general.  Also, am I the only one who finds it moronic that writeBytes doesn't take a byte array, but write does?



  • @bstorer said:

    It could just be that the programmer knew of only one type of output stream that had a function to directly write a string, and knew nothing about writers in general.  Also, am I the only one who finds it moronic that writeBytes doesn't take a byte array, but write does?

    I've long since stopped questioning Java, as it is a futile and quite painful endeavor. 


Log in to reply