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