2011-11-20 33 views
10

可能性の重複:
Android:“Unexpected end of stream” exception downloading large files予期しないストリーム終了エラーが発生しましたか?

私はおよそのファイルをdownlodingよ。私は私のコード行でエラー「ストリームの予期せぬ終了」を取得downlod中のHttpURLConnectionを使って5メガバイトが、途中:ここ

     while ((count = input.read(data)) > 0) { 

はLOGです:

11-20 16:05:55.749: ERROR/PRINTSTACK(3425): STACK:unexpected end of stream 
11-20 16:05:55.749: WARN/System.err(3425): java.io.IOException: unexpected end of stream 
11-20 16:05:55.749: WARN/System.err(3425):  at org.apache.harmony.luni.internal.net.www.protocol.http.FixedLengthInputStream.read(FixedLengthInputStream.java:47) 
11-20 16:05:55.749: WARN/System.err(3425):  at java.io.BufferedInputStream.read(BufferedInputStream.java:319) 
11-20 16:05:55.749: WARN/System.err(3425):  at java.io.FilterInputStream.read(FilterInputStream.java:133) 
11-20 16:05:55.759: WARN/System.err(3425):  at com.conjure.skiproj.DownloadService$Job.process(DownloadService.java:265) 
11-20 16:05:55.759: WARN/System.err(3425):  at com.conjure.skiproj.DownloadService$1.run(DownloadService.java:193) 
11-20 16:05:55.759: WARN/System.err(3425):  at java.lang.Thread.run(Thread.java:1019) 

ヘルプ! 1

EDIT:InputStreamを設定するコードの詳細情報:

HttpURLConnection conexion = (HttpURLConnection)url.openConnection(); 
         conexion.setRequestMethod("GET"); 
         conexion.setReadTimeout(20000); 
         conexion.connect(); 
         File file = new File(root.getAbsolutePath()+"/", fileName); 

          int lenghtOfFile = conexion.getContentLength(); 

          InputStream input = new BufferedInputStream(url.openStream()); 

          OutputStream output = new FileOutputStream(file); 

          byte data[] = new byte[8192]; 
........................... 

EDIT 2:次の行に、ダウンロードの25%に新しいエラーはIndexOutOfBoundsExceptionを手に入れた:

output.write(data, 0, count); 

以下LOG:

11-20 17:47:02.311: ERROR/totaltotal(303): 24 
11-20 17:47:02.311: INFO/System.out(303): countcountcount:4332 
11-20 17:47:02.330: ERROR/totaltotal(303): 24 
11-20 17:47:02.330: INFO/System.out(303): countcountcount:2904 
11-20 17:47:02.330: ERROR/totaltotal(303): 25 
11-20 17:47:02.330: INFO/System.out(303): countcountcount:1452 
11-20 17:47:02.330: ERROR/totaltotal(303): 25 
11-20 17:47:02.650: INFO/System.out(303): countcountcount:4356 
11-20 17:47:02.650: ERROR/totaltotal(303): 25 
11-20 17:47:02.650: INFO/System.out(303): countcountcount:-1 
11-20 17:47:02.660: ERROR/totaltotal(303): 25 
11-20 17:47:02.892: DEBUG/dalvikvm(303): GC_FOR_MALLOC freed 10770 objects/490896 bytes in 143ms 
11-20 17:47:03.060: ERROR/PRINTSTACK(303): STACK:Arguments out of bounds 
11-20 17:47:03.060: WARN/System.err(303): java.lang.IndexOutOfBoundsException: Arguments out of bounds 
11-20 17:47:03.070: WARN/System.err(303):  at java.io.FileOutputStream.write(FileOutputStream.java:288) 
11-20 17:47:03.080: WARN/System.err(303):  at com.conjure.skiproj.DownloadService$Job.process(DownloadService.java:275) 
11-20 17:47:03.080: WARN/System.err(303):  at com.conjure.skiproj.DownloadService$1.run(DownloadService.java:191) 
11-20 17:47:03.080: WARN/System.err(303):  at java.lang.Thread.run(Thread.java:1096) 

EDIT 3: 私は戻ってFixedLengthInputStreamにエラーをトレースし次にこのメソッドがあるAbstractHttpInputStreamクラスに戻ります。

/** 
      * Calls abort on the cache entry and disconnects the socket. This 
      * should be invoked when the connection is closed unexpectedly to 
      * invalidate the cache entry and to prevent the HTTP connection from 
      * being reused. HTTP messages are sent in serial so whenever a message 
      * cannot be read to completion, subsequent messages cannot be read 
      * either and the connection must be discarded. 
      * 
      * <p>An earlier implementation skipped the remaining bytes, but this 
      * requires that the entire transfer be completed. If the intention was 
      * to cancel the transfer, closing the connection is the only solution. 
      */ 
      protected final void unexpectedEndOfInput() { 
       if (cacheRequest != null) { 
        cacheRequest.abort(); 
       } 
       httpURLConnection.releaseSocket(false); 
      } 

HTTPメッセージエラーの場合、ダウンロードストリーム全体がキャンセルされるようです。

+0

'url.openStream()'を使用する代わりに、 'InputStream input = conexion.getInputStream();'を使って 'BufferedInputStream'を作成してみてください。 – Squonk

+0

ちょうどこれを試して、それは動作しません! – bytebiscuit

+1

誰かがこの問題を解決するか、またはどのようなヒントや指示を行っても問題ありません。 – bytebiscuit

答えて

0

ネットワークに問題がある場合は、私が理解できる限り0をカウントすることができます。 ...(通常応答のContent-Lengthヘッダに設定された)バイト数の期待値が応答して実際のデータよりも大きい場合、例外がFixedLengthInputStreamによってスローさ

while ((count = input.read(data)) != -1) { 
    if (count != 0) { 
     ... 
    } 
} 
+0

まだエラーがありません:S – bytebiscuit

+0

私は0がここで許可されるべきだとは思わない。 [docs for InputStream](http://download.oracle.com/javase/6/docs/api/)から:bの長さがゼロの場合、バイトは読み取られず、0が返されます。それ以外の場合は、少なくとも1バイトを読み取ろうとしています。ストリームがファイルの最後にあるために使用可能なバイトがない場合、値-1が返されます。それ以外の場合、少なくとも1バイトが読み込まれ、b._ –

+0

@Tedに格納されます。OK、ドキュメントに記載されていることに同意しますが、私のアプリがいくつかのファイルをダウンロードしているときにベータテスターの1人に問題が発生しました。 Javaプログラマー自身、彼は私のコードを上記の私の答えと同様に構造化することを提案しました。彼は、ネットワークストリームに関連付けられているときに0バイトが返ってくる可能性があると提案しました。彼はダウンロードの失敗を修正しました。さらに、この場合OPの問題を修正しなかったので、それは疑問点です。 – Squonk

8

を試みます。内容長ヘッダーが正しいことを確認してください。コンテンツの長さに独自の値を指定している場合は、それが正しいことを確認してください。

入力ストリームを設定するコードが表示されます。

+0

ただ私の質問を編集しました。もう少し情報を提供しました。 – bytebiscuit

+0

コンテンツの長さのヘッダーが間違っているように見えます。また、データを読み込んでファイルにダンプするだけであれば、入力ストリームをBufferedInputStreamにラップする必要はありません。すでに独自のバッファを使用しており、オーバーバッファーを追加するだけのダブルバッファリングを行っています。それは何にも影響しないはずですが、そのラッパーを取り除くことを試みてください。 –

+0

コンテンツの長さは問題ありません。ファイルのサイズが正しくなっています。5171472(〜5.2MB) – bytebiscuit

関連する問題