From: Giulio V. <gva...@us...> - 2004-04-01 09:52:04
|
Update of /cvsroot/exist/eXist-1.0/src/org/exist/xmlrpc In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv688/src/org/exist/xmlrpc Modified Files: RpcServer.java Log Message: Add COMPRESS_OUTPUT option on xmlrpc:getdocument method. The compression format are gzip. Default no Index: RpcServer.java =================================================================== RCS file: /cvsroot/exist/eXist-1.0/src/org/exist/xmlrpc/RpcServer.java,v retrieving revision 1.31 retrieving revision 1.32 diff -C2 -d -r1.31 -r1.32 *** RpcServer.java 30 Mar 2004 16:56:31 -0000 1.31 --- RpcServer.java 1 Apr 2004 09:40:07 -0000 1.32 *************** *** 29,32 **** --- 29,34 ---- import java.util.Stack; import java.util.Vector; + import java.util.zip.*; + import java.io.*; import javax.xml.transform.OutputKeys; *************** *** 304,307 **** --- 306,311 ---- String encoding = "UTF-8"; + String compression ="no"; + if (((String) parametri.get("encoding")) == null) { *************** *** 310,313 **** --- 314,323 ---- encoding = (String) parametri.get("encoding"); } + + + if (((String) parametri.get(EXistOutputKeys.COMPRESS_OUTPUT)) != null) { + compression = (String) parametri.get(EXistOutputKeys.COMPRESS_OUTPUT); + } + RpcConnection con = pool.get(); try { *************** *** 316,322 **** throw new EXistException("document " + name + " not found!"); try { ! return xml.getBytes(encoding); } catch (UnsupportedEncodingException uee) { ! return xml.getBytes(); } } catch (Exception e) { --- 326,345 ---- throw new EXistException("document " + name + " not found!"); try { ! if (compression.equals("no")) { ! return xml.getBytes(encoding); ! } else { ! LOG.debug("getdocument with compression"); ! return compress(xml.getBytes(encoding)); ! } ! } catch (UnsupportedEncodingException uee) { ! ! if (compression.equals("no")) { ! return xml.getBytes(); ! } else { ! LOG.debug("getdocument with compression"); ! return compress(xml.getBytes()); ! } ! } } catch (Exception e) { *************** *** 1369,1371 **** --- 1392,1419 ---- } + private static byte[] compress(byte[] whatToCompress) throws IOException { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ZipOutputStream gzos = new ZipOutputStream(baos); + gzos.setMethod(gzos.DEFLATED); + gzos.putNextEntry(new ZipEntry(whatToCompress.length+"")); + gzos.write(whatToCompress); + gzos.closeEntry(); + gzos.finish(); + gzos.close(); + return baos.toByteArray(); + } + + private static byte[] uncompress(byte[] whatToUncompress) throws IOException { + ByteArrayInputStream bais = new ByteArrayInputStream(whatToUncompress); + ZipInputStream gzis = new ZipInputStream(bais); + ZipEntry zipentry = gzis.getNextEntry(); + int len = Integer.parseInt(zipentry.getName()); + ByteArrayOutputStream baos = new ByteArrayOutputStream(len); + byte[] buf = new byte[512]; + int bread; + while ((bread = gzis.read(buf)) != -1) baos.write(buf, 0, bread); + gzis.closeEntry(); + gzis.close(); + return baos.toByteArray(); + } } |