|
From: <rm...@hy...> - 2007-03-25 22:21:11
|
Author: rmorgan Date: 2007-03-25 14:21:07 -0800 (Sun, 25 Mar 2007) New Revision: 3897 URL: http://svn.hyperic.org/?view=rev&root=Hyperic+HQ&revision=3897 Modified: trunk/src/org/hyperic/hq/livedata/agent/commands/LiveData_result.java Log: Compress livedata results. Modified: trunk/src/org/hyperic/hq/livedata/agent/commands/LiveData_result.java =================================================================== --- trunk/src/org/hyperic/hq/livedata/agent/commands/LiveData_result.java 2007-03-25 21:36:53 UTC (rev 3896) +++ trunk/src/org/hyperic/hq/livedata/agent/commands/LiveData_result.java 2007-03-25 22:21:07 UTC (rev 3897) @@ -28,9 +28,19 @@ import org.hyperic.hq.agent.AgentRemoteValue; import org.hyperic.hq.agent.AgentAssertionException; import org.hyperic.hq.agent.AgentRemoteException; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.zip.Deflater; +import java.util.zip.Inflater; +import java.util.zip.DataFormatException; + public class LiveData_result extends AgentRemoteValue { + private static final Log _log = LogFactory.getLog(LiveData_result.class); + private static final String PARAM_RESULT = "result"; public void setValue(String key, String val) { @@ -42,18 +52,78 @@ } public LiveData_result(AgentRemoteValue val) - throws AgentRemoteException { + throws AgentRemoteException + { + String resultAlreadyCompressed = val.getValue(PARAM_RESULT); + // Only used from the LiveDataClient, where the result value has + // already been compressed. + setResultCompressed(resultAlreadyCompressed); + } - String result = val.getValue(PARAM_RESULT); + public void setResultCompressed(String alreadyCompressed) { + super.setValue(PARAM_RESULT, alreadyCompressed); + } - setResult(result); + public void setResult(String result) + throws IOException + { + String compressed = compress(result); + _log.debug("Compressed " + result.length() + " bytes to " + + compressed.length() + " bytes."); + + super.setValue(PARAM_RESULT, compressed); } - public void setResult(String result) { - super.setValue(PARAM_RESULT, result); + public String getResult() + throws IOException, DataFormatException + { + String compressed = super.getValue(PARAM_RESULT); + _log.debug("Decompressing " + compressed.length() + " bytes"); + return decompress(compressed); } - public String getResult() { - return super.getValue(PARAM_RESULT); + private String compress(String s) + throws IOException + { + Deflater compressor = new Deflater(); + compressor.setInput(s.getBytes()); + compressor.finish(); + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + + int total = 0; + byte[] buf = new byte[1024]; + while (!compressor.finished()) { + int count = compressor.deflate(buf); + total += count; + bos.write(buf, 0, count); + } + + bos.close(); + + byte[] compressedData = bos.toByteArray(); + return new String(compressedData, 0, total); } + + public static String decompress(String s) + throws IOException, DataFormatException + { + Inflater decompressor = new Inflater(); + decompressor.setInput(s.getBytes()); + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + + int total = 0; + byte[] buf = new byte[1024]; + while (!decompressor.finished()) { + int count = decompressor.inflate(buf); + total += count; + bos.write(buf, 0, count); + } + + bos.close(); + + byte[] decompressedData = bos.toByteArray(); + return new String(decompressedData, 0, total); + } } |