use of com.ibm.as400.access.AS400Text in project camel by apache.
the class Jt400PgmProducer method getParameterList.
private ProgramParameter[] getParameterList(Exchange exchange) throws InvalidPayloadException, PropertyVetoException {
Object body = exchange.getIn().getMandatoryBody();
Object[] params = (Object[]) body;
ProgramParameter[] parameterList = new ProgramParameter[params.length];
for (int i = 0; i < params.length; i++) {
Object param = params[i];
boolean input;
boolean output;
if (getISeriesEndpoint().isFieldIdxForOuput(i)) {
output = true;
input = param != null;
} else {
output = false;
input = true;
}
byte[] inputData = null;
// XXX Actually, returns any field length, not just output.
int length = getISeriesEndpoint().getOutputFieldLength(i);
if (input) {
if (param != null) {
AS400DataType typeConverter;
if (getISeriesEndpoint().getFormat() == Jt400Configuration.Format.binary) {
typeConverter = new AS400ByteArray(length);
} else {
typeConverter = new AS400Text(length, iSeries);
}
inputData = typeConverter.toBytes(param);
}
// Else, inputData will remain null.
}
if (input && output) {
LOG.trace("Parameter {} is both input and output.", i);
parameterList[i] = new ProgramParameter(inputData, length);
} else if (input) {
LOG.trace("Parameter {} is input.", i);
if (inputData != null) {
parameterList[i] = new ProgramParameter(inputData);
} else {
parameterList[i] = new ProgramParameter();
parameterList[i].setParameterType(ProgramParameter.PASS_BY_REFERENCE);
// Just for self documentation.
parameterList[i].setNullParameter(true);
}
} else {
// output
LOG.trace("Parameter {} is output.", i);
parameterList[i] = new ProgramParameter(length);
}
}
return parameterList;
}
use of com.ibm.as400.access.AS400Text 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.AS400Text in project IBMiProgTool by vzupka.
the class DisplayFile method displayIfsFile.
/**
* Display contents of the IFS file using its CCSID attribute
*
* @param remoteServer
* @param ifsFilePathString
*/
protected void displayIfsFile(AS400 remoteServer, String ifsFilePathString) {
this.setTitle("Display IFS file '" + ifsFilePathString + "'");
// The user can correct the parameter "IBMi CCSID" and try again.
try {
IFSFile ifsFile = new IFSFile(remoteServer, ifsFilePathString);
int attributeCCSID = ifsFile.getCCSID();
characterSetLabel.setText("CCSID " + attributeCCSID + " was used for display.");
byte[] inputBuffer = new byte[100000];
byte[] workBuffer = new byte[100000];
try (IFSFileInputStream inputStream = new IFSFileInputStream(remoteServer, ifsFilePathString)) {
int bytesRead = inputStream.read(inputBuffer);
while (bytesRead != -1) {
for (int idx = 0; idx < bytesRead; idx++) {
// Copy input byte to output byte
workBuffer[idx] = inputBuffer[idx];
}
// Copy the printable part of the work array to a new buffer that will be written out.
byte[] bufferToWrite = new byte[bytesRead];
// Copy bytes from the work buffer to the new buffer
for (int indx = 0; indx < bytesRead; indx++) {
bufferToWrite[indx] = workBuffer[indx];
}
// Create object for conversion from bytes to characters
AS400Text textConverter = new AS400Text(bytesRead, attributeCCSID, remoteServer);
// Convert byte array buffer to text line
String textLine = (String) textConverter.toObject(bufferToWrite);
// Append the line to text area
textArea.append(textLine + NEW_LINE);
// Read next input buffer
bytesRead = inputStream.read(inputBuffer);
}
// Set scroll bar to top
textArea.setCaretPosition(0);
// Display the window.
setVisible(true);
row = "Info: IFS file " + ifsFilePathString + " has CCSID " + attributeCCSID + ".";
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);
}
use of com.ibm.as400.access.AS400Text in project IBMiProgTool by vzupka.
the class EditFile method displayIfsFile.
/**
* Display contents of the IFS file using its CCSID attribute.
*/
protected void displayIfsFile() {
this.setTitle("Edit IFS file '" + filePathString + "'");
// Correct the parameter "IBMi CCSID".
try {
IFSFile ifsFile = new IFSFile(remoteServer, filePathString);
ccsidAttribute = ifsFile.getCCSID();
characterSetLabel.setText("CCSID " + ccsidAttribute + " was used for display.");
byte[] inputBuffer = new byte[100000];
byte[] workBuffer = new byte[100000];
textArea.setText("");
try (IFSFileInputStream inputStream = new IFSFileInputStream(remoteServer, filePathString)) {
int bytesRead = inputStream.read(inputBuffer);
while (bytesRead != -1) {
for (int idx = 0; idx < bytesRead; idx++) {
// Copy input byte to output byte
workBuffer[idx] = inputBuffer[idx];
}
// Copy the printable part of the work array
// to a new buffer that will be written out.
byte[] bufferToWrite = new byte[bytesRead];
// Copy bytes from the work buffer to the new buffer
for (int indx = 0; indx < bytesRead; indx++) {
bufferToWrite[indx] = workBuffer[indx];
}
// Create object for conversion from bytes to characters
AS400Text textConverter = new AS400Text(bytesRead, ccsidAttribute, remoteServer);
// Convert byte array buffer to text line
String textLine = (String) textConverter.toObject(bufferToWrite);
// Append the line to text area
textArea.append(textLine + NEW_LINE);
// Read next input buffer
bytesRead = inputStream.read(inputBuffer);
}
}
} catch (Exception exc) {
isError = true;
row = "Error in displaying IFS file: " + 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);
}
use of com.ibm.as400.access.AS400Text 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";
}
}
Aggregations