use of com.ibm.as400.access.SequentialFile 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";
}
}
Aggregations