I want to read a file (89432 bytes), I have wrote code:
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")
...
}
But I find The while
loop will trigger several times(times>10 ).But when I debug this error is not appear.
Why does this happen?T_T
The Complated code :
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");
}
This is log : enter image description here
CodePudding user response:
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)
OR
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();