0

application has below piece of old servlet code for which none of the input streams was closed. I am not sure whether that is correct or wrong. Should I closed each stream (ByteArrayInputStream ,InputStream,PrintWriter)?

    public void doPost(HttpServletRequest reqest, HttpServletResponse response)  
                throws ServletException, IOException { 

            PrintWriter out=response.getWriter();
    try{
    InputStream xml = new BufferedInputStream(request.getInputStream());
    byte[] xmlbytes =getBytes(xml);
    if(xmlBytes.lenght>0){

    ByteArrayInputStream byteIn = new ByteArrayInputStream(xmlbytes);
    ObjectInputStream in = new ObjectInputStream(byteIn);
    // some logic here//
    }
}catch(Exception e)
    { out.println("Exception");

    }

    }


    Private static byte[] getBytes(InputStream is){
    ByteArrayOutputStream os = new ByteArrayOutputStream() ;
    BufferedInputStream is= new BufferdInputStream(is);
    try {
    Boolean b= false;
    while(!(b)){
    int i = is.read();
    if(i == -1)
    b= true;
    else o.write(i);
    }
    }catch(IOException e){
    return null;
    }
    return os.toByteArray();
    }
no_name22
  • 29
  • 1
  • 1
  • 5
  • here no fileinputstream is used. – no_name22 Apr 09 '18 at 15:04
  • Like discussed in the answers of the linked question, you should only close streams you open yourself. Byte array streams don't have to be closed because they hold no resources and calling close() does nothing – Joni Apr 09 '18 at 15:23

0 Answers0