1

I am defining a buffered writer in a class I am developing, but having problems with it.

In the class constructor I am defining:

public class RestHandler  {
public static BufferedWriter rest_logger;

public RestHandler(parsedXMLConfigData _config, BufferedWriter writer) {
    rest_logger = writer;
    try {
        rest_logger.write("RestHandler instance finished init and ready to receive calls!" + "\n");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

This works and prints the text to my file. But when I try to use the same rest_logger in another one of my class methods:

    @POST
@Path("{subResources: [a-zA-Z0-9_/]+}")
public void postHandler
(
        @Context final UriInfo uriInfo,
        @PathParam("subResources") String subResources) {

    try {
        rest_logger.write("TEXT...");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }

It gives me a stream closed exception! I should mention that I use this to close the stream:

    protected void finalize() throws Throwable {
    rest_logger.close();
}
Perception
  • 79,279
  • 19
  • 185
  • 195
Michael A
  • 5,770
  • 16
  • 75
  • 127

3 Answers3

4

There are several problems with your code:

  • the rest_logger variable should not be static
  • you shouldn't initialize it to a new BufferedWriter just to discard it afterwards and reinitialize it with the writer argument (that you have no control on)
  • you shouldn't ignore exceptions. If you don't know what to do with them, make your methods throw IOException and let the caller decide what to do
  • you should not use finalizers
  • you should not close a writer that you have not created. Let the opener of the writer close it.

Other than that, and since your code doesn't make much sense, it's hard to understand what the code is supposed to do.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • 2
    +1. @MichaelA - apart from all the things JB Nizet mentioned, the one singular cause of your issue is making your BufferedWriter static and then closing it in the `finalize()` method. Keep in mind that finalize is an ***instance*** level method, in which you are manipulating a ***class*** level variable. – Perception Feb 18 '12 at 10:43
1

Removing the exception handlers for clarity, your code does:

rest_logger = new BufferedWriter(new FileWriter("rest_logger.txt"));
rest_logger = writer;

You're throwing away that new BufferedWriter immediately there. It does not make much sense. rest_logger will be set to whatever was handed over to you in that constructor call. When that gets closed, rest_logger will be closed too.

Mat
  • 202,337
  • 40
  • 393
  • 406
  • You didn't "fix" anything, you changed it. There is no way for us to tell what your problem is, we don't know what `writer` is, where it comes from, or when it will be closed. – Mat Feb 18 '12 at 10:35
1

I'm not quite sure I understood your question, BUT:

why exactly are you overwriting your newly created BufferedWriter?

rest_logger = writer;

maybe you should look into that...

  • Well, it's impossible to tell where your problem is without any context. as far as I can tell, now you are not instantiating a `BufferedWriter` at all, instead you are passing it in through the constructor. Where does it get created? this could be the problem source... – Silvano Brugnoni Feb 18 '12 at 10:46