Remember once NegativeArraySizeException

Problem Description: The server receives the message returned when the background, suggesting java.lang.NegativeArraySizeException

Analysis: The reason for this anomaly returned, under normal circumstances there is no message for the prompt return empty packet analysis is preliminary response packet stream length out of the question

Baidu, a similar situation: https://stackoverflow.com/questions/11207897/negative-array-size-exception

Content excerpt:

try{
     connection = (HttpConnection)Connector.open("http://someurl.xml",Connector.READ_WRITE);
     URLEncodedPostData postData = new URLEncodedPostData(URLEncodedPostData.DEFAULT_CHARSET, false);
     postData.append("username", "loginapi");
     postData.append("password", "myapilogin");
     postData.append("term", word);

     connection.setRequestMethod(HttpConnection.POST);
     connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
     connection.setRequestProperty("User-Agent","Profile/MIDP-2.0 Configuration/CLDC-1.0");
     requestOut = connection.openOutputStream();
     requestOut.write(postData.getBytes());
     String contentType = connection.getHeaderField("Content-type"); 
     detailIn = connection.openInputStream();         
     int length = (int) connection.getLength();
     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     if (length> 0) {// the case where the length is not determined, byte array length to -1 if an error
         byte info[] = new byte[length];
         int bytesRead = detailIn.read(info);
         while(bytesRead > 0) { 
             baos.write(info, 0, bytesRead); 
             bytesRead = detailIn.read(info); 
             }
         baos.close();
         connection.close();
         requestSuceeded(baos.toByteArray(), contentType);

         detailIn.read(info);
     }
     else
     {
          System.out.println("Negative array size");
     }
           requestOut.close();
           detailIn.close();
           connection.close();
    }

  Conclusion: HTTP server sends a response when no content.length length determination, in accordance with a conventional process in response to the packet length error, leading to an abnormal length packet receiver

Guess you like

Origin www.cnblogs.com/bobkingblog/p/11023620.html