use of com.ibm.as400.access.AS400Text 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);
}
use of com.ibm.as400.access.AS400Text 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";
}
}
use of com.ibm.as400.access.AS400Text 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 "";
}
use of com.ibm.as400.access.AS400Text in project IBMiProgTool by vzupka.
the class Copy_IBMi_PC method copyDataFromMemberToFile.
/**
* Copy data from source member to PC file;
* If the output PC file does not exist, one is created.
*
* @param remoteServer
* @param as400PathString
* @param pcPathString
* @return
*/
@SuppressWarnings("UseSpecificCatch")
protected String copyDataFromMemberToFile(AS400 remoteServer, String as400PathString, String pcPathString) {
IFSFile ifsFile = new IFSFile(remoteServer, as400PathString);
// Create an AS400FileRecordDescription object that represents the file
AS400FileRecordDescription inRecDesc = new AS400FileRecordDescription(remoteServer, as400PathString);
Path pcFilePath = Paths.get(pcPathString);
try {
int ccsidAttribute = ifsFile.getCCSID();
int ccsidForDisplay = ccsidAttribute;
// In this case, the universal CCSID 65535 is assumed.
if (ccsidAttribute == 1208) {
ccsidForDisplay = 65535;
}
if (ibmCcsidInt == 1208) {
ibmCcsidInt = 65535;
}
// 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);
//
if (Files.exists(pcFilePath) && !properties.getProperty("OVERWRITE_FILE").equals("Y")) {
row = "Info: Source member " + libraryName + "/" + fileName + "(" + memberName + ") cannot be copied to PC file " + pcPathString + ". Overwriting files is not allowed.";
mainWindow.msgVector.add(row);
mainWindow.showMessages(noNodes);
return "ERROR";
}
// If the file does not exist, create it
if (!Files.exists(pcFilePath)) {
Files.createFile(pcFilePath);
}
// Rewrite the PC file with records from the source member
// -------------------
//
// Open the output PC text file - with specified character set
// ----------------------------
// Characters read from input are translated to the specified character set if possible.
// If input characters are incapable to be translated, an error message is reported (UnmappableCharacterException).
BufferedWriter pcOutFile;
if (pcCharset.equals("*DEFAULT")) {
// Ignore PC charset parameter for binary transfer.
pcOutFile = Files.newBufferedWriter(pcFilePath, StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING);
} else {
// Use PC charset parameter for conversion.
pcOutFile = Files.newBufferedWriter(pcFilePath, Charset.forName(pcCharset), StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING);
}
// Read the first source member record
Record inRecord = as400seqFile.readNext();
// --------------------
while (inRecord != null) {
StringBuilder textLine = new StringBuilder();
if (sourceRecordPrefixPresent) {
// 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");
String seq = df1.format((Number) inRecord.getField("SRCSEQ"));
String seq2 = seq.substring(0, 4) + seq.substring(5);
textLine.append(seq2);
String srcDat = df2.format((Number) inRecord.getField("SRCDAT"));
textLine.append(srcDat);
}
// Otherwise, source record consists of source data only
// Convert source data into byte array
byte[] byteArray = inRecord.getFieldAsBytes("SRCDTA");
String translatedData;
// Translate member data using parameter "IBM i CCSID"
AS400Text textConverter = new AS400Text(byteArray.length, ibmCcsidInt, remoteServer);
if (ibmCcsid.equals("*DEFAULT")) {
// Translate member data using its CCSID attribute (possibly modified)
textConverter = new AS400Text(byteArray.length, ccsidForDisplay, remoteServer);
}
// Convert byte array buffer to String text line (UTF-16)
String str = (String) textConverter.toObject(byteArray);
if (pcCharset.equals("*DEFAULT")) {
// Assign "ISO-8859-1" as default charset
pcCharset = "ISO-8859-1";
}
// Translate the String into PC charset
translatedData = new String(str.getBytes(pcCharset), pcCharset);
textLine.append(translatedData).append(NEW_LINE);
// Write the translated text line to the PC file
pcOutFile.write(textLine.toString());
// Read next source member record
inRecord = as400seqFile.readNext();
}
// Close the files
pcOutFile.close();
as400seqFile.close();
row = "Info: Source member " + libraryName + "/" + fileName + "(" + memberName + ") was copied to PC file " + pcPathString + " using charset " + pcCharset + ".";
mainWindow.msgVector.add(row);
mainWindow.showMessages(noNodes);
return "";
} catch (Exception exc) {
exc.printStackTrace();
row = "Error: Copying member " + libraryName + "/" + fileName + "(" + memberName + ") - " + exc.toString();
mainWindow.msgVector.add(row);
mainWindow.showMessages(noNodes);
return "ERROR";
}
}
use of com.ibm.as400.access.AS400Text in project IBMiProgTool by vzupka.
the class SearchWindow method readIfsFile.
/**
* Read IFS file data into text area.
*
* @return
*/
protected JTextArea readIfsFile() {
try {
IFSFile ifsFile = new IFSFile(remoteServer, mainWindow.sourcePathString);
int attributeCCSID = ifsFile.getCCSID();
byte[] inputBuffer = new byte[100000];
byte[] workBuffer = new byte[100000];
try (IFSFileInputStream inputStream = new IFSFileInputStream(remoteServer, mainWindow.sourcePathString)) {
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);
}
mainWindow.row = "Info: IFS file " + mainWindow.sourcePathString + " has CCSID " + attributeCCSID + ".";
mainWindow.msgVector.add(mainWindow.row);
mainWindow.showMessages(mainWindow.nodes);
}
} catch (Exception exc) {
exc.printStackTrace();
mainWindow.row = "Error: " + exc.toString();
mainWindow.msgVector.add(mainWindow.row);
mainWindow.showMessages(mainWindow.nodes);
}
// Remove message scroll listener (cancel scrolling to the last message)
mainWindow.scrollMessagePane.getVerticalScrollBar().removeAdjustmentListener(mainWindow.messageScrollPaneAdjustmentListenerMax);
return textArea;
}
Aggregations