use of com.ibm.as400.access.IFSFileOutputStream in project IBMiProgTool by vzupka.
the class Copy_PC_IBMi method copyFromPcFile.
/**
* Copying simple PC file to IBMi IFS directory/file or to Source File/Source Member or to Save File
*
* @param sourcePathString
* @param targetPathString
* @param fromWalk
* @return
*/
protected String copyFromPcFile(String sourcePathString, String targetPathString, boolean fromWalk) {
if (!sourcePathString.contains(pcFileSep + ".")) {
try {
// Extract individual names (libraryName, fileName, memberName) from the AS400 IFS path
extractNamesFromIfsPath(targetPathString);
// Path to PC file
Path pcFilePath = Paths.get(sourcePathString);
// IFS file or directory object
IFSFile targetPath = new IFSFile(remoteServer, targetPathString);
//
if (!targetPathString.startsWith("/QSYS.LIB")) {
String outFileName;
if (targetPath.isDirectory()) {
//
// to IFS directory:
// IFS file name = IFS directory name + PC file name
outFileName = targetPathString + "/" + pcFilePath.getFileName();
if (outFileName.endsWith(".savf")) {
copyToSaveFile(sourcePathString, outFileName, notToLibrary);
return "";
}
} else {
//
// to IFS file:
// IFS file name does not change
outFileName = targetPathString;
if (outFileName.endsWith(".savf")) {
copyToSaveFile(sourcePathString, outFileName, notToLibrary);
return "";
}
// If input PC file ends with .savf, output IFS file must also end with .savf
if (sourcePathString.endsWith(".savf") && !outFileName.endsWith(".savf")) {
row = "Error: PC file " + sourcePathString + " ending with suffix \".savf\" cannot be copied to IFS file " + outFileName + " with a different suffix.";
mainWindow.msgVector.add(row);
mainWindow.showMessages();
return "ERROR";
}
}
// Create IFS file object
IFSFile outFilePath = new IFSFile(remoteServer, outFileName);
if (outFilePath.exists() && !properties.getProperty("OVERWRITE_FILE").equals("Y")) {
row = "Error: PC file " + sourcePathString + " was NOT copied to the existing file " + outFileName + ". Overwriting files is not allowed.";
mainWindow.msgVector.add(row);
mainWindow.showMessages();
return "ERROR";
}
// Default CCSID for IFS files is 819
if (ibmCcsid.equals("*DEFAULT")) {
ibmCcsid = "819";
ibmCcsidInt = 819;
}
// no conversion id necessary and transfer is faster.
if (pcCharset.toUpperCase().equals("UTF-8") && ibmCcsid.equals("1208") || pcCharset.toUpperCase().equals("UTF-16") && ibmCcsid.equals("1200") || pcCharset.toUpperCase().equals("UTF-16") && ibmCcsid.equals("13488") || // ASCII Windows
pcCharset.toUpperCase().equals("WINDOWS-1250") && ibmCcsid.equals("1250") || // ASCII Windows
pcCharset.toUpperCase().equals("WINDOWS-1251") && ibmCcsid.equals("1251") || // ASCII Windows
pcCharset.toUpperCase().equals("CP1250") && ibmCcsid.equals("1250") || // ASCII Windows
pcCharset.toUpperCase().equals("CP1251") && ibmCcsid.equals("1251") || // ASCII Latin-1
pcCharset.toUpperCase().equals("ISO-8859-1") && ibmCcsid.equals("819") || // ASCII Latin-1
pcCharset.toUpperCase().equals("ISO-8859-1") && ibmCcsid.equals("858") || // EBCDIC Latin-1
pcCharset.toUpperCase().equals("IBM500") && ibmCcsid.equals("500") || // EBCDIC Latin-1
pcCharset.toUpperCase().equals("CP500") && ibmCcsid.equals("500") || // ASCII Latin-2
pcCharset.toUpperCase().equals("ISO-8859-2") && ibmCcsid.equals("912") || // EBCDIC Latin-2
pcCharset.toUpperCase().equals("IBM870") && ibmCcsid.equals("870")) {
// Allocate buffer for data
ByteBuffer byteBuffer = ByteBuffer.allocate(2000000);
// Open output IFS file - SHARED for all users, REWRITE data
//
IFSFileOutputStream ifsOutStream = new IFSFileOutputStream(remoteServer, outFileName, IFSFileOutputStream.SHARE_ALL, false);
// Force this CCSID as an attribute to the output IFS file
outFilePath.setCCSID(ibmCcsidInt);
// Open the input PC file
FileChannel fileChannel = (FileChannel) Files.newByteChannel(pcFilePath);
// Copy PC file to IFS file with byte buffer
int bytesRead = fileChannel.read(byteBuffer);
while (bytesRead > 0) {
for (int idx = 0; idx < bytesRead; idx++) {
// New line byte must be changed for EBCDIC
if (byteBuffer.get(idx) == 0x15) {
byteBuffer.put(idx, (byte) 0x25);
}
}
ifsOutStream.write(byteBuffer.array(), 0, bytesRead);
// Set start of buffer to read next bytes into
byteBuffer.rewind();
bytesRead = fileChannel.read(byteBuffer);
}
// Close files
ifsOutStream.close();
fileChannel.close();
if (fromWalk) {
row = "Info: PC file " + sourcePathString + " was copied unchanged (binary) to IFS file " + outFileName + ", CCSID " + ibmCcsid + ".";
} else {
row = "Comp: PC file " + sourcePathString + " was copied unchanged (binary) to IFS file " + outFileName + ", CCSID " + ibmCcsid + ".";
}
mainWindow.msgVector.add(row);
mainWindow.showMessages();
} else {
//
// Conversion from pcCharset to ibmCcsid
// -------------------------------------
//
byte[] byteArray = new byte[2000000];
// Open input
BufferedReader bufferedReader;
if (pcCharset.equals("*DEFAULT")) {
pcCharset = "ISO-8859-1";
}
// Input will be decoded using PC charset parameter.
bufferedReader = Files.newBufferedReader(pcFilePath, Charset.forName(pcCharset));
// }
// Open output
IFSFileOutputStream ifsOutStream = new IFSFileOutputStream(remoteServer, outFileName, IFSFileOutputStream.SHARE_ALL, false, ibmCcsidInt);
// Force the CCSID from application parameter to the IFS file as an attribute
outFilePath.setCCSID(ibmCcsidInt);
// Copy data
int nbrOfBytes = 0;
String textLine = bufferedReader.readLine();
while (textLine != null) {
textLine += "\r\n";
// Decide how long in bytes the line is given target encoding.
if (ibmCcsid.equals("1200") || ibmCcsid.equals("13488")) {
// Get length in bytes for conversion to Unicode 1200 (UTF-16) and 13488 (UCS-2)
nbrOfBytes = textLine.length() * 2;
} else if (ibmCcsid.equals("1208")) {
// Get length in bytes
// for UTF-8 -> 1208
// and for single byte CCSIDs.
nbrOfBytes = textLine.getBytes().length;
} else {
// Get length of bytes of the text line for single byte characters
nbrOfBytes = textLine.length();
}
// Create text converter with correct length in bytes
AS400Text textConverter = new AS400Text(nbrOfBytes, ibmCcsidInt, remoteServer);
try {
byteArray = textConverter.toBytes(textLine);
} catch (Exception exc) {
exc.printStackTrace();
row = "Error: 1 Copying PC text file " + sourcePathString + " to IFS file " + targetPathString + ". Convert " + pcCharset + " -> " + ibmCcsid + ". - " + exc.toString();
mainWindow.msgVector.add(row);
mainWindow.showMessages();
return "ERROR";
}
ifsOutStream.write(byteArray);
// Read next line
textLine = bufferedReader.readLine();
}
bufferedReader.close();
ifsOutStream.close();
if (fromWalk) {
row = "Info: PC file " + sourcePathString + " was copied to IFS file " + outFileName + ", Convert " + pcCharset + " -> " + ibmCcsid + ".";
} else {
row = "Comp: PC file " + sourcePathString + " was copied to IFS file " + outFileName + ", Convert " + pcCharset + " -> " + ibmCcsid + ".";
}
mainWindow.msgVector.add(row);
mainWindow.showMessages();
}
return "";
} else // --------------------------------------------------------
if (targetPathString.endsWith(".LIB")) {
// Default CCSID for Library objects is 500 (EBCDIC Latin-1)
if (ibmCcsid.equals("*DEFAULT")) {
ibmCcsid = "500";
ibmCcsidInt = 500;
}
// PC file with suffix .savf to LIBRARY
if (pcFilePath.getFileName().toString().endsWith(".savf")) {
msgText = copyToSaveFile(sourcePathString, targetPathString, toLibrary);
} else //
//
// PC file without .savf suffix is NOT ALLOWED to copy to LIBRARY!
{
row = "Error: PC file " + sourcePathString + " without .savf suffix cannot be copied to the library " + libraryName + ".";
mainWindow.msgVector.add(row);
mainWindow.showMessages();
}
// PC file to SAVE FILE
} else if (targetPathString.contains(".LIB") && targetPathString.endsWith(".SAVF")) // && targetPath.getSubtype().equals("SAVF")
{
msgText = copyToSaveFile(sourcePathString, targetPathString, notToLibrary);
if (!msgText.isEmpty()) {
row = "Comp: PC file " + sourcePathString + " was NOT copied to the save file " + libraryName + "/" + fileName + ".";
mainWindow.msgVector.add(row);
mainWindow.showMessages();
}
} else if (targetPathString.endsWith(".FILE")) {
// File ending with .savf is not allowed to source member
if (sourcePathString.endsWith(".savf")) {
row = "Error: PC file " + sourcePathString + " ending with .savf cannot be copied to source file " + libraryName + "/" + fileName + ".";
mainWindow.msgVector.add(row);
mainWindow.showMessages();
return "ERROR";
}
// Insert to source file as a member
msgText = copyToSourceFile(sourcePathString, targetPathString);
if (!msgText.isEmpty()) {
// + libraryName + "/" + fileName + "."
row = "Comp: PC file " + sourcePathString + " was NOT copied to the existing source physical file ";
mainWindow.msgVector.add(row);
mainWindow.showMessages();
}
} else if (targetPathString.endsWith(".MBR")) {
//
if (Files.isDirectory(pcFilePath)) {
// PC file to SOURCE MEMBER
msgText = copyToSourceMember(sourcePathString, targetPathString, fromDirectory);
} else {
// File ending with .savf is not allowed to source member
if (sourcePathString.endsWith(".savf")) {
row = "Error: PC file " + sourcePathString + " ending with .savf cannot be copied to source member " + libraryName + "/" + fileName + "/" + memberName + ".";
mainWindow.msgVector.add(row);
mainWindow.showMessages();
return "ERROR";
}
// Rewrite source member
msgText = copyToSourceMember(sourcePathString, targetPathString, notFromDirectory);
}
if (!msgText.isEmpty()) {
row = "Comp: PC file " + sourcePathString + " was NOT copied to the existing source physical member " + libraryName + "/" + fileName + "(" + memberName + ").";
mainWindow.msgVector.add(row);
mainWindow.showMessages();
}
}
} catch (Exception exc) {
exc.printStackTrace();
row = "Error: 2 Copying PC file " + sourcePathString + " to IFS file " + targetPathString + ". Convert " + pcCharset + " -> " + ibmCcsid + ". - " + exc.toString();
mainWindow.msgVector.add(row);
mainWindow.showMessages();
return "ERROR";
}
}
return msgText;
}
use of com.ibm.as400.access.IFSFileOutputStream in project IBMiProgTool by vzupka.
the class EditFile method rewriteSourceMember.
/**
* Rewrite source member with edited text area using an intermediate temporary IFS file; This method is fast enough.
*
* @return
*/
protected String rewriteSourceMember() {
// Extract individual names (libraryName, fileName, memberName) from the AS400 IFS path
extractNamesFromIfsPath(filePathString);
// Path to the output source member
String outMemberPathString = "/QSYS.LIB/" + libraryName + ".LIB/" + fileName + ".FILE" + "/" + memberName + ".MBR";
// Enable calling CL commands
CommandCall cmdCall = new CommandCall(remoteServer);
IFSFileOutputStream outStream = null;
try {
// ------------------------------------
if (!properties.getProperty("OVERWRITE_FILE").equals("Y")) {
row = "Info: Member " + libraryName + "/" + fileName + "(" + memberName + ") cannot be overwtitten. " + " Overwriting files is not allowed.";
mainWindow.msgVector.add(row);
mainWindow.showMessages();
return "ERROR";
}
// Overwrite is allowed
// --------------------
// First create an IFS '/home/userName directory if it does not exist
String home_userName = "/home/" + userName;
IFSFile ifsDir = new IFSFile(remoteServer, home_userName);
// Create new directory if it does not exist
ifsDir.mkdir();
// Create hidden temporary file (with leading dot) in the directory
String tmpFileString = home_userName + "/.tmp" + Timestamp.valueOf(LocalDateTime.now()).toString();
IFSFile ifsTmpFile = new IFSFile(remoteServer, tmpFileString);
IFSFile ifsTmpFilePath = new IFSFile(remoteServer, tmpFileString);
ifsTmpFile.createNewFile();
// Force the memeber CCSID to the IFS file as an attribute
ifsTmpFilePath.setCCSID(ibmCcsidInt);
// Copy edited text area to the temporary IFS file
outStream = new IFSFileOutputStream(remoteServer, tmpFileString);
String textAreaString = textArea.getText();
byte[] byteArray;
AS400Text textConverter = new AS400Text(textAreaString.length(), ibmCcsidInt);
byteArray = textConverter.toBytes(textAreaString);
// Write text from the text area to the file
outStream.write(byteArray);
// Close file
outStream.close();
// Copy data from temporary IFS file to the member. If the member does not exist it is created.
String commandCpyFrmStmfString = "CPYFRMSTMF FROMSTMF('" + tmpFileString + "') TOMBR('" + outMemberPathString + "') MBROPT(*REPLACE) CVTDTA(*AUTO)";
// Perform the command
cmdCall.run(commandCpyFrmStmfString);
// Get messages from the command if any
AS400Message[] as400MessageList = cmdCall.getMessageList();
// return.
for (AS400Message as400Message : as400MessageList) {
if (as400Message.getType() == AS400Message.ESCAPE) {
row = "Error: Copy IFS file " + tmpFileString + " to source member " + tmpFileString + " using command CPYFRMSTMF - " + as400Message.getID() + " " + as400Message.getText();
mainWindow.msgVector.add(row);
mainWindow.showMessages();
return "ERROR";
} else {
row = "Info: Copy IFS file " + tmpFileString + " to source member " + tmpFileString + " using command CPYFRMSTMF - " + as400Message.getID() + " " + as400Message.getText();
mainWindow.msgVector.add(row);
mainWindow.showMessages();
}
}
// Delete the temporary file
ifsTmpFile.delete();
row = "Comp: Source member " + libraryName + "/" + fileName + "(" + memberName + ") was saved.";
mainWindow.msgVector.add(row);
mainWindow.showMessages();
return "";
} catch (Exception exc) {
try {
outStream.close();
} catch (Exception exce) {
exce.printStackTrace();
}
exc.printStackTrace();
row = "Error: 3 Data cannot be written to the source member " + libraryName + "/" + fileName + "(" + memberName + ") - " + exc.toString();
mainWindow.msgVector.add(row);
mainWindow.showMessages();
// Must not continue in order not to lock an object
return "ERROR";
}
}
use of com.ibm.as400.access.IFSFileOutputStream in project IBMiProgTool by vzupka.
the class Copy_IBMi_IBMi method copyToIfsFile.
/**
* Copy IFS file or Source member or Save file to an IFS file; If the target
* IFS file does not exist, one is created.
*
* @param targetPathString
* @param sourcePathString
* @param fromWalk
* @return
*/
protected String copyToIfsFile(String sourcePathString, String targetPathString, boolean fromWalk) {
if (targetPathString.equals(sourcePathString)) {
return "ERROR";
}
// Path to input IFS file
inputDirFile = new IFSFile(remoteServer, sourcePathString);
// Path to output IFS file
outputDirFile = new IFSFile(remoteServer, targetPathString);
try {
// ---------------
if (sourcePathString.startsWith("/QSYS.LIB/")) {
extractNamesFromIfsPath(sourcePathString);
if (sourcePathString.endsWith(".FILE") && inputDirFile.isSourcePhysicalFile()) {
if (targetPathString.endsWith(".savf")) {
row = "Error: Source file " + libraryName + "/" + fileName + " cannot be copied to IFS file " + targetPathString + " ending with .savf.";
mainWindow.msgVector.add(row);
mainWindow.showMessages(nodes);
return "ERROR";
}
msgText = copyFromSourceFile(remoteServer, sourcePathString, targetPathString);
if (msgText.isEmpty()) {
row = "Comp: Source file " + libraryName + "/" + fileName + " was copied to IFS file " + targetPathString + ".";
mainWindow.msgVector.add(row);
mainWindow.showMessages(nodes);
return "";
} else {
row = "Comp File: Source file " + libraryName + "/" + fileName + " was NOT copied to IFS file.";
mainWindow.msgVector.add(row);
mainWindow.showMessages(nodes);
return "ERROR";
}
}
if (sourcePathString.endsWith(".MBR")) {
if (targetPathString.endsWith(".savf")) {
row = "Error: Source member " + libraryName + "/" + fileName + "(" + memberName + ") cannot be copied to IFS file " + targetPathString + " ending with .savf.";
mainWindow.msgVector.add(row);
mainWindow.showMessages(nodes);
return "ERROR";
}
msgText = copyFromSourceMember(remoteServer, sourcePathString, targetPathString);
if (msgText.isEmpty()) {
row = "Comp: Source member " + libraryName + "/" + fileName + "(" + memberName + ") was copied to IFS file " + targetPathString + ".";
mainWindow.msgVector.add(row);
mainWindow.showMessages(nodes);
return "";
} else {
row = "Comp File: Source member " + libraryName + "/" + fileName + "(" + memberName + ") was NOT copied to IFS file.";
mainWindow.msgVector.add(row);
mainWindow.showMessages(nodes);
return "ERROR";
}
} else // ---------
if (inputDirFile.toString().contains(".LIB") && inputDirFile.toString().endsWith(".SAVF")) {
msgText = copyFromSaveFile(remoteServer, sourcePathString, targetPathString);
return msgText;
}
} else {
if (outputDirFile.isDirectory()) {
// When IFS directory, add source file name
// ------------------
// IFS file path string = target IFS directory + source IFS file
// name
targetPathString = sourcePathStringPrefix + "/" + inputDirFile.getName();
outputDirFile = new IFSFile(remoteServer, targetPathString);
}
// =======================================
try {
byte[] inputByteArray = new byte[2000000];
int bytesRead;
// ---------------------------------
if (outputDirFile.exists() && !overwriteAllowed) {
row = "Error: IFS file " + inputDirFile + " was NOT copied to the existing file " + targetPathString + ". Overwriting files is not allowed.";
mainWindow.msgVector.add(row);
mainWindow.showMessages(noNodes);
return "ERROR";
}
// Get input file CCSID attribute
int inputFileCcsid = inputDirFile.getCCSID();
// Target file CCSID attribute - not yet ready
int outputFileCcsid;
// -------------------------------------
if (!outputDirFile.exists()) {
// If target file does not exist, create one and set its CCSID
// from the input file
outputDirFile.createNewFile();
outputDirFile.setCCSID(inputFileCcsid);
}
outputFileCcsid = outputDirFile.getCCSID();
// Open input IFS file
ifsInStream = new IFSFileInputStream(remoteServer, sourcePathString);
//
if (ibmCcsid.equals("*DEFAULT") || outputFileCcsid == inputFileCcsid) {
ifsOutStream = new IFSFileOutputStream(remoteServer, targetPathString);
// Copy IFS file to IFS file reading input stream to byte
// array and using output stream for output
// Read first portion of bytes
bytesRead = ifsInStream.read(inputByteArray);
// Repeat if at least one byte was read
while (bytesRead > 0) {
// Write out bytes read before
ifsOutStream.write(inputByteArray, 0, bytesRead);
// Read next portion of bytes
bytesRead = ifsInStream.read(inputByteArray);
}
// Close files
ifsOutStream.close();
ifsInStream.close();
if (fromWalk) {
row = "Info: IFS file " + sourcePathString + " was copied unchanged (binary) to IFS file " + targetPathString + ". " + outputFileCcsid + " -> " + outputFileCcsid + ".";
} else {
row = "Comp: IFS file " + sourcePathString + " was copied unchanged (binary) to IFS file " + targetPathString + ". " + outputFileCcsid + " -> " + outputFileCcsid + ".";
}
//
} else {
//
// Conversion from source IFS file's CCSID to target IFS file's CCSID
// ----------
// Open output IFS file
ifsOutStream = new IFSFileOutputStream(remoteServer, targetPathString, outputFileCcsid);
// Copy IFS file to IFS file reading input stream to byte
// array and using byte array for output
// Read first portion of bytes
bytesRead = ifsInStream.read(inputByteArray);
// Repeat if at least one byte was read
while (bytesRead > 0) {
// Convert input byte array with input CCSID to String
// (UTF-16)
AS400Text textConverter = new AS400Text(bytesRead, inputFileCcsid, remoteServer);
String text = (String) textConverter.toObject(inputByteArray);
// Convert data from String (UTF-16) to outpu byte array
// with output CCSID
AS400Text textConverter2 = new AS400Text(bytesRead, outputFileCcsid, remoteServer);
byte[] outputByteArray = (byte[]) textConverter2.toBytes(text);
// Write converted text in second byte array to output
// stream
ifsOutStream.write(outputByteArray, 0, outputByteArray.length);
// Read next byte array
bytesRead = ifsInStream.read(inputByteArray);
// Close files
ifsOutStream.close();
ifsInStream.close();
if (fromWalk) {
row = "Info: IFS file " + sourcePathString + " was copied to IFS file " + targetPathString + ", Convert " + inputFileCcsid + " -> " + outputFileCcsid;
} else {
row = "Comp: IFS file " + sourcePathString + " was copied to IFS file " + targetPathString + ", Convert " + inputFileCcsid + " -> " + outputFileCcsid;
}
}
}
mainWindow.msgVector.add(row);
mainWindow.showMessages(nodes);
return "";
} catch (Exception exc) {
exc.printStackTrace();
row = "Error: Copying to IFS file " + targetPathString + " - " + exc.toString();
mainWindow.msgVector.add(row);
mainWindow.showMessages(nodes);
return "ERROR";
}
}
} catch (Exception exc) {
exc.printStackTrace();
row = "Error: Copying to IFS file " + targetPathString + " - " + exc.toString();
mainWindow.msgVector.add(row);
mainWindow.showMessages(nodes);
return "ERROR";
}
return "";
}
use of com.ibm.as400.access.IFSFileOutputStream in project IBMiProgTool by vzupka.
the class EditFile method rewriteIfsFile.
/**
* Rewrite IFS file with edited text area.
*
* @return
*/
protected String rewriteIfsFile() {
try {
IFSFileOutputStream outStream = new IFSFileOutputStream(remoteServer, filePathString);
String textAreaString = textArea.getText();
byte[] byteArray;
AS400Text textConverter = new AS400Text(textAreaString.length(), ccsidAttribute, remoteServer);
byteArray = textConverter.toBytes(textAreaString);
// Write text from the text area to the file
outStream.write(byteArray);
// Close file
outStream.close();
row = "Comp: IFS file " + filePathString + " was saved.";
mainWindow.msgVector.add(row);
mainWindow.showMessages();
return "";
} catch (Exception exc) {
exc.printStackTrace();
row = "Error in rewriting IFS file: " + exc.toString();
mainWindow.msgVector.add(row);
mainWindow.showMessages();
return "ERROR";
}
}
Aggregations