kikumotoのメモ帳

インフラ・ミドル周りを中心に、興味をもったことを適当な感じで。twitter : @takakiku

CloudStoreを使う・Java編:読み書き

Javaライブラリを使って、CloudStoreに読み書きをする方法をまとめておく。

読み込み

サンプルコードは以下の通り。CloudStore の API は、Java New I/O を使うようになっているので、それがらみのコードが多くなってしまっているが、実質的には

  • メタサーバと、ポートを指定して KfsAccessのインスタンスを作る。
  • kfs_open でファイルを開き、入力チャネルを取得する。
  • 入力チャネルから読む込む。

という単純なものである。

public class KfsReadSample {
    public static void main(String[] args) throws IOException {
        String metaServer = args[0].trim();
        int port = Integer.parseInt(args[1].trim());
        String inputPath = args[2].trim();

        Charset charset = Charset.forName("UTF-8");
        CharsetDecoder decoder = charset.newDecoder();
        decoder.onMalformedInput(CodingErrorAction.IGNORE)
                .onUnmappableCharacter(CodingErrorAction.IGNORE);

        ByteBuffer buffer = ByteBuffer.allocate(1024);
        int n = (int)(buffer.capacity() * (double)decoder.averageCharsPerByte());
        CharBuffer out = CharBuffer.allocate(n);

        KfsAccess kfsAccess = new KfsAccess(metaServer, port);
        KfsInputChannel inputChannel = kfsAccess.kfs_open(inputPath);
        if (inputChannel != null) {
            try {
                while (inputChannel.read(buffer) != -1) {
                    buffer.flip();
                    decoder.decode(buffer, out, false);
                    
                    out.flip();
                    System.out.print(out.toString());
                    
                    buffer.compact();
                    out.clear();
                }
                buffer.flip();
                decoder.decode(buffer, out, true);
                decoder.flush(out);
                out.flip();
                System.out.print(out.toString());
            } finally {
                inputChannel.close();
            }
        }
    }
}

ちなみに、Java New I/O での読み込み・デコード処理は

を参考にした。

書き込み

手順的には、読み込みと似たようなもので、

  • メタサーバと、ポートを指定して KfsAccessのインスタンスを作る。
  • kfs_create でファイルを作成し、出力チャネルを取得する。
  • 出力チャネルに書き込む。

となる。ファイルをバッファ経由でコピーする場合を例として、以下にサンプルコード載せておく。
注意としては、kfs_create に複製数を指定しないとデフォルトでは、'1' が使われるということ。

public class KfsWriteSample {

    public static void main(String[] args) throws IOException {
        String metaServer = args[0].trim();
        int port = Integer.parseInt(args[1].trim());
        String localPath = args[2].trim();
        String kfsPath = args[3].trim();

        ByteBuffer buffer = ByteBuffer.allocate(1024);
        FileChannel src = new FileInputStream(localPath).getChannel();
        try {
            KfsAccess kfsAccess = new KfsAccess(metaServer, port);
            KfsOutputChannel dest = kfsAccess.kfs_create(kfsPath, 2);
            if (dest != null) {
                try {
                    while (src.read(buffer) != -1) {
                        buffer.flip();
                        dest.write(buffer);
                        buffer.clear();
                    }
                } finally {
                    dest.close();
                }
            }
        } finally {
            src.close();
        }
    }
}