1
19
20 package com.liferay.util.transport;
21
22 import com.liferay.portal.kernel.log.Log;
23 import com.liferay.portal.kernel.log.LogFactoryUtil;
24
25 import java.io.ByteArrayInputStream;
26 import java.io.ByteArrayOutputStream;
27 import java.io.InputStream;
28
29 import java.net.DatagramPacket;
30
31 import java.util.zip.GZIPInputStream;
32
33
40 public class MulticastDatagramHandler implements DatagramHandler {
41
42 public MulticastDatagramHandler(boolean gzipData, boolean shortData) {
43 _gzipData = gzipData;
44 _shortData = shortData;
45 }
46
47 public void errorReceived(Throwable t) {
48 _log.error(t, t);
49 }
50
51 public void process(DatagramPacket packet) {
52 byte[] bytes = packet.getData();
53
54 if (_gzipData) {
55 try {
56 bytes = getUnzippedBytes(bytes);
57 }
58 catch (Exception e) {
59 _log.error(e, e);
60 }
61 }
62
63 if (_shortData) {
64 byte[] temp = new byte[96];
65
66 System.arraycopy(bytes, 0, temp, 0, 96);
67
68 bytes = temp;
69 }
70
71 StringBuilder sb = new StringBuilder();
72
73 sb.append("[");
74 sb.append(packet.getSocketAddress());
75 sb.append("] ");
76 sb.append(new String(bytes));
77
78 if (_log.isInfoEnabled()) {
79 _log.info(sb);
80 }
81 }
82
83 protected byte[] getUnzippedBytes(byte[] bytes) throws Exception {
84 InputStream is = new GZIPInputStream(new ByteArrayInputStream(bytes));
85 ByteArrayOutputStream baos = new ByteArrayOutputStream(bytes.length);
86
87 byte[] buffer = new byte[1500];
88
89 int c = 0;
90
91 while (true) {
92 if (c == -1) {
93 break;
94 }
95
96 c = is.read(buffer, 0, 1500);
97
98 if (c != -1) {
99 baos.write(buffer, 0, c);
100 }
101 }
102
103 is.close();
104
105 baos.flush();
106 baos.close();
107
108 return baos.toByteArray();
109 }
110
111 private static final Log _log = LogFactoryUtil.getLog(
112 MulticastDatagramHandler.class);
113
114 private boolean _gzipData;
115 private boolean _shortData;
116
117 }