Closed wonderfly closed 9 years ago
From yan...@google.com on October 20, 2011 06:20:24
Please try to reproduce and investigate.
Owner: rmis...@google.com
Labels: -Priority-Medium Priority-High
From roy.smit...@gmail.com on October 21, 2011 07:30:29
In case it helps, I've mimiced some code from HttpResponse.getContent() [HttpResponse:360] to create a workaround in my app...
InputStream is = response.getContent();
// ----- start of kludge --------- Logger logger = Logger.getLogger(HttpTransport.class.getName()); boolean loggable = logger.isLoggable(Level.CONFIG) || logger.isLoggable(Level.ALL); if (!loggable && is.available() == 0) { ByteArrayOutputStream is2 = new ByteArrayOutputStream(); AbstractInputStreamContent.copy(response.getContent(), is2); return is2.toByteArray(); } // ----- end of kludge ---------
content = new byte[is.available()]; is.read(content); return content;
From yan...@google.com on October 31, 2011 12:25:51
You are making the assumption that available() returns the number of bytes in the content. In fact, the documentation of available() says the following:
* Returns an estimate of the number of bytes that can be read (or
* skipped over) from this input stream without blocking by the next
* invocation of a method for this input stream. The next invocation
* might be the same thread or another thread. A single read or skip of this
* many bytes will not block, but may read or skip fewer bytes.
*
* \<p> Note that while some implementations of {@code InputStream} will return
* the total number of bytes in the stream, many will not. It is
* never correct to use the return value of this method to allocate
* a buffer intended to hold all data in this stream.
So if you change the last 3 lines in your code from:
content = new byte[is.available()]; is.read(content); return content
to this:
ByteArrayOutputStream debugStream = new ByteArrayOutputStream();
AbstractInputStreamContent.copy(is, debugStream);
return debugStream.toByteArray();
Everything should work as expected.
Status: ByDesign
From roy.smit...@gmail.com on November 02, 2011 02:54:26
Hi Yaniv. Many thanks for looking into this for me. Your suggestion works great.
From roy.smit...@gmail.com on October 19, 2011 19:49:17
Version of google-http-java-client (e.g. 1.5.0-beta)? 1.5.0-beta Java environment (e.g. Java 6, Android 2.3, App Engine)? Android 2.2 emulator on Fedora 16 jdk1.6.0_27 Describe the problem. With logging disabled,downloaded content has zero bytes. With logging enabled, downloaded content is correct.
Code fragment to isolate fault....... ----------- code start ------------------- private void doDtest() { String url = " https://docs.google.com/feeds/download/documents/export/Export?id=12DquFymgKDTeIKkHa-OQuO5V-pHz-1v5EKAxaQPwQm0&exportFormat=html&format=html"; byte[] content1 = da.fetchDocumentContent(url, "xls"); System.out.println("===========1 "+content1.length); Logger.getLogger("com.google.api.client").setLevel(Level.INFO); Logger.getLogger("com.google.api.client.http").setLevel(Level.ALL); byte[] content2 = da.fetchDocumentContent(url, "xls"); System.out.println("===========2 "+content2.length; Logger.getLogger("com.google.api.client").setLevel(Level.INFO); Logger.getLogger("com.google.api.client.http").setLevel(Level.OFF); byte[] content3 = da.fetchDocumentContent(url, "xls"); System.out.println("===========3 "+content3.length; }
byte[] da.fetchDocumentContent(String url, String type) { HttpRequest request = requestFactory.buildGetRequest(url); request.setDisableContentLogging(true); l("[DA733] fdc url=" + request.getUrl() + " type=" + type); HttpResponse response = null; response = request401(request); response.setDisableContentLogging(true); InputStream is = response.getContent(); content = new byte[is.available()]; is.read(content); return content } ------------ code end ---------------------
------------ log start -------------------- I/System.out( 4134): Tue Oct 18 21:32:31 GMT+00.00 2011) [DA733] fdc url= https://docs.google.com/feeds/download/documents/export/Export?id=12DquFymgKDTeIKkHa-OQuO5V-pHz-1v5EKAxaQPwQm0&exportFormat=xls&format=xls type=xls D/dalvikvm( 59): GC_EXTERNAL_ALLOC freed 16768 objects / 830328 bytes in 126ms I/System.out( 4134): ===========1 0 I/System.out( 4134): Tue Oct 18 21:32:32 GMT+00.00 2011) [DA733] fdc url= https://docs.google.com/feeds/download/documents/export/Export?id=12DquFymgKDTeIKkHa-OQuO5V-pHz-1v5EKAxaQPwQm0&exportFormat=xls&format=xls type=xls D/dalvikvm( 4134): GC_FOR_MALLOC freed 11184 objects / 571448 bytes in 58ms I/System.out( 4134): ===========2 4096 I/System.out( 4134): Tue Oct 18 21:32:33 GMT+00.00 2011) [DA733] fdc url= https://docs.google.com/feeds/download/documents/export/Export?id=12DquFymgKDTeIKkHa-OQuO5V-pHz-1v5EKAxaQPwQm0&exportFormat=xls&format=xls type=xls I/System.out( 4134): ===========3 0 ------------- log end ------------------------- How would you expect it to be fixed? The results should be identical regardless of logging
Original issue: http://code.google.com/p/google-http-java-client/issues/detail?id=45