Google Cloud Storageを利用する (5/5)
作成:2012-06-10 11:20
更新:2012-06-10 11:31
更新:2012-06-10 11:31
■バイナリファイルの読み書きは?
テキストファイルの読み書きについてはこれでわかりました。ではバイナリファイルは? 実は、こちらはもっと簡単なのです。読み書き共に、ReaChannel/WriteChannelを作成するところまでは同じです。これらのインスタンスが用意できたら、そこから直接メソッドを呼び出してバイナリデータを読み書きできるのです。
・バイナリデータの読み込み
・バイナリデータの書き出し
readは、バイナリデータを読み込んで引数に用意したByteBufferにコピーします。またwriteは引数のByteBufferをバイナリデータとして書き出します。これらを利用することでバイナリデータの読み書きも可能になります。
では、先ほどのサンプルを修正して、バイナリファイルとして保存するようにしてみましょう。基本的なインターフェイス(JSP部分)は同じですが、テキストファイルではなく、バイナリデータに変換して保存をしてみます。(下のリストを参照)
では、ファイルの書き出し部分から見てみましょう。ここでは、GSFileOptionsBuilder を作成する際、Mimeタイプを"binary/octet-stream"にしてあります。
・バイナリデータの読み込み
《ReaChannel》.read(《ByteBuffer》);
・バイナリデータの書き出し
《WriteChannel》.write(《ByteBuffer》);
readは、バイナリデータを読み込んで引数に用意したByteBufferにコピーします。またwriteは引数のByteBufferをバイナリデータとして書き出します。これらを利用することでバイナリデータの読み書きも可能になります。
では、先ほどのサンプルを修正して、バイナリファイルとして保存するようにしてみましょう。基本的なインターフェイス(JSP部分)は同じですが、テキストファイルではなく、バイナリデータに変換して保存をしてみます。(下のリストを参照)
では、ファイルの書き出し部分から見てみましょう。ここでは、GSFileOptionsBuilder を作成する際、Mimeタイプを"binary/octet-stream"にしてあります。
GSFileOptionsBuilder optionsBuilder = new GSFileOptionsBuilder()そして、テキストからByteBufferインスタンスを作成し、それをwriteで出力しています。
……略……
.setMimeType("binary/octet-stream")
String data = title + "\n\n" + content;これで、バイナリデータを書きだすことができます。テキストよりだいぶ簡単ですね。――続いて読み込み部分を見てみましょう。
ByteBuffer buffer = ByteBuffer.wrap(data.getBytes());
writeChannel.write(buffer);
ByteBuffer buffer = ByteBuffer.allocate(1024);あらかじめByteBufferインスタンスを用意しておき、readでこのインスタンスにバイナリデータを読み込みます。後は、ここからStringを生成して出力するだけですね。
readChannel.read(buffer);
out.println(new String(buffer.array()));これで、保存したバイナリデータからテキストを生成して表示できました。今回はテキストのエンコードなど何も考えていませんから、日本語などは読み込む際に文字化けしてしまいます。しかしまぁ、バイナリデータの読み書きの基本はこれでわかったのではないでしょうか。
(by. SYODA-Tuyano.)
※プログラムリストが表示されない場合
AddBlockなどの広告ブロックツールがONになっていると、プログラムリスト等が表示されない場合があります。これらのツールをOFFにしてみてください。
●プログラム・リスト●
package com.tuyano.libro.mygaeapp; import java.io.*; import java.nio.ByteBuffer; import java.nio.channels.Channels; import javax.servlet.http.*; import com.google.appengine.api.files.*; import com.google.appengine.api.files.GSFileOptions.GSFileOptionsBuilder; @SuppressWarnings("serial") public class MyGaeAppServlet extends HttpServlet { public static final String BUCKETNAME = "my_cloud_data"; public static final String FILENAME = "sample.data"; public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { response.setContentType("text/plain"); request.setCharacterEncoding("utf8"); response.setCharacterEncoding("utf8"); PrintWriter out = response.getWriter(); String filename = "/gs/" + BUCKETNAME + "/" + FILENAME; FileService fileService = FileServiceFactory.getFileService(); AppEngineFile readableFile = new AppEngineFile(filename); FileReadChannel readChannel = null; try { readChannel = fileService. openReadChannel(readableFile, true); ByteBuffer buffer = ByteBuffer.allocate(1024); readChannel.read(buffer); out.println(new String(buffer.array())); } catch(FileNotFoundException e){ out.println("no file..."); } finally { try { readChannel.close(); } catch(Exception ex){} } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { response.setContentType("text/plain"); request.setCharacterEncoding("utf8"); response.setCharacterEncoding("utf8"); String title = request.getParameter("title"); String content = request.getParameter("content"); FileService fileService = FileServiceFactory .getFileService(); GSFileOptionsBuilder optionsBuilder = new GSFileOptionsBuilder() .setBucket(BUCKETNAME) .setKey(FILENAME) .setMimeType("binary/octet-stream") .setAcl("public_read"); AppEngineFile file = fileService .createNewGSFile(optionsBuilder.build()); FileWriteChannel writeChannel = fileService.openWriteChannel(file, true); String data = title + "\n\n" + content; ByteBuffer buffer = ByteBuffer.wrap(data.getBytes()); writeChannel.write(buffer); writeChannel.closeFinally(); response.sendRedirect("/helo.jsp"); } }
※関連コンテンツ
「Google App Engine for Java(GAE/J)プログラミング入門」に戻る