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