java FileInputStream.read()方法循环触发若干次

siv3szwd  于 2023-01-07  发布在  Java
关注(0)|答案(1)|浏览(149)

我想读一个文件**(89432字节)**,我写了代码:

FileInputStream fis = new FileInputStream(wantsUploadFile);
byte[] chunkBytes = new byte[89432];
int chunkIndex = 0;
while (fis.read(chunkBytes) != -1){
  // read file logic
  Log.e("XXX","loop is triggered")
  ...
}

但是我发现while循环会触发几次(次数〉10+)。但是当我调试时,这个错误没有出现。为什么会发生这种情况?T_T
编译代码:

FileInputStream fis = new FileInputStream(wantsUploadFile);

                        byte[] chunkBytes = new byte[uploadEntity.getFile().length()<=uploadEntity.getChunkSize()? (int)uploadEntity.getFile().length() : uploadEntity.getChunkSize()];
                        int chunkIndex = 0;
                        while (fis.read(chunkBytes) != -1){
                            OkHttpClient okHttpClient = new OkHttpClient();
                            RequestBody requestBody = RequestBody.create(MediaType.parse("application/octet-stream"),chunkBytes);
                            int finalChunkIndex = chunkIndex;
                            ProgressRequestBody progressRequestBody = new ProgressRequestBody(requestBody, chunkIndex, new ProgressRequestBody.OnUploadSizeChangedListener() {
                                @Override
                                public void chunkChanged(int uploadedSize) {
                                    if (onProgressChangeListener != null){
                                        int chunkTotalSize = uploadEntity.getChunkSize();
                                        UploadEntity wantsModifiedEntity = tasks.get(key);
                                        wantsModifiedEntity.setUploadedSize(wantsModifiedEntity.getUploadedSize() + uploadedSize);
                                        tasks.put(key,wantsModifiedEntity);
                                        int progressPresent =  (uploadedSize / chunkTotalSize) * 100;
                                        if (onProgressChangeListener != null){
                                            onProgressChangeListener.chunkChanged(uploadEntity.getTaskId(), finalChunkIndex,progressPresent);
                                        }
                                    }
                                }
                            });
                            long contentLength = uploadEntity.getChunkSize() <= uploadEntity.getTotalSize() ? uploadEntity.getChunkSize():uploadEntity.getTotalSize();
                            Request request = new Request.Builder()
                                    .url(uploadEntity.getChunkUrls().get(chunkIndex))
                                    .post(progressRequestBody)
                                    .header("Cookie", GlobalRunningConfiguration.authentication_cookie_token)
                                    .header("content-length",String.valueOf(contentLength- 1))
                                    .build();
                            int finalChunkIndex1 = chunkIndex;
                            okHttpClient.newCall(request).enqueue(new Callback() {
                                @Override
                                public void onFailure(Call call, IOException e) {
                                    Log.e(LOG_TAG,"文件段上传发生错误!!!!chunkIndex="+finalChunkIndex1);
                                    if (onProgressChangeListener != null) onProgressChangeListener.chunkFinished(uploadEntity.getTaskId(), finalChunkIndex1,true);
                                    // 移除任务
                                    tasks.remove(key);
                                }

                                @Override
                                public void onResponse(Call call, Response response) throws IOException {
                                    Log.e(LOG_TAG,"文件段["+ finalChunkIndex1 +"] 上传完毕,response= "+response.body().string());
                                    if (onProgressChangeListener != null){
                                        onProgressChangeListener.chunkFinished(uploadEntity.getTaskId(), finalChunkIndex1,false);
                                    }
                                    tasks.remove(key);
                                }
                            });

                            // chunk add
                            chunkIndex ++;
                            Log.e(LOG_TAG,"LOOP EXED");

                        }

这是日志:enter image description here

ocebsuys

ocebsuys1#

FileInputStream fis = new FileInputStream(wantsUploadFile);
   int chunkIndex = 0;
   do{
       byte[] chunkBytes = new byte[89432];
       chunkIndex = fis.read(chunkBytes);
       // read file logic
       Log.e("XXX","loop is triggered")
       ...
    }while (chunkIndex != -1)

FileInputStream fis = new FileInputStream(wantsUploadFile);
byte[] chunkBytes = new byte[89432];
int chunkIndex = 0;
while (fis.read(chunkBytes , 0, chunkBytes.length) != -1){
    chunkIndex++;
    chunkBytes=new byte[89432] ;
   // read file logic
   Log.e("XXX","loop is triggered")
   ...
} fis.close();

相关问题