
出力ストリームをバイト配列へ変換するには
いつもお世話になります。
BufferedOutpuStreamで取得したオブジェクトを(ByteArrayOutputStreamでインスタンス生成)、バイト配列へ変換するにはどのようにすればよいでしょうか。
ByteArrayOutputStreamだと、toByteArrayメソッドでバイト配列へ変換できるのですが、効率化を考慮し、BufferedOutputStreamへ出力するように
しているのですが、この場合だと取得したBufferedOutputStreamをバイト配列へ変換する方法が分からず困っています。
宜しくお願いします。
No.1ベストアンサー
- 回答日時:
>ByteArrayOutputStreamでインスタンス生成
ByteBufferじゃ駄目なんですか?
Bruce Eckel, "Thinking in Java (4th Edition)" (Prentice Hall, 2006)
によると、p.946~p.948に、次のようにあります。
The Java “new” I/O library, introduced in JDK 1.4 in the java.nio.* packages, has one goal: speed.
(中略)
The speed comes from using structures that are closer to the operating system’s way of performing I/O: channels and buffers.
(中略)
The only kind of buffer that communicates directly with a channel is a ByteBuffer?that is, a buffer that holds raw bytes.
(中略)
Three of the classes in the “old” I/O have been modified so that they produce a FileChannel: FileInputStream, FileOutputStream, and, for both reading and writing, RandomAccessFile.
Notice that these are the byte manipulation streams, in keeping with the low-level nature of nio.
The Reader and Writer character-mode classes do not produce channels, but the class java.nio.channels.Channels has utility methods to produce Readers and Writers from channels.
Here’s a simple example that exercises all three types of stream to produce channels that are writeable, read/writeable, and readable:
//: io/GetChannel.java
// Getting channels from streams
import java.nio.*;
import java.nio.channels.*;
import java.io.*;
public class GetChannel {
private static final int BSIZE = 1024;
public static void main(String[] args) throws Exception {
// Write a file:
FileChannel fc =
new FileOutputStream("data.txt").getChannel();
fc.write(ByteBuffer.wrap("Some text ".getBytes()));
fc.close();
// Add to the end of the file:
fc =
new RandomAccessFile("data.txt", "rw").getChannel();
fc.position(fc.size()); // Move to the end
fc.write(ByteBuffer.wrap("Some more".getBytes()));
fc.close();
// Read the file:
fc = new FileInputStream("data.txt").getChannel();
ByteBuffer buff = ByteBuffer.allocate(BSIZE);
fc.read(buff);
buff.flip();
while(buff.hasRemaining())
System.out.print((char)buff.get());
}
} /* Output:
Some text Some more
*///:~
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
関連するカテゴリからQ&Aを探す
おすすめ情報
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
【C#】ハッシュテーブル(連想...
-
[Ljava.lang.Stringってなんですか
-
この警告はどうすれば?
-
【Ajax通信&Java】配列の受け取...
-
strutsでの配列の扱い方について。
-
C言語でunsigned char配列を連...
-
javaでEUC-JP文字列→UTF-8への変換
-
Javaで文字と数字が混ざったも...
-
配列をセッションに割り当てた後で
-
同じ配列またはクラスを、2回...
-
String型をbyte型へ
-
jspでの二次元配列
-
C#で配列の戻り値について
-
C言語のポインターに関する警告
-
IF関数でEmpty値を設定する方法。
-
ORA-01858: 数値を指定する箇所...
-
ループ処理の際、最後だけ","を...
-
System.err. printlnとSystem.o...
-
System.exit()の値を取得したい
-
動的配列が存在(要素が有る)か...
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
[Ljava.lang.Stringってなんですか
-
Javaで文字と数字が混ざったも...
-
この警告はどうすれば?
-
配列の中に複数存在する数がい...
-
【C#】ハッシュテーブル(連想...
-
ページング
-
C#の質問
-
n番目に大きな値を探索する
-
javaでデータベース(mysql)か...
-
C#で動的配列Listの中身をListB...
-
C言語でunsigned char配列を連...
-
同じ配列またはクラスを、2回...
-
Javaで文字を漢字であるか否か...
-
ArrayListからBean配列に値をセ...
-
Java配列の問題を教えてくださ...
-
ランダムでのboolean値の与え方
-
配列で、値の入っている要素数...
-
strutsで2次元配列をやりとりす...
-
java StringとString[]の違い
-
javaでEUC-JP文字列→UTF-8への変換
おすすめ情報