Android 如何更改Outputstream类中的缓冲区计数
我正在使用下面的类来扩展outputstream,以了解上传的进度Android 如何更改Outputstream类中的缓冲区计数,android,count,outputstream,Android,Count,Outputstream,我正在使用下面的类来扩展outputstream,以了解上传的进度 import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import org.apache.http.entity.InputStreamEntity; public class CountingInputStreamEntity extends InputStreamEntity { private
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.http.entity.InputStreamEntity;
public class CountingInputStreamEntity extends InputStreamEntity {
private UploadListener listener;
private long length;
public CountingInputStreamEntity(InputStream instream, long length) {
super(instream, length);
this.length = length;
}
public void setUploadListener(UploadListener listener) {
this.listener = listener;
}
@Override
public void writeTo(OutputStream outstream) throws IOException {
super.writeTo(new CountingOutputStream(outstream));
}
class CountingOutputStream extends OutputStream {
private long counter = 0l;
private OutputStream outputStream;
public CountingOutputStream(OutputStream outputStream) {
this.outputStream = outputStream;
}
@Override
public void write(int oneByte) throws IOException {
this.outputStream.write(oneByte);
counter++;
if (listener != null) {
int percent = (int) ((counter * 100)/ length);
listener.onChange(percent);
}
}
}
public interface UploadListener {
public void onChange(int percent);
}
}
调用类:
final HttpResponse resp;
final HttpClient httpClient = new DefaultHttpClient();
final HttpPost post = new HttpPost(UPLOAD_URL);
ParcelFileDescriptor fileDescriptor = context.getContentResolver().openFileDescriptor(uri, "r");
InputStream in = context.getContentResolver().openInputStream(uri);
CountingInputStreamEntity entity = new CountingInputStreamEntity(in, fileDescriptor.getStatSize());
entity.setListener(this);
post.setEntity(entity);
resp = httpClient.execute(post);
默认情况下,write方法中的len变量返回2048字节。有没有办法将len变量更改为自定义值。我希望len变量为64kb如何实现这一点?任何人都可以帮助我解决这个问题
我可以增加写入服务器的字节数。
默认情况下,write方法中的len变量返回2048个字节
这是什么意思?谁在设置此变量?通常,write(byte[]bytes)
方法的实现类似于write(bytes,0,bytes.length)
。我们是否可以将write(byte[]bytes)字节数组大小更改为特定值,因为我需要减少上载时间。如果是2048字节,则上载大文件所需的时间很长。否。给定的字节数组中已经有一个大小和一些数据。我不知道你会如何改变这一点,除非发明数据来填充额外的长度。谁调用这个方法并传递字节[]?