This code writes a 193 MB file in 4 secs if the input is a byte but takes about 12 secs to write to a file if the input is a string. Try it and see if it works for you.
I never did it before but if you want to reduce the execution time further, I believe there should be a method somewhere in IDataCoder class that can convert IData object to byte directly without converting to string. Use that in conjunction with this service and pass the byte as the input to this service.
Inputs:
xmlData: input xml string
fileLocation: Target location to write the file.
(optional)bytes: byte representation of the xml string or plain text.
IDataCursor pipelineCursor = pipeline.getCursor();
String xmlData = IDataUtil.getString( pipelineCursor, "xmlData" );
String fileLocation = IDataUtil.getString( pipelineCursor, "fileLocation" );
//Object bytes = IDataUtil.get( pipelineCursor, "bytes" );
pipelineCursor.destroy();
//Clock start time
long timeIn = new Date().getTime();
try {
//InputStream is = new ByteArrayInputStream((byte[]) bytes);
InputStream is = new ByteArrayInputStream(xmlData.getBytes());
Reader in = new InputStreamReader(is);
Writer out = new FileWriter(fileLocation);
while (true) {
synchronized (inChars) {
int amountRead = in.read(inChars);
if (amountRead == -1) {
break;
}
out.write(inChars, 0, amountRead);
}
}
in.close();
out.close();
//Clock end time
long timeOut = new Date().getTime();
IDataUtil.put(pipelineCursor, "debug", "Custom buffered copy Time for a file of size :" + (int) new File(fileLocation).length() +" is "+(timeOut-timeIn));
} catch (FileNotFoundException e) {
e.printStackTrace();
IDataUtil.put( pipelineCursor, "result", e.toString());
} catch (IOException e) {
e.printStackTrace();
IDataUtil.put( pipelineCursor, "result", e.toString());
}
Shared Code:
static final int CHUNK_SIZE = 100000;
static final char[] inChars = new char[CHUNK_SIZE];
Cheers,
Akshith
#Flow-and-Java-services#webMethods#Integration-Server-and-ESB