0

Why i cant read POST-request with 150k chars? I can only read ~15k chars all time

InputStream is = socket.getInputStream();

ByteArrayOutputStream baos = new ByteArrayOutputStream();

while (is.available() > 0 && (length = is.read(buffer)) != -1) {
    baos.write(buffer, 0, length);
}

System.out.println(baos.toString(StandardCharsets.UTF_8.name()));

UPD: if we ignored is.available(), code freezes in the while:

InputStream is = socket.getInputStream();

ByteArrayOutputStream baos = new ByteArrayOutputStream();

while ((length = is.read(buffer)) != -1) {
    baos.write(buffer, 0, length);
}

System.out.println(baos.toString(StandardCharsets.UTF_8.name()));

There are no exceptions.

AKSTAR
  • 1
  • 1
  • Possible duplicate of https://stackoverflow.com/questions/2880722/is-http-post-limitless – Prashant Mar 24 '18 at 19:02
  • @Prashant, my POST-request is not getting restrictions, because i can read all request through the BufferedReader – AKSTAR Mar 24 '18 at 19:04

1 Answers1

0

Docs for avaiable() says:

available() Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking

So I'm going to guess that you internally have 15k buffers and you're only reading up to the end of your own buffer, not to the end of the stream. You should frankly be ignoring availabe() in this case and just call read( byte[] ) until it returns -1.

Your updated code example looks almost exactly like the code I use to read streams. I think the problem must be on the sender's side. Either the sender is not closing the stream properly, or there's some network issue that doesn't allow enough packets through.

For reference, here's the code I use to read an entire stream. (Lightly tested.)

   public static ByteArrayOutputStream readFully( InputStream ins )
           throws IOException
   {
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      byte[] bytes = new byte[ 1024 ];
      for( int length; ( length = ins.read( bytes ) ) != -1; )
         bos.write( bytes, 0, length );
      return bos;
   }
markspace
  • 10,621
  • 3
  • 25
  • 39
  • I tried to do this, but after that the code hovered in while – AKSTAR Mar 24 '18 at 19:12
  • Well, please show the code you tried that didn't work. Whenever I do that, it works fine. – markspace Mar 24 '18 at 19:13
  • InputStream is = socket.getInputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); while ((length = is.read(buffer)) != -1) { baos.write(buffer, 0, length); } String data = baos.toString(StandardCharsets.UTF_8.name()); – AKSTAR Mar 24 '18 at 19:14
  • Not in a comment; I can't read that. Edit your question and add the additional code there, mention you also tried that and it didn't work either. Please describe exactly what "didn't work" means. Provide sample output or exceptions if possible. – markspace Mar 24 '18 at 19:19
  • I've updated my answer. Unfortunately it looks like a network problem, not a problem with your code. – markspace Mar 24 '18 at 19:32
  • The problem is not in the network, because when we use is.available(), everything works – AKSTAR Mar 24 '18 at 19:36