Issue with BufferedReader.read while reading from InputStream -
i trying read input stream in chunks , write file avoid memory issue, receiving data in json format , same write file using following code.
bufferedreader rd = new bufferedreader(new inputstreamreader(conn.getinputstream())); bufferedwriter writer = null; if(getoutputformat().equals(constants.uri_format_json)){ boolean enableappend = true; charbuffer buffer = charbuffer.allocate(1024); writer = new bufferedwriter(new filewriter(request.getjsonfile(isnewfile),enableappend)); int readstate = 0; while ((readstate = rd.read(buffer)) != -1) { buffer.flip(); if(isnewfile){ writer.write(buffer.tostring()); isnewfile = false; }else { writer.append(buffer.tostring()); } buffer.clear(); } writer.newline(); writer.flush(); writer.close(); }
my problem is, of json written fine, while of them contains broken data, not sure if correctly using charbuffer bufferedreader, 1 more thing have observed is, small amount of data - correctly takes charbuffer , writes file, face when move bit bigger data (around 2mb in inputstream - not big) received server. verified using readline method works fine, want read data in chunks hence using charbuffer, idea?
while debugging found "connection reset" happening in middle hence data lose. handled exception make controlled retry same request , problem got resolved. hope helps somebody.
Comments
Post a Comment