Reputation: 3147
I am having an application where two processes talk using SOAP. A file is being transferred from Process A to Process B. Where Process B will store it to DB.
opqData.setBase64Binary(new DataHandler(new FileDataSource(file)));
where file is the data which needs to be stored to the DB. However now i want to zip the data when storing it to DB. One option is to zip the file and send it as FileDataSource. However i cant use it because we have more than 1000 such files and it creates a lot of zip entries in the file structure and creating the zip is additions overhead.
So i was thinking to implement the DataHandler as GzipDataHandler and the input stream returned is gzipInputStream to process B. So the data will be zipped and stored to the DB.
However i am confused how to write the getInputStream method for my new GzipDataHandler.
Has any one tried something like this before? Or can i get any pointers from Java and SOAP experts?
Thanks,
Dheeraj Joshi
Upvotes: 0
Views: 1359
Reputation: 3147
Ok. I found out the solution. Solution is not to change the DataHandler but to change the FileDataSource.
Create a new FileDataSource say ZipFileDataSource and extend the FileDataSource and implement the getInputStream method.
Your getInputStream method should read the file and GZIPOutputStream should zip it and it should be passed through the pipes to the caller.
final PipedOutputStream pos = new PipedOutputStream();
final PipedInputStream pis = new PipedInputStream(pos);
FileInputStream fis = null;
GZIPOutputStream gos = null;
byte[] buffer = new byte[1024];
try {
fis = new FileInputStream(MyFile);
gos = new GZIPOutputStream(pos);
int length;
while ((length = fis.read(buffer, 0, 1024)) != -1)
gos.write(buffer, 0, length);
fis.close();
} catch(Exception e){
}
Above is the sample code.
Regards,
Dheeraj Joshi
Upvotes: 0
Reputation: 4104
This might help you:
http://www.exampledepot.com/egs/java.util.zip/CompressFile.html
Upvotes: 1