LoginSignup
3
3

More than 5 years have passed since last update.

node-jt400を試してみた(IFS read)

Posted at

IFS read

以下のJavaコードでIFS上に作成したファイル(SJIS,UTF8,UTF16)をNodeからReadしてみます。

IfsWrite.java
public class IfsWrite {

    public static void main(String[] args) {
            AS400 as400  = new AS400("192.168.X.XXX" ,"MYUSER" ,"MYPASS");
            final String BR = System.getProperty("line.separator");

            IFSFileOutputStream fileutf8;
            IFSFileOutputStream fileutf16;
            IFSFileOutputStream filesjis;
            try {
                String str1 = "テスト文字列1行目abc";
                String str2 = "テスト文字列2行目アイウ";
                String str = str1 + BR + str2;

                byte[] utf8 = str.getBytes("UTF-8"); 
                byte[] utf16 = str.getBytes("UTF-16");
                byte[] sjis = str.getBytes(Charset.forName("Shift_JIS"));

                fileutf8 = new IFSFileOutputStream(as400, "/myIfsFolder/java-utf8.txt",IFSFileOutputStream.SHARE_ALL, true,1208);
                fileutf8.write(utf8);
                fileutf8.close();

                fileutf16 = new IFSFileOutputStream(as400, "/myIfsFolder/java-utf16.txt",IFSFileOutputStream.SHARE_ALL, true,1200);
                fileutf16.write(utf16);
                fileutf16.close();

                filesjis = new IFSFileOutputStream(as400, "/myIfsFolder/java-sjis.txt",IFSFileOutputStream.SHARE_ALL, true,932);
                filesjis.write(sjis);
                filesjis.close();

            } catch (AS400SecurityException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    }
}

文字コードの変換に、iconv-liteを使用していますのでインストールしてから実行して下さい。

npm install iconv-lite
IfsRaed.JS
var jt400 = require("node-jt400");
var express = require("express");
var iconv = require('iconv-lite');
var app = express();

var pool = jt400.pool({ host: '192.168.X.XXX', user: 'MYUSER', password: 'MYPASS' });

var server = app.listen(8888, function () {
  console.log("curl http://localhost:" + server.address().port + '/~');
});

app.get("/ifsReadSjis", function (req, res, next) {
  const ifs = pool.ifs();
  let ifsReadableStream = ifs.createReadStream("/myIfsFolder/java-sjis.txt").pipe(iconv.decodeStream("Shift_JIS"));
  let buffer = "";
  ifsReadableStream.on("data", (chunk) => {
    buffer += chunk;
  });
  ifsReadableStream.on("end", function () {
    res.send(buffer);
  });
});

app.get("/ifsReadUtf8", function (req, res, next) {
  const ifs = pool.ifs();
  let ifsReadableStream = ifs.createReadStream("/myIfsFolder/java-utf8.txt").pipe(iconv.decodeStream("UTF-8"));
  let buffer = "";
  ifsReadableStream.on("data", (chunk) => {
    buffer += chunk;
  });
  ifsReadableStream.on("end", function () {
    res.send(buffer);
  });
});

app.get("/ifsReadUtf16", function (req, res, next) {
  const ifs = pool.ifs();
  let ifsReadableStream = ifs.createReadStream("/myIfsFolder/java-utf16.txt").pipe(iconv.decodeStream("UTF-16"));
  let buffer = "";
  ifsReadableStream.on("data", (chunk) => {
    buffer += chunk;
  });
  ifsReadableStream.on("end", function () {
    res.send(buffer);
  });
});

実行結果です。

nodejt25.png

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3