我正在分析正在加载二进制文件的代码.加载时间约为15秒.
我的大部分加载时间来自加载二进制数据的方法.
我有以下代码来创建我的DataInputStream:
is = new DataInputStream(
new GZIPInputStream(
new FileInputStream("file.bin")));
Run Code Online (Sandbox Code Playgroud)
我把它改成了这个:
is = new DataInputStream(
new BufferedInputStream(
new GZIPInputStream(
new FileInputStream("file.bin"))));
Run Code Online (Sandbox Code Playgroud)
所以在我做了这个小修改之后,加载代码从15秒变为4.
但后来我发现BufferedInputStream有两个构造函数.另一个构造函数允许您显式定义缓冲区大小.
我有两个问题:
我正在尝试从网址下载imags然后解码它们.问题是,我不知道它们有多大,如果我立即解码它们,应用程序会因太大的图像而崩溃.
我正在做以下操作,它适用于大多数图像,但是对于其中一些图像,它会抛出java.io.IOException: Mark has been invalidated异常.这不是一个大小问题,因为它发生在75KB或120KB的图像上,而不是20MB或45KB的图像.此格式也不重要,因为它可能发生在jpg或png图像上.
pis是一个InputStream.
Options opts = new BitmapFactory.Options();
BufferedInputStream bis = new BufferedInputStream(pis);
bis.mark(1024 * 1024);
opts.inJustDecodeBounds = true;
Bitmap bmImg=BitmapFactory.decodeStream(bis,null,opts);
Log.e("optwidth",opts.outWidth+"");
try {
bis.reset();
opts.inJustDecodeBounds = false;
int ratio = opts.outWidth/800;
Log.e("ratio",String.valueOf(ratio));
if (opts.outWidth>=800)opts.inSampleSize = ratio;
return BitmapFactory.decodeStream(bis,null,opts);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
Run Code Online (Sandbox Code Playgroud) 我正在使用Java的BufferedInputStream类来读取发送到套接字的字节.套接字的数据是HTTP格式,因此通常是具有已定义内容长度的标头,然后是一些内容.
我遇到的问题是有时BufferedInputStream.read()无法读取发送给它的全部数据.它返回读取的字节数,但这比发送的要少得多.我已经验证了Wireshark发送的字节,并且可以确认正在传输完整的消息.)
示例代码如下:
BufferedInputStream inFromClient = new BufferedInputStream(socket.getInputStream());
int contentLength = getContentLengthFromHeader();
byte[] b = new byte[contentLength];
int bytesRead = inFromClient.read(b, 0, contentLength);
Run Code Online (Sandbox Code Playgroud)
一旦read()完成,有时bytesRead等于contentLength但在其他情况下read()似乎不会读到内容的结尾.有没有人对正在发生的事情有任何想法?Java缓冲输出?有更好的方法从套接字读取?
我已经读过,如果你以小块的形式读取输入流,那么在输入流周围包装一个BufferedInputStream是有好处的.否则,使用它可能实际上会产生不利影响.
当输入流是使用HttpURLConnection(或您最喜欢的网络库,例如OkHttp)获取的位图数据时,情况是什么......这是一个帮助还是障碍?
我不仅在整体时间/速度方面,而且在弹性方面都不知道......在连接正在进出的片状网络条件下,是否会使用缓冲区帮助?
boolean useBufferedInputStream = true; // <--- true or false?
URL url = new URL("https://example.com/my_bitmap.png");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream inputStream;
if (useBufferedInputStream) {
inputStream = new BufferedInputStream(connection.getInputStream());
} else {
inputStream = connection.getInputStream();
}
Bitmap bmp = BitmapFactory.decodeStream(inputStream);
Run Code Online (Sandbox Code Playgroud) 在读取HttpURLConnection的InputStream时,是否有任何理由使用以下哪一个而不是另一个?我见过两个例子都用过.
手动缓冲:
while ((length = inputStream.read(buffer)) > 0) {
os.write(buf, 0, ret);
}
Run Code Online (Sandbox Code Playgroud)
的BufferedInputStream
is = http.getInputStream();
bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append(current);
}
Run Code Online (Sandbox Code Playgroud)
编辑我一般都是HTTP的新手,但我想到的一个考虑因素是,如果我使用持久的HTTP连接,我不能只读到输入流是空的吗?在这种情况下,我不需要读取消息长度并只读取该长度的输入流吗?
同样,如果不使用持久连接,那么我所包含的代码在正确读取流方面是否100%好?
mock.request返回响应:body作为BufferedInputStream.我需要打印并将其作为字符串进行比较.我该如何转换它?
当我尝试将响应作为消息传递给我的断言时,我看到一个原始输出,例如
(is (= 200 (:status response) (:body response)))
=> #object[java.io.BufferedInputStream 0x211bdf40 java.io.BufferedInputStream@211bdf40]
Run Code Online (Sandbox Code Playgroud)
相关问题是特定于Java的.
新问题;
我在内存中有一个位图;
Private Bitmap MyPicture;
Run Code Online (Sandbox Code Playgroud)
然后,我从相机的成像仪填充MyPicture.我需要使用来自apache commons的FTP客户端上传该照片.
fcon.storeFile("filename", new BufferedInputStream(MyPicture.????));
Run Code Online (Sandbox Code Playgroud)
但是apache想要一个BufferedInputStream.如何将内存位图转换为内存流?
多谢你们!
我被赋予了从服务器复制数据的任务.我正在使用BufferedInputStream和输出流来复制数据,我正在逐字节地进行.即使它正在运行但复制数据需要花费很长时间,因为其中一些数据是100的MB,所以肯定它不会起作用.任何人都可以建议我Byte副本的Byte副本,以便我的代码可以复制几百MB的文件.缓冲区是2048.
以下是我的代码的样子:
static void copyFiles(SmbFile[] files, String parent) throws IOException {
SmbFileInputStream input = null;
FileOutputStream output = null;
BufferedInputStream buf_input = null;
try {
for (SmbFile f : files) {
System.out.println("Working on files :" + f.getName());
if (f.isDirectory()) {
File folderToBeCreated = new File(parent+f.getName());
if (!folderToBeCreated.exists()) {
folderToBeCreated.mkdir();
System.out.println("Folder name " + parent
+ f.getName() + "has been created");
} else {
System.out.println("exists");
}
copyFiles(f.listFiles(), parent + f.getName());
} else {
input = (SmbFileInputStream) f.getInputStream();
buf_input = …Run Code Online (Sandbox Code Playgroud) 我想用bufferedInputStream和bufferedOutputStream大型的二进制文件从源文件复制到目标文件.
这是我的代码:
byte[] buffer = new byte[1000];
try {
FileInputStream fis = new FileInputStream(args[0]);
BufferedInputStream bis = new BufferedInputStream(fis);
FileOutputStream fos = new FileOutputStream(args[1]);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int numBytes;
while ((numBytes = bis.read(buffer))!= -1)
{
bos.write(buffer);
}
//bos.flush();
//bos.write("\u001a");
System.out.println(args[0]+ " is successfully copied to "+args[1]);
bis.close();
bos.close();
} catch (IOException e)
{
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
我可以成功复制,但后来我使用
cmp src dest
Run Code Online (Sandbox Code Playgroud)
在命令行中比较两个文件.错误消息
cmp:文件上的EOF
出现.我可以知道我哪里错了吗?
在工作时BufferedOutputStream发现它并没有抛出IOException我们在关闭流后写的时候.
为了验证我的结果,我检查FileOutputStream发现IOException一旦我们在关闭它之后尝试写它就扔了.
public class Test {
public static void main(String[] args) {
try {
// Created a byte[] barry1 barry2
byte[] barry1 = { '1', '3' };
byte[] barray2 = { '2', '4' };
OutputStream os = new BufferedOutputStream(
new FileOutputStream("abc.txt", false));
// Writing to stream
os.write(barry1);
os.close();
os.write(barray2); // this suceeds - bug
os = new FileOutputStream("abc.txt", true);
//Writing to stream
os.write(barry1);
os.close();
os.write(barray2); // crashes here, correct.
} catch (Exception …Run Code Online (Sandbox Code Playgroud) java ×5
android ×3
bitmap ×2
file-io ×2
inputstream ×2
buffer ×1
clojure ×1
copy ×1
eof ×1
http ×1
optimization ×1
performance ×1
sockets ×1
string ×1