How to avoid java.io.IOException: Attempted read on closed stream
Asked Answered
S

2

7

I'm trying to find a way to avoid the IOException related to the fact that I read on a closed stream.

I'm calling a webservice method that returns a Stream:

      InputStream stream = callRestWebService();
     try {
         parkingState = objectMapper.readValue(stream, ParkingState.class);
      }   catch (IOException e) {
        throw new ParkingMeasurementProviderException("Could not retrieve data.", e);
      } 

Then, I have my Web Service method where I close the get connection:

public InputStream callRestWebService() {
    int parkingId = 2803;
    String endpointURL = REST_ENDPOINT + URI_INFO_PATH + parkingId + "/parkingState";
    InputStream inputStream = null;

    // Create an instance of HttpClient.
    HttpClient httpclient = new HttpClient();

    // Create a method instance.
    GetMethod getMethod = new GetMethod(endpointURL);
    getMethod.addRequestHeader("accept", "application/json");

    try {
        // Execute the method.
        int statusCode = httpclient.executeMethod(getMethod);
        inputStream = getMethod.getResponseBodyAsStream();

     } catch (IOException e) {
       e.printStackTrace();
    } finally {
        // Release the connection.
        getMethod.releaseConnection();
    }
    return inputStream;
}

Is there a way to avoid having this exception without removing the : getMethod.releaseConnection();

The stack trace:

    Disconnected from the target VM, address: '127.0.0.1:62152', transport: 'socket'
    at be.ixor.itg.server.service.parking.hermesWS.HermesWSParkingControllerMeasurementProvider.getHermesMechelenData(HermesWSParkingControllerMeasurementProvider.java:126)
    at be.ixor.itg.server.service.parking.hermesWS.Main.main(Main.java:14)
Caused by: java.io.IOException: Attempted read on closed stream.
    at org.apache.commons.httpclient.AutoCloseInputStream.isReadAllowed(AutoCloseInputStream.java:183)
    at org.apache.commons.httpclient.AutoCloseInputStream.read(AutoCloseInputStream.java:86)
    at com.sun.org.apache.xerces.internal.impl.XMLEntityManager$RewindableInputStream.read(XMLEntityManager.java:2977)
    at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:702)
    at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:186)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:772)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
    at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:232)
    at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:284)
    at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:124)
    at be.ixor.itg.server.service.parking.hermesWS.HermesWSParkingControllerMeasurementProvider.getHermesMechelenData(HermesWSParkingControllerMeasurementProvider.java:116)
    ... 1 more
Sidelong answered 14/6, 2013 at 12:23 Comment(1)
Same thing; I still have the Exception : Caused by: java.io.IOException: Attempted read on closed stream.Sidelong
S
6

Because you are calling releaseConnection() in your finally block, the input stream will no longer be available.

If you do not expect the content to be large, you could read the data from the input stream into a buffer and return the buffer instead of the input stream. Otherwise, you will need to change your code to allow the called to process the data from the input stream before releasing the connection.

Sunstone answered 14/6, 2013 at 14:18 Comment(0)
M
0
BufferedReader br = new BufferedReader(new     
InputStreamReader((response.getEntity().getContent())));
String response = br.readLine();
System.out.println("response" + response );

This code is working for me.

Maher answered 9/10, 2018 at 11:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.