Menu
Download Free Trial

Javadoc for flexmonster compressor

Resolved

Is there any javadoc (or any documentation at all) for the flexmonster CSVCompressor API.  There are only a few examples in your documentation and I cannot find any in the forums.  I am interested in using the compressor, but need some addition information to try and figure out how to integrate it with our Java application that currently extracting the data from MongoDB, transforming the data to csv format and streaming it out one record at a time to the Response object’s PrintWiter (text-stream)

5 answers

Public
Tanya Gryshko Flexmonster August 10, 2017

Hi Dennis,

Unfortunately, we don’t have a detailed description of CSVCompressor API, but it doesn’t have a complicated structure.
Please find all supported method below:

Compression for CSV files

InputStream compressFile(String fileName, char delimiter)
InputStream compressFile(String fileName)

Compression for strings in CSV format

InputStream compressString(String string, char delimiter)
InputStream compressString(String string)

Compression for streams

InputStream compressStream(InputStream inputStream, char delimiter)
InputStream compressStream(InputStream inputStream)

Compression for Databases

InputStream compressDb(ResultSet resultSet)

Example

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement; import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.flexmonster.compressor.Compressor;
public class CompressorServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
  try {
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/foodmart", "root", "password");
    String query = "SELECT * FROM customer";
    Statement statement = connection.createStatement();
    ResultSet resultSet = statement.executeQuery(query);

    InputStream inputStream = Compressor.compressDb(resultSet);
     //InputStream inputStream = Compressor.compressFile(this.getServletContext().getRealPath("/WEB-INF/data.csv"));

    response.setContentType("text/plain");
    OutputStream outputStream = response.getOutputStream();
     byte[] buffer = new byte[10240];
    int length = 0;
    while ((length = inputStream.read(buffer)) > 0) {
      outputStream.write(buffer, 0, length);
      outputStream.flush();
     }
   } catch (Exception e) {
     e.printStackTrace();
  }

  }
}

Public
Dennis Urech August 10, 2017

I am using MongDB, am performing a query and when the resultset is returned, I am processing each record, converting it to csv format and writing it to the HttpServletResponse outstream via ‘out.println(csvData);’  So I would assume I would use the Compressors for strings
InputStream compressString(String string) to compress the csvData and then use the standard out.write(); out.flush() loop as you have documented.  However, wouldn’t I need to change the response.setContentType to “application/octet-stream”

Public
Tanya Gryshko Flexmonster August 11, 2017

Hi Dennis,

I think in your case it would be enough to set content-type as "text/plain".
Please ask me if you need more help.

Regards,
  Tanya

Public
Dennis Urech August 18, 2017

when I call…

InputStream in = Compresor.compressString(columnNames); 

where columnNames is a string of comma separated CSV column names (e.g. “ID,Name,Date,Time,Source,Severity,Category,Favorite”), I get an UnsupportedEncodingException:uft8
Note the “UFT8”.  I think you have a typo in your code.  You probably meant “UTF8”. Assuming this is issue and since you have not provided the source, I am unable to to use the Compressor until this code is corrected. 
By the way, it would be nice to document an example of using the “compressString()” method.  The only examples I can find are compressDB and compressFile.

Public
Dmytro Zvazhii Flexmonster August 22, 2017

Hello,
Please find the answer to the question in the following thread: http://www.flexmonster.com/question/error-calling-compressor-compressstringstring/.
Regards,
Dmytro.

Please login or Register to Submit Answer