Search in sources :

Example 31 with IFSFile

use of com.ibm.as400.access.IFSFile in project IBMiProgTool by vzupka.

the class DisplayFile method displaySourceMember.

/**
 * Display source member using its CCSID attribute; Only data part of the source record is translated (to String -
 * UTF-16).
 *
 * @param remoteServer
 * @param as400PathString
 */
protected void displaySourceMember(AS400 remoteServer, String as400PathString) {
    this.setTitle("Display member  '" + as400PathString + "'");
    IFSFile ifsFile = new IFSFile(remoteServer, as400PathString);
    // Create an AS400FileRecordDescription object that represents the file
    AS400FileRecordDescription inRecDesc = new AS400FileRecordDescription(remoteServer, as400PathString);
    try {
        // Decide what CCSID is appropriate for displaying the member
        int ccsidAttribute = ifsFile.getCCSID();
        characterSetLabel.setText("CCSID " + ccsidAttribute + " was used for display.");
        // Get list of record formats of the database file
        RecordFormat[] format = inRecDesc.retrieveRecordFormat();
        // Create an AS400File object that represents the file
        SequentialFile as400seqFile = new SequentialFile(remoteServer, as400PathString);
        // Set the record format (the only one)
        as400seqFile.setRecordFormat(format[0]);
        // Open the source physical file member as a sequential file
        as400seqFile.open(AS400File.READ_ONLY, 0, AS400File.COMMIT_LOCK_LEVEL_NONE);
        // Read the first source member record
        Record inRecord = as400seqFile.readNext();
        // --------------------
        while (inRecord != null) {
            StringBuilder textLine = new StringBuilder();
            // Source record is composed of three source record fields: seq.
            // number, date, source data.
            DecimalFormat df1 = new DecimalFormat("0000.00");
            DecimalFormat df2 = new DecimalFormat("000000");
            // Sequence number - 6 bytes
            String seq = df1.format((Number) inRecord.getField("SRCSEQ"));
            String seq2 = seq.substring(0, 4) + seq.substring(5);
            textLine.append(seq2);
            // Date - 6 bytes
            String srcDat = df2.format((Number) inRecord.getField("SRCDAT"));
            // textLine.append(srcDat);
            textLine.append(srcDat);
            // Data from source record (the source line)
            byte[] bytes = inRecord.getFieldAsBytes("SRCDTA");
            // Create object for conversion from bytes to characters
            // Ignore "IBM i CCSID" parameter - display characters in the
            // member.
            AS400Text textConverter = new AS400Text(bytes.length, remoteServer);
            // Convert byte array buffer to text line (String - UTF-16)
            String translatedData = (String) textConverter.toObject(bytes);
            // Append translated data to text line
            textLine.append(translatedData).append(NEW_LINE);
            // Append text line to text area
            textArea.append(textLine.toString());
            // Read next source member record
            inRecord = as400seqFile.readNext();
        }
        // Close the file
        as400seqFile.close();
        // Set scroll bar to top
        textArea.setCaretPosition(0);
        // Display the window.
        setVisible(true);
        row = "Info: Source member  " + as400PathString + "  has CCSID  " + ccsidAttribute + ".";
        mainWindow.msgVector.add(row);
        mainWindow.showMessages(nodes);
    } catch (Exception exc) {
        exc.printStackTrace();
        row = "Error: " + exc.toString();
        mainWindow.msgVector.add(row);
        mainWindow.showMessages(nodes);
    }
    // Remove message scroll listener (cancel scrolling to the last message)
    mainWindow.scrollMessagePane.getVerticalScrollBar().removeAdjustmentListener(mainWindow.messageScrollPaneAdjustmentListenerMax);
}
Also used : SequentialFile(com.ibm.as400.access.SequentialFile) RecordFormat(com.ibm.as400.access.RecordFormat) DecimalFormat(java.text.DecimalFormat) Point(java.awt.Point) PatternSyntaxException(java.util.regex.PatternSyntaxException) BadLocationException(javax.swing.text.BadLocationException) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) AS400FileRecordDescription(com.ibm.as400.access.AS400FileRecordDescription) AS400Text(com.ibm.as400.access.AS400Text) Record(com.ibm.as400.access.Record) IFSFile(com.ibm.as400.access.IFSFile)

Example 32 with IFSFile

use of com.ibm.as400.access.IFSFile in project IBMiProgTool by vzupka.

the class EditFile method displaySourceMember.

protected void displaySourceMember() {
    this.setTitle("Edit member  '" + filePathString + "'");
    // Extract individual names (libraryName, fileName, memberName) from the AS400 IFS path
    extractNamesFromIfsPath(filePathString);
    IFSFile ifsFile = new IFSFile(remoteServer, filePathString);
    // Create an AS400FileRecordDescription object that represents the file
    AS400FileRecordDescription inRecDesc = new AS400FileRecordDescription(remoteServer, filePathString);
    // Set editability
    textArea.setEditable(true);
    textArea.setText("");
    try {
        ccsidAttribute = ifsFile.getCCSID();
        characterSetLabel.setText("CCSID " + ccsidAttribute + " was used for display.");
        // Get list of record formats of the database file
        RecordFormat[] format = inRecDesc.retrieveRecordFormat();
        // Create an AS400File object that represents the file
        SequentialFile as400seqFile = new SequentialFile(remoteServer, filePathString);
        // Set the record format (the only one)
        as400seqFile.setRecordFormat(format[0]);
        // Open the source physical file member as a sequential file
        as400seqFile.open(AS400File.READ_ONLY, 0, AS400File.COMMIT_LOCK_LEVEL_NONE);
        // Read the first source member record
        Record inRecord = as400seqFile.readNext();
        // --------------------
        while (inRecord != null) {
            StringBuilder textLine = new StringBuilder();
            // Prefix is not displayed because it must not be edited!!!
            // Source record is composed of three source record fields: seq.
            // number, date, source data.
            // -- DecimalFormat df1 = new DecimalFormat("0000.00");
            // -- DecimalFormat df2 = new DecimalFormat("000000");
            // Sequence number - 6 bytes String seq = df1.format((Number)
            // --inRecord.getField("SRCSEQ"));
            // String seq2 = seq.substring(0, 4) + seq.substring(5);
            // Date - 6 bytes
            // --String srcDat = df2.format((Number)
            // inRecord.getField("SRCDAT"));
            // Data from source record (the source line)
            byte[] bytes = inRecord.getFieldAsBytes("SRCDTA");
            // Create object for conversion from bytes to characters
            // Ignore "IBM i CCSID" parameter - display characters in the
            // member.
            AS400Text textConverter = new AS400Text(bytes.length, remoteServer);
            // Convert byte array buffer to text line (String - UTF-16)
            String translatedData = (String) textConverter.toObject(bytes);
            // Append translated data to text line
            textLine.append(translatedData).append(NEW_LINE);
            // Append text line to text area
            textArea.append(textLine.toString());
            // Read next source member record
            inRecord = as400seqFile.readNext();
        }
        // Close the file
        as400seqFile.close();
    } catch (Exception exc) {
        isError = true;
        exc.printStackTrace();
        row = "Error in displaying source member: " + exc.toString();
        mainWindow.msgVector.add(row);
        mainWindow.showMessages(nodes);
    }
    // Remove message scroll listener (cancel scrolling to the last message)
    mainWindow.scrollMessagePane.getVerticalScrollBar().removeAdjustmentListener(mainWindow.messageScrollPaneAdjustmentListenerMax);
}
Also used : SequentialFile(com.ibm.as400.access.SequentialFile) AS400Text(com.ibm.as400.access.AS400Text) RecordFormat(com.ibm.as400.access.RecordFormat) Record(com.ibm.as400.access.Record) CannotUndoException(javax.swing.undo.CannotUndoException) CannotRedoException(javax.swing.undo.CannotRedoException) BadLocationException(javax.swing.text.BadLocationException) IFSFile(com.ibm.as400.access.IFSFile) AS400FileRecordDescription(com.ibm.as400.access.AS400FileRecordDescription)

Example 33 with IFSFile

use of com.ibm.as400.access.IFSFile in project IBMiProgTool by vzupka.

the class AddAS400Nodes method parallelAddAS400Nodes.

protected void parallelAddAS400Nodes() {
    libraryPattern = mainWindow.libraryPatternTextField.getText();
    filePattern = mainWindow.filePatternTextField.getText();
    memberPattern = mainWindow.memberPatternTextField.getText();
    libraryField = libraryPattern;
    fileField = filePattern;
    memberField = memberPattern;
    if (libraryField.isEmpty()) {
        libraryPattern = "*";
    }
    if (fileField.isEmpty()) {
        filePattern = "*";
    }
    if (memberField.isEmpty()) {
        memberPattern = "*";
    }
    libraryWildCard = libraryPattern.replace("*", ".*");
    libraryWildCard = libraryWildCard.replace("?", ".");
    fileWildCard = filePattern.replace("*", ".*");
    fileWildCard = fileWildCard.replace("?", ".");
    memberWildCard = memberPattern.replace("*", ".*");
    memberWildCard = memberWildCard.replace("?", ".");
    DefaultMutableTreeNode nodeLevel2 = null;
    DefaultMutableTreeNode nodeLevel3 = null;
    DefaultMutableTreeNode nodeLevel4 = null;
    DefaultMutableTreeNode nodeLevel5 = null;
    // Set wait-cursor (rotating wheel?)
    mainWindow.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
    mainWindow.scrollMessagePane.getVerticalScrollBar().addAdjustmentListener(mainWindow.messageScrollPaneAdjustmentListenerMax);
    // First, remove all children in order to create all new child nodes
    nodeParam.removeAllChildren();
    try {
        // IFSFile list of directories/files
        ifsFiles = ifsFileParam.listFiles();
        // For each directory add a new node - IFS directory or file
        for (IFSFile ifsFile : ifsFiles) {
            try {
                // Get parent file
                IFSFile parent = ifsFile.getParentFile();
                // IFS path does not start with /QSYS.LIB and it is not QSYS.LIB (without a leading slash)
                if (!ifsFile.toString().toUpperCase().startsWith("/QSYS.LIB") && !ifsFile.toString().toUpperCase().equals("QSYS.LIB")) {
                    // Level 1
                    nodeLevel2 = new DefaultMutableTreeNode(ifsFile.getName());
                    nodeParam.add(nodeLevel2);
                    // Level 2
                    IFSFile[] ifsFiles2;
                    nodeLevel2.removeAllChildren();
                    if (ifsFile.isDirectory()) {
                        try {
                            // Get list of sub-files/sub-directories.
                            // Some objects may be secured (e. g /QDLS directory).
                            ifsFiles2 = ifsFile.listFiles();
                            for (IFSFile ifsFileLevel2 : ifsFiles2) {
                                nodeLevel3 = new DefaultMutableTreeNode(ifsFileLevel2.getName());
                                nodeLevel2.add(nodeLevel3);
                                // // -----------------------------
                                if (false) {
                                    // Level 3
                                    IFSFile[] ifsFiles3;
                                    nodeLevel3.removeAllChildren();
                                    // Get list of sub-files/sub-directories.
                                    if (ifsFileLevel2.isDirectory()) {
                                        ifsFiles3 = ifsFileLevel2.listFiles();
                                        for (IFSFile ifsFileLevel3 : ifsFiles3) {
                                            nodeLevel4 = new DefaultMutableTreeNode(ifsFileLevel3.getName());
                                            nodeLevel3.add(nodeLevel4);
                                            // Level 4
                                            IFSFile[] ifsFiles4;
                                            nodeLevel4.removeAllChildren();
                                            if (ifsFileLevel3.isDirectory()) {
                                                ifsFiles4 = ifsFileLevel3.listFiles();
                                                for (IFSFile ifsFileLevel4 : ifsFiles4) {
                                                    nodeLevel5 = new DefaultMutableTreeNode(ifsFileLevel4.getName());
                                                    nodeLevel4.add(nodeLevel5);
                                                }
                                            }
                                        }
                                    }
                                // // ----------------
                                // // Other levels end
                                }
                            }
                        } catch (Exception exc) {
                            System.out.println(exc.getLocalizedMessage());
                            exc.printStackTrace();
                            mainWindow.row = "Info: Object  " + ifsFile.toString() + "  -  " + exc.toString();
                            mainWindow.msgVector.add(mainWindow.row);
                            mainWindow.showMessages(mainWindow.noNodes);
                            continue;
                        }
                    }
                }
                // starting with a prefix and add them as secondary nodes
                if (ifsFile.toString().toUpperCase().equals("/QSYS.LIB")) {
                    // Add QSYS library
                    nodeLevel2 = new DefaultMutableTreeNode(ifsFile.getName());
                    nodeParam.add(nodeLevel2);
                    nodeLevel2.removeAllChildren();
                    // Get list of libraries - Level 2
                    // Select all libraries conforming to the library pattern
                    IFSFile[] ifsFiles2 = ifsFile.listFiles(libraryPattern + ".LIB");
                    for (IFSFile ifsFileLevel2 : ifsFiles2) {
                        nodeLevel3 = new DefaultMutableTreeNode(ifsFileLevel2.getName());
                        nodeLevel2.add(nodeLevel3);
                    }
                } else // Secondary nodes of Source Physical Files (level3) are added.
                if (ifsFile.toString().toUpperCase().startsWith("/QSYS.LIB/") && ifsFile.toString().toUpperCase().endsWith(".LIB")) {
                    libraryName = ifsFile.getName();
                    String bareLibraryName = libraryName.substring(0, ifsFile.getName().lastIndexOf("."));
                    // Select libraries on file pattern and member pattern
                    if (bareLibraryName.matches(libraryWildCard)) {
                        // Select Source Physical Files depending on member pattern
                        files = ifsFile.listFiles(filePattern + ".FILE");
                        // Add empty library
                        if (files.length == 0) {
                            mainWindow.row = "Info: Library 0  " + libraryName + "  added.";
                            mainWindow.msgVector.add(mainWindow.row);
                            mainWindow.showMessages(mainWindow.noNodes);
                            nodeLevel2 = new DefaultMutableTreeNode(libraryName);
                            nodeParam.add(nodeLevel2);
                        }
                        // Note Library not yet added
                        boolean libraryAdded = false;
                        labelFile: for (IFSFile file : files) {
                            fileName = file.getName();
                            // Member pattern is all inclusive
                            if (memberPattern.equals("*")) {
                                // If library not yet added - add it
                                if (!libraryAdded) {
                                    mainWindow.row = "Info: Library 1  " + libraryName + "  added.";
                                    mainWindow.msgVector.add(mainWindow.row);
                                    mainWindow.showMessages(mainWindow.noNodes);
                                    nodeLevel2 = new DefaultMutableTreeNode(libraryName);
                                    nodeParam.add(nodeLevel2);
                                    nodeLevel2.removeAllChildren();
                                    // Note library added for next loop
                                    libraryAdded = true;
                                }
                                // Add the file
                                // if (file.isSourcePhysicalFile()) {
                                mainWindow.row = "Info: File 1  " + file.toString() + "  added.";
                                mainWindow.msgVector.add(mainWindow.row);
                                mainWindow.showMessages(mainWindow.noNodes);
                                nodeLevel3 = new DefaultMutableTreeNode(fileName);
                                nodeLevel2.add(nodeLevel3);
                            // }
                            } else {
                                // Member pattern is specific
                                if (file.isSourcePhysicalFile()) {
                                    members = file.listFiles(memberPattern + ".MBR");
                                    for (IFSFile member : members) {
                                        String bareMemberName = member.getName().substring(0, member.getName().lastIndexOf("."));
                                        if (bareMemberName.matches(memberWildCard)) {
                                            if (!libraryAdded) {
                                                // If the library was not added - add it
                                                mainWindow.row = "Info: Library 2  " + libraryName + "  added.";
                                                mainWindow.msgVector.add(mainWindow.row);
                                                mainWindow.showMessages(mainWindow.noNodes);
                                                nodeLevel2 = new DefaultMutableTreeNode(libraryName);
                                                nodeParam.add(nodeLevel2);
                                                nodeLevel2.removeAllChildren();
                                                // Note library added for next loop
                                                libraryAdded = true;
                                            }
                                            // Add the Source Physical File
                                            // if (file.isSourcePhysicalFile()) {
                                            mainWindow.row = "Info: File 2  " + file.toString() + "  added.";
                                            mainWindow.msgVector.add(mainWindow.row);
                                            mainWindow.showMessages(mainWindow.noNodes);
                                            nodeLevel3 = new DefaultMutableTreeNode(fileName);
                                            nodeLevel2.add(nodeLevel3);
                                            // Get next file to check
                                            continue labelFile;
                                        // }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                // ---------------------
                if (ifsFile.toString().contains(".LIB") && ifsFile.toString().contains(".FILE") && ifsFile.isSourcePhysicalFile()) {
                    fileName = ifsFile.getName();
                    String bareFileName = fileName.substring(0, ifsFile.getName().lastIndexOf("."));
                    if (bareFileName.matches(fileWildCard)) {
                        // Add the Source Physical File
                        nodeLevel2 = new DefaultMutableTreeNode(fileName);
                        nodeParam.add(nodeLevel2);
                        nodeLevel2.removeAllChildren();
                        // Select members
                        IFSFile[] members = ifsFile.listFiles(memberPattern + ".MBR");
                        for (IFSFile member : members) {
                            // Select the member
                            nodeLevel3 = new DefaultMutableTreeNode(member.getName());
                            nodeLevel2.add(nodeLevel3);
                        }
                    }
                }
                // --------------
                if (ifsFile.toString().contains(".LIB") && ifsFile.toString().contains(".FILE") && parent.isSourcePhysicalFile() && ifsFile.toString().endsWith(".MBR")) {
                    String bareMemberName = ifsFile.getName().substring(0, ifsFile.getName().lastIndexOf("."));
                    if (bareMemberName.matches(memberWildCard)) {
                        // Add the member
                        nodeLevel2 = new DefaultMutableTreeNode(ifsFile.getName());
                        nodeParam.add(nodeLevel2);
                    }
                }
                // ----------
                if (ifsFile.toString().endsWith(".FILE") && ifsFile.getSubtype().equals("SAVF")) {
                    fileName = ifsFile.getName();
                    String bareFileName = fileName.substring(0, fileName.lastIndexOf("."));
                    // Save file has suffix .SAVF in the visible tree structure:
                    String saveFileName = bareFileName + ".SAVF";
                    mainWindow.row = "Info: Save file  " + saveFileName + " added.";
                    mainWindow.msgVector.add(mainWindow.row);
                    mainWindow.showMessages(mainWindow.noNodes);
                    nodeLevel2 = new DefaultMutableTreeNode(saveFileName);
                    nodeParam.add(nodeLevel2);
                }
                // -------------
                if (ifsFile.toString().contains(".LIB") && ifsFile.toString().contains(".OUTQ")) {
                    nodeLevel2 = new DefaultMutableTreeNode(ifsFile.getName());
                    nodeParam.add(nodeLevel2);
                }
            } catch (Exception exc) {
                exc.printStackTrace();
                mainWindow.row = "Info: Object  " + ifsFile.toString() + "  -  " + exc.toString();
                mainWindow.msgVector.add(mainWindow.row);
                mainWindow.showMessages(mainWindow.noNodes);
            }
        }
    } catch (Exception exc) {
        exc.printStackTrace();
        mainWindow.row = "Info: Object  " + ifsFile.toString() + "  -  " + exc.toString();
        mainWindow.msgVector.add(mainWindow.row);
        mainWindow.showMessages(mainWindow.noNodes);
    }
    // Change cursor to default
    // mainWindow.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
    // Remove setting last element of messages
    // mainWindow.scrollMessagePane.getVerticalScrollBar()
    // .removeAdjustmentListener(mainWindow.messageScrollPaneAdjustmentListenerMax);
    expandNode();
    refreshWindow();
}
Also used : DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) IFSFile(com.ibm.as400.access.IFSFile)

Example 34 with IFSFile

use of com.ibm.as400.access.IFSFile in project IBMiProgTool by vzupka.

the class Copy_IBMi_PC method copyingToPC.

/**
 * Initial method calling further methods for copying from IBM i to PC
 *
 * @param sourcePathString
 * @param targetPathString
 */
protected void copyingToPC(String sourcePathString, String targetPathString) {
    if (remoteServer == null) {
        return;
    }
    IFSFile ifsDirFile = new IFSFile(remoteServer, sourcePathString);
    // Path prefix is the leading part of the path up to and including the last slash:
    // e.g.  /home/vzupka/ILESRC  ->  /home/vzupka/
    ifsPathStringPrefix = sourcePathString.substring(0, sourcePathString.indexOf(ifsDirFile.getName()));
    if (Files.isDirectory(Paths.get(targetPathString))) {
        // PC directory
        copyToPcDirectory(sourcePathString, targetPathString, ifsPathStringPrefix);
    } else {
        // 
        // Simple PC file
        copyToPcFile(sourcePathString, targetPathString, notFromWalk);
    }
    // Remove message scroll listener (cancel scrolling to the last message)
    mainWindow.scrollMessagePane.getVerticalScrollBar().removeAdjustmentListener(mainWindow.messageScrollPaneAdjustmentListenerMax);
}
Also used : IFSFile(com.ibm.as400.access.IFSFile)

Example 35 with IFSFile

use of com.ibm.as400.access.IFSFile in project IBMiProgTool by vzupka.

the class Copy_IBMi_PC method copyToPcFile.

/**
 * Copying IBM i IFS file or Source member or Save file to PC file;
 * If the PC file does not exist, one is created.
 *
 * @param pcPathString
 * @param as400PathString
 * @param fromWalk
 * @return
 */
protected String copyToPcFile(String as400PathString, String pcPathString, boolean fromWalk) {
    // Path to PC file
    Path pcFilePath = Paths.get(pcPathString);
    IFSFile ifsDirFile = new IFSFile(remoteServer, as400PathString);
    try {
        // Source physical file is a directory and cannot be copied to PC file
        if (ifsDirFile.isSourcePhysicalFile()) {
            row = "Error: Source physical file  " + as400PathString + "  cannot be copied to file  " + pcPathString + ".";
            mainWindow.msgVector.add(row);
            mainWindow.showMessages(noNodes);
            return "ERROR";
        }
        // IFS directory cannot be copied to PC file
        if (ifsDirFile.isDirectory()) {
            row = "Error: IFS directory  " + as400PathString + "  cannot be copied to PC file  " + pcPathString + ".";
            mainWindow.msgVector.add(row);
            mainWindow.showMessages(noNodes);
            return "ERROR";
        }
        // Source physical file MEMBER:
        if (sourcePathString.startsWith("/QSYS.LIB/")) {
            extractNamesFromIfsPath(as400PathString);
            if (sourcePathString.endsWith(".MBR")) {
                // 
                if (pcPathString.endsWith(".savf")) {
                    row = "Error: Source member  " + libraryName + "/" + fileName + "(" + memberName + ")  cannot be copied to PC file  " + pcPathString + "  ending with .savf.";
                    mainWindow.msgVector.add(row);
                    mainWindow.showMessages(noNodes);
                    return "ERROR";
                }
                // 
                // Member to PC file
                // ------
                msgText = copyFromSourceMember(remoteServer, sourcePathString, pcPathString);
                if (msgText.isEmpty()) {
                    row = "Comp: Source member  " + libraryName + "/" + fileName + "(" + memberName + ")  was copied to PC file  " + pcPathString + " using charset " + pcCharset + ".";
                    mainWindow.msgVector.add(row);
                    mainWindow.showMessages(nodes);
                    return "";
                } else {
                    row = "Comp File: Source member  " + libraryName + "/" + fileName + "(" + memberName + ")  was NOT copied to PC file  " + pcPathString + ".";
                    mainWindow.msgVector.add(row);
                    mainWindow.showMessages(noNodes);
                    return "ERROR";
                }
            } else // ---------
            if (ifsDirFile.toString().contains(".LIB") && ifsDirFile.toString().endsWith(".SAVF") && ifsDirFile.getSubtype().equals("SAVF")) {
                msgText = copyFromSaveFile(remoteServer, sourcePathString, pcPathString);
                return msgText;
            }
        } else {
            // From IFS stream file to PC file (no directories are involved)
            try {
                byte[] byteArray = new byte[2000000];
                int bytesRead;
                // ---------------------------------
                if (Files.exists(pcFilePath) && !properties.getProperty("OVERWRITE_FILE").equals("Y")) {
                    row = "Error: IFS file  " + ifsDirFile + "  was NOT copied to the existing file  " + pcPathString + ". Overwriting files is not allowed.";
                    mainWindow.msgVector.add(row);
                    mainWindow.showMessages(noNodes);
                    return "ERROR";
                }
                // ------------------------------------
                if (Files.notExists(pcFilePath)) {
                    Files.createFile(pcFilePath);
                }
                // IFS file with suffix .savf cannot be copied to PC file with different suffix
                if (sourcePathString.endsWith(".savf") && !pcPathString.endsWith(".savf")) {
                    row = "Error: IFS file  " + ifsDirFile + "  ending with suffix \".savf\" cannot be copied to the existing file  " + pcPathString + "  with a different suffix.";
                    mainWindow.msgVector.add(row);
                    mainWindow.showMessages(noNodes);
                    return "ERROR";
                }
                // Copy "save" file from IFS to PC file
                if (sourcePathString.endsWith(".savf") && pcPathString.endsWith(".savf")) {
                    // Copy the PC file to Save file using FTP (File Transfer Protocol)
                    AS400FTP ftp = new AS400FTP(remoteServer);
                    try {
                        // FTP Binary data transfer
                        // ftp.setDataTransferType(AS400FTP.BINARY); // not necessary when suffix is .savf
                        // FTP Get command
                        ftp.get(sourcePathString, pcPathString);
                        ftp.disconnect();
                        row = "Comp: IFS save file  " + sourcePathString + "  was copied to PC  save file  " + pcPathString + ".";
                        mainWindow.msgVector.add(row);
                        mainWindow.showMessages(noNodes);
                        return "";
                    } catch (Exception exc) {
                        exc.printStackTrace();
                        row = "Error: Copying IFS save file  " + sourcePathString + "  to PC  save file  " + pcPathString + "  failed:  " + exc.toString();
                        mainWindow.msgVector.add(row);
                        mainWindow.showMessages(noNodes);
                        return "ERROR";
                    }
                }
                // 
                if (pcCharset.equals("*DEFAULT") && ibmCcsid.equals("*DEFAULT")) {
                    // Open input IFS file
                    IFSFileInputStream ifsInStream = new IFSFileInputStream(remoteServer, as400PathString);
                    // Open the output PC file as buffered output stream
                    OutputStream os = Files.newOutputStream(pcFilePath, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING);
                    BufferedOutputStream bos = new BufferedOutputStream(os);
                    // Copy IFS file to PC file reading input stream to byte array and using byte buffer for output
                    // Read first portion of bytes
                    bytesRead = ifsInStream.read(byteArray);
                    // Repeat if at least one byte was read
                    while (bytesRead > 0) {
                        // Write out bytes read before
                        bos.write(byteArray, 0, bytesRead);
                        // Read next portion of bytes
                        bytesRead = ifsInStream.read(byteArray);
                    }
                    // Close files
                    bos.close();
                    ifsInStream.close();
                    if (fromWalk) {
                        row = "Info: IFS file  " + as400PathString + "  was copied unchanged (binary) to PC file  " + pcPathString + ".";
                        mainWindow.msgVector.add(row);
                        mainWindow.showMessages(noNodes);
                    } else {
                        row = "Comp: IFS file  " + as400PathString + "  was copied unchanged (binary) to PC file  " + pcPathString + ".";
                        mainWindow.msgVector.add(row);
                        mainWindow.showMessages(noNodes);
                    }
                // 
                // Data conversion is done
                // -----------------------
                } else {
                    // 
                    IFSFile ifsFile = new IFSFile(remoteServer, as400PathString);
                    if (ibmCcsid.equals("*DEFAULT")) {
                        // CCSID attribute of the input file
                        ibmCcsidInt = ifsFile.getCCSID();
                    }
                    // Open input IFS file
                    IFSFileInputStream ifsInStream = new IFSFileInputStream(remoteServer, as400PathString);
                    // Open output text file
                    if (pcCharset.equals("*DEFAULT")) {
                        pcCharset = "ISO-8859-1";
                    }
                    BufferedWriter outfileText = Files.newBufferedWriter(pcFilePath, Charset.forName(pcCharset), StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING);
                    // Copy IFS file to PC file reading input stream to byte array and using byte buffer for output
                    // Read first portion of bytes
                    bytesRead = ifsInStream.read(byteArray);
                    // Repeat if at least one byte was read
                    while (bytesRead > 0) {
                        // Convert input file data using "IBM i CCSID" parameter
                        AS400Text textConverter = new AS400Text(bytesRead, ibmCcsidInt, remoteServer);
                        // Convert byte array buffer to translated data text
                        String str = (String) textConverter.toObject(byteArray);
                        String translatedData = new String(str.getBytes(pcCharset), pcCharset);
                        // Write translated data to the text file
                        outfileText.write(translatedData);
                        // Read next byte array
                        bytesRead = ifsInStream.read(byteArray);
                    }
                    // Close files
                    outfileText.close();
                    ifsInStream.close();
                    if (fromWalk) {
                        row = "Info: IFS file  " + as400PathString + "  was copied to PC file  " + pcPathString + ",  " + ibmCcsid + " -> " + pcCharset + ".";
                        mainWindow.msgVector.add(row);
                        mainWindow.showMessages(noNodes);
                    } else {
                        row = "Comp: IFS file  " + as400PathString + "  was copied to PC file  " + pcPathString + ",  " + ibmCcsid + " -> " + pcCharset + ".";
                        mainWindow.msgVector.add(row);
                        mainWindow.showMessages(noNodes);
                    }
                }
                return "";
            } catch (Exception exc) {
                exc.printStackTrace();
                row = "Error: Copying to PC file " + pcPathString + "  failed.  -  " + exc.toString();
                mainWindow.msgVector.add(row);
                mainWindow.showMessages(noNodes);
                return "ERROR";
            }
        }
    } catch (Exception exc) {
        exc.printStackTrace();
        row = "Error: Copying to PC file " + pcPathString + "  -  " + exc.toString();
        mainWindow.msgVector.add(row);
        mainWindow.showMessages(noNodes);
        return "ERROR";
    }
    return "";
}
Also used : Path(java.nio.file.Path) AS400Text(com.ibm.as400.access.AS400Text) OutputStream(java.io.OutputStream) BufferedOutputStream(java.io.BufferedOutputStream) AS400FTP(com.ibm.as400.access.AS400FTP) BufferedOutputStream(java.io.BufferedOutputStream) IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) IFSFile(com.ibm.as400.access.IFSFile) IFSFileInputStream(com.ibm.as400.access.IFSFileInputStream) BufferedWriter(java.io.BufferedWriter)

Aggregations

IFSFile (com.ibm.as400.access.IFSFile)44 IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)14 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)14 AS400Text (com.ibm.as400.access.AS400Text)11 CommandCall (com.ibm.as400.access.CommandCall)9 AS400Message (com.ibm.as400.access.AS400Message)8 Path (java.nio.file.Path)7 AS400FileRecordDescription (com.ibm.as400.access.AS400FileRecordDescription)5 IFSFileInputStream (com.ibm.as400.access.IFSFileInputStream)5 Record (com.ibm.as400.access.Record)5 RecordFormat (com.ibm.as400.access.RecordFormat)5 SequentialFile (com.ibm.as400.access.SequentialFile)5 BadLocationException (javax.swing.text.BadLocationException)5 Point (java.awt.Point)4 PatternSyntaxException (java.util.regex.PatternSyntaxException)4 IFSFileOutputStream (com.ibm.as400.access.IFSFileOutputStream)3 DecimalFormat (java.text.DecimalFormat)3 CannotRedoException (javax.swing.undo.CannotRedoException)3 CannotUndoException (javax.swing.undo.CannotUndoException)3 BufferedWriter (java.io.BufferedWriter)2