Java code:

URL urlw = new URL("http://95.161.19.3:8000/low"); HttpURLConnection Connection = (HttpURLConnection) urlw.openConnection(); recordingStream = Connection.getInputStream(); byte[] buffer= new byte[1]; while (true){ if (recordingStream.read(buffero, 0, 1) != -1 && isFlg) { writer.write(buffer, 0, 1); // запись в файл. writer.flush(); } } 

It reads a stream in a loop one by one (!) Byte and writes this byte to a binary file. Apparently it was developed in the days of GPRS. Increasing the buffer results in a loss of bytes. The file is not read.

Python code:

 bp = urllib.urlopen("http://95.161.19.3:8000/low") while True: dpa=bp.read(1024) // да хоть 1000000, и без потерь. writer.write(dpa) 

Question, is it also possible in java ?

1 answer 1

 byte[] buffer= new byte[1024]; while (true){ int lenght = recordingStream.read(buffero); if (lenght != -1 && isFlg) { writer.write(buffer, 0, lenght); // запись в файл. writer.flush(); } else break; } 
  • There is an option to immediately read 80 kb per turn, - loss of bytes, the file is filled with hell than. Well, you can and so. But what if you need to count exactly 16,000 bytes? - fangobobo