IO操作(read,write,flush)メモ
9964 ワード
1シーン:urlでリモートピクチャファイルを読み込み、byte[]を返します.将来、これらのbyteはHttpClientでリモートマシンにアップロードできます.
1.1これらのbyte[]をアップロードすると、画像の一部しか表示されないことがわかります.最終的には、アップロードツールHttpClientがネットワークの問題でアップロードが不完全であることが疑われるが、urlを読んでbyte[]を取得する方法に問題がある可能性があることを何度も試みた.
エラー:BufferedInputStreamおよびreadメソッドの使用
1.2 ByteArrayOutputStreamおよびtoByteArrayメソッドの使用
1.2.1誤った方法:ByteArrayOutputStreamに書く場合、読み上げた長さではなくbuffer.lengthは書きます.これにより、画像が花になります.
1.2.2正しい書き込み:readをsrcからbufferに読み込むたびのバイト数をdestに正確に書き、destがFileに対応する場合flushが機能します.
2. シーン:Fileからbyte[]を読み込む
3.OutputStreamのflush操作について:
Flush function is to explicitly ask Java program to write something into the disk, which means doing IO. Say if we are using a bufferedOutputStream, when we do write("something"), it is stored in the buffer, but not in the disk yet. When you call flush, it will write the stuffs in your buffer to the disk. Usually, you don't need to call the flush by yourself, cause it requires IO and lower down the speed. The stream will automatically flush when the buffer is full. Unless you want some contents being updated to the disk, you can use flush.
4.リモートファイルの読み込みavailableを使用してbufferを割り当てない
Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream. The next invocation might be the same thread or another thread. A single read or skip of this many bytes will not block, but may read or skip fewer bytes.
Note that while some implementations of InputStream will return the total number of bytes in the stream, many will not. It is never correct to use the return value of this method to allocate a buffer intended to hold all data in this stream.
In short,
If you need to detect the end of the stream,
1.1これらのbyte[]をアップロードすると、画像の一部しか表示されないことがわかります.最終的には、アップロードツールHttpClientがネットワークの問題でアップロードが不完全であることが疑われるが、urlを読んでbyte[]を取得する方法に問題がある可能性があることを何度も試みた.
エラー:BufferedInputStreamおよびreadメソッドの使用
private byte[] readFileImage(String sImageUrl) throws IOException, ServiceException
{
URL url = new URL(sImageUrl);
URLConnection urlConnection = url.openConnection();
BufferedInputStream bufferedInputStream = new BufferedInputStream(urlConnection.getInputStream());
int len = bufferedInputStream.available();// 。 ,available 。 。
if (len > 1024 * 1224 * 5)
{
throw new ServiceException("The post image is larger than 5M");
}
byte[] bytes = new byte[len];
int r = bufferedInputStream.read(bytes);
if (len != r)
{
bytes = null;
bufferedInputStream.close();
throw new IOException(" ");
}
bufferedInputStream.close();
return bytes;
}
1.2 ByteArrayOutputStreamおよびtoByteArrayメソッドの使用
1.2.1誤った方法:ByteArrayOutputStreamに書く場合、読み上げた長さではなくbuffer.lengthは書きます.これにより、画像が花になります.
private byte[] getImageByteArray(InputStream ins)
{
byte[] buffer = new byte[1024];
System.out.println(buffer.length);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try
{
boolean isComplete = false;
while (!isComplete)
{
if (ins.read(buffer) != -1)
{
baos.write(buffer); // baos.write(buffer, 0,
// buffer.length); 。
// baos.flush();// flush 。
}
else
{
isComplete = true;
}
}
baos.flush();// dest ByteArrayOutputStream, flush 。
}
catch (Exception e)
{
Logging.DAODAO_SNS.error(e);
}
byte[] res = baos.toByteArray();
try
{
ins.close();
baos.close();
}
catch (IOException e2)
{
Logging.DAODAO_SNS.error(e2);
}
return res;
}
1.2.2正しい書き込み:readをsrcからbufferに読み込むたびのバイト数をdestに正確に書き、destがFileに対応する場合flushが機能します.
private byte[] getImageByteArray(InputStream ins)
{
byte[] buffer = new byte[1024];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try
{
int length = -1;
while ((length = ins.read(buffer)) != -1)
{
baos.write(buffer, 0, length);
// baos.flush();
}
}
catch (IOException e)
{
Logging.DAODAO_SNS.error(e);
}
byte[] res = baos.toByteArray();
try
{
ins.close();
baos.close();
}
catch (IOException e2)
{
Logging.DAODAO_SNS.error(e2);
}
return res;
}
2. シーン:Fileからbyte[]を読み込む
public static byte[] readFileImage(String filename)throws IOException{
BufferedInputStream bufferedInputStream=new BufferedInputStream(
new FileInputStream(filename));
int len =bufferedInputStream.available();
byte[] bytes=new byte[len];
int r=bufferedInputStream.read(bytes);
if(len !=r){
bytes=null;
throw new IOException(" ");
}
bufferedInputStream.close();
return bytes;
}
3.OutputStreamのflush操作について:
Flush function is to explicitly ask Java program to write something into the disk, which means doing IO. Say if we are using a bufferedOutputStream, when we do write("something"), it is stored in the buffer, but not in the disk yet. When you call flush, it will write the stuffs in your buffer to the disk. Usually, you don't need to call the flush by yourself, cause it requires IO and lower down the speed. The stream will automatically flush when the buffer is full. Unless you want some contents being updated to the disk, you can use flush.
4.リモートファイルの読み込みavailableを使用してbufferを割り当てない
Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream. The next invocation might be the same thread or another thread. A single read or skip of this many bytes will not block, but may read or skip fewer bytes.
Note that while some implementations of InputStream will return the total number of bytes in the stream, many will not. It is never correct to use the return value of this method to allocate a buffer intended to hold all data in this stream.
In short,
InputStream.available()
is not half as useful as you think it is. If you need to detect the end of the stream,
read()
from it and it detect if the result is -1
. Do not use available()
.