Example usage for java.nio.channels GatheringByteChannel write

List of usage examples for java.nio.channels GatheringByteChannel write

Introduction

In this page you can find the example usage for java.nio.channels GatheringByteChannel write.

Prototype

public long write(ByteBuffer[] srcs) throws IOException;

Source Link

Document

Writes a sequence of bytes to this channel from the given buffers.

Usage

From source file:com.castis.sysComp.PoisConverterSysComp.java

private List<subDataDTO> writeNodeInfoOnFile(int byteSize, ByteBuffer byteBuffer,
        GatheringByteChannel outByteCh, InputDataDTO data, String isLeafNode) {

    StringBuffer strBuffer = new StringBuffer();
    strBuffer.append(data.getRegion());//  ww  w. j a v  a2 s  .  c om
    strBuffer.append("|");
    strBuffer.append(data.getCategory());
    strBuffer.append("|");
    strBuffer.append(data.getWeekday());
    strBuffer.append("|");
    strBuffer.append(data.getHour());
    strBuffer.append("|");
    strBuffer.append(data.getPlatform());
    strBuffer.append("|");
    strBuffer.append(data.getCount());
    List<subDataDTO> subDataList = getSubDataList(data);

    // leafNode or not(Y/N)
    strBuffer.append("|");
    strBuffer.append(isLeafNode);

    strBuffer.append("\r\n");

    byte[] outByte = null;
    try {
        outByte = strBuffer.toString().getBytes("UTF-8");
    } catch (UnsupportedEncodingException e2) {
        e2.printStackTrace();
    }
    byteBuffer.put(outByte);
    byteBuffer.flip();
    try {
        outByteCh.write(byteBuffer);
    } catch (IOException e) {
    }
    byteBuffer.clear();

    return subDataList;
}

From source file:com.castis.sysComp.PoisConverterSysComp.java

public void parseRegionFile(File file) throws Exception {
    String line = "";
    FileInputStream in = null;/*from w w  w.jav a 2s .  c om*/
    Reader isReader = null;
    LineNumberReader bufReader = null;

    FileOutputStream fos = null;
    String fileName = file.getName();

    int index = fileName.indexOf("-");
    if (index != -1) {
        fileName = fileName.substring(index + 1, fileName.length());
    }

    String dir = filePolling.getValidFileDirectory(resultDir);

    String tempDir = dir + "/temp/";
    File targetDirectory = new File(CiFileUtil.getReplaceFullPath(tempDir));
    if (!targetDirectory.isDirectory()) {
        CiFileUtil.createDirectory(tempDir);
    }

    fos = new FileOutputStream(tempDir + fileName);

    int byteSize = 2048;
    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(byteSize);
    GatheringByteChannel outByteCh = fos.getChannel();

    try {
        in = new FileInputStream(file);
        isReader = new InputStreamReader(in, "UTF-16LE");
        bufReader = new LineNumberReader(isReader);

        boolean first = true;
        while ((line = bufReader.readLine()) != null) {

            byte[] utf8 = line.getBytes("UTF-8");
            String string = new String(utf8, "UTF-8");

            String data[] = string.split("\t");

            if (first == true) {
                first = false;

                if (data[0] == null || data[0].contains("region") == false) {
                    throw new DataParsingException("data parsing error(not formatted)");
                }
                continue;
            }

            if (data[0] == null || data[0].equals("")) {
                throw new DataParsingException("data parsing error(region id)");
            }
            if (data[1] == null || data[1].equals("")) {
                throw new DataParsingException("data parsing error(region name)");
            }
            if (data[2] == null || data[2].equals("")) {
                throw new DataParsingException("data parsing error(parent id)");
            }

            StringBuffer strBuffer = new StringBuffer();
            strBuffer.append(data[0]);
            strBuffer.append("\t");
            strBuffer.append(data[1]);
            strBuffer.append("\t");
            strBuffer.append(data[2]);

            strBuffer.append("\r\n");

            byte[] outByte = null;
            try {
                outByte = strBuffer.toString().getBytes("UTF-8");
            } catch (UnsupportedEncodingException e2) {
                e2.printStackTrace();
            }
            byteBuffer.put(outByte);
            byteBuffer.flip();
            try {
                outByteCh.write(byteBuffer);
            } catch (IOException e) {
            }
            byteBuffer.clear();
        }

        fos.close();

        index = fileName.indexOf("_");

        String targetDir = resultDir;
        File sourceFile = new File(tempDir + fileName);
        if (index != -1) {
            String directory = fileName.substring(0, index);
            targetDir += "/" + directory;
        }

        try {

            File resultTargetDir = new File(CiFileUtil.getReplaceFullPath(targetDir));
            if (!resultTargetDir.isDirectory()) {
                CiFileUtil.createDirectory(targetDir);
            }

            CiFileUtil.renameFile(sourceFile, targetDir, fileName);
        } catch (Exception e) {
            log.error(e.getMessage());
        }

    } catch (Exception e) {
        String errorMsg = "Fail to parsing Line.[current line(" + bufReader.getLineNumber() + ") :" + line
                + "] : ";
        log.error(errorMsg, e);
        throw new DataParsingException(errorMsg, e); //throw(e);

    } finally {
        if (in != null)
            in.close();
        if (isReader != null)
            isReader.close();
        if (bufReader != null)
            bufReader.close();
    }
}

From source file:com.castis.sysComp.PoisConverterSysComp.java

private void writeClientUIFile(List<sceneDTO> list, String platform, File file) throws FileNotFoundException {

    FileOutputStream fos = null;/*from  w ww . j ava 2 s .  c  o  m*/
    String dir = filePolling.getValidFileDirectory(resultDir);

    String fileName = file.getName();
    String tempDir = dir + "/temp/";
    File targetDirectory = new File(CiFileUtil.getReplaceFullPath(tempDir));
    if (!targetDirectory.isDirectory()) {
        CiFileUtil.createDirectory(tempDir);
    }

    fos = new FileOutputStream(tempDir + fileName);
    int byteSize = 2048;
    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(byteSize);
    GatheringByteChannel outByteCh = fos.getChannel();

    try {
        for (int i = 0; i < list.size(); i++) {
            sceneDTO scene = list.get(i);
            StringBuffer strBuffer = new StringBuffer();
            if (i == 0) {
                strBuffer.append("policy");
                strBuffer.append("|");
                strBuffer.append(platform);
                strBuffer.append("|");
                strBuffer.append("ClientUI");

                strBuffer.append("\r\n");
            }
            strBuffer.append("info");
            strBuffer.append("|");
            strBuffer.append(platform);
            strBuffer.append("|");
            strBuffer.append(scene.getId());
            strBuffer.append("|");
            strBuffer.append(scene.getName());
            strBuffer.append("|");
            strBuffer.append(scene.getTemplateFileName());
            strBuffer.append("|");
            strBuffer.append(scene.getMenuId());
            strBuffer.append("|");
            strBuffer.append(scene.getMenuName());
            strBuffer.append("|");
            strBuffer.append(scene.getSpaceId());
            strBuffer.append("|");
            strBuffer.append(scene.getSpaceName());
            strBuffer.append("|");
            strBuffer.append(scene.getResolution());
            strBuffer.append("|");
            strBuffer.append(scene.getResolutionOnFocus());
            strBuffer.append("|");
            strBuffer.append(scene.getSizeLimit());
            strBuffer.append("|");
            strBuffer.append(scene.getClickable());

            strBuffer.append("\r\n");

            byte[] outByte = null;
            try {
                outByte = strBuffer.toString().getBytes("UTF-8");
            } catch (UnsupportedEncodingException e2) {
                e2.printStackTrace();
            }
            byteBuffer.put(outByte);
            byteBuffer.flip();
            try {
                outByteCh.write(byteBuffer);
            } catch (IOException e) {
            }
            byteBuffer.clear();
        }

        fos.close();

        String targetDir = resultDir;
        File sourceFile = new File(tempDir + fileName);

        int index = fileName.indexOf("-");
        if (index != -1) {
            fileName = fileName.substring(index + 1, fileName.length());
        }
        index = fileName.indexOf("_");

        if (index != -1) {
            String directory = fileName.substring(0, index);
            targetDir += "/" + directory;
        }

        index = fileName.indexOf(".");
        if (index != -1) {
            fileName = fileName.substring(0, index) + ".csv";
        }

        try {

            File resultTargetDir = new File(CiFileUtil.getReplaceFullPath(targetDir));
            if (!resultTargetDir.isDirectory()) {
                CiFileUtil.createDirectory(targetDir);
            }

            CiFileUtil.renameFile(sourceFile, targetDir, fileName);
        } catch (Exception e) {
            log.error(e.getMessage());
        }

    } catch (Exception e) {
        String errorMsg = e.getMessage();
        log.error(errorMsg, e);
        throw new DataParsingException(errorMsg, e); //throw(e);

    }
}