1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.core.net;
18
19 import org.apache.logging.log4j.Logger;
20 import org.apache.logging.log4j.core.appender.AppenderRuntimeException;
21 import org.apache.logging.log4j.status.StatusLogger;
22
23 import java.io.IOException;
24 import java.io.OutputStream;
25 import java.net.DatagramPacket;
26 import java.net.DatagramSocket;
27 import java.net.InetAddress;
28 import java.net.SocketException;
29 import java.net.UnknownHostException;
30
31
32
33
34 public class DatagramOutputStream extends OutputStream {
35
36
37
38
39 protected static final Logger LOGGER = StatusLogger.getLogger();
40
41 private static final int SHIFT_1 = 8;
42 private static final int SHIFT_2 = 16;
43 private static final int SHIFT_3 = 24;
44
45 private DatagramSocket ds;
46 private final InetAddress address;
47 private final int port;
48
49 private byte[] data;
50
51
52
53
54
55
56 public DatagramOutputStream(String host, int port) {
57 this.port = port;
58 try {
59 address = InetAddress.getByName(host);
60 } catch (UnknownHostException ex) {
61 String msg = "Could not find host " + host;
62 LOGGER.error(msg, ex);
63 throw new AppenderRuntimeException(msg, ex);
64 }
65
66 try {
67 ds = new DatagramSocket();
68 } catch (SocketException ex) {
69 String msg = "Could not instantiate DatagramSocket to " + host;
70 LOGGER.error(msg, ex);
71 throw new AppenderRuntimeException(msg, ex);
72 }
73 }
74
75 @Override
76 public synchronized void write(byte[] bytes, int offset, int length) throws IOException {
77 copy(bytes, offset, length);
78 }
79
80 @Override
81 public synchronized void write(int i) throws IOException {
82 copy(new byte[] {(byte) (i >>> SHIFT_3), (byte) (i >>> SHIFT_2), (byte) (i >>> SHIFT_1), (byte) i}, 0, 4);
83 }
84
85 @Override
86 public synchronized void write(byte[] bytes) throws IOException {
87 copy(bytes, 0, bytes.length);
88 }
89
90 @Override
91 public synchronized void flush() throws IOException {
92 if (this.ds != null && this.address != null) {
93 DatagramPacket packet = new DatagramPacket(data, data.length, address, port);
94 ds.send(packet);
95 }
96 data = null;
97 }
98
99 @Override
100 public synchronized void close() throws IOException {
101 if (ds != null) {
102 if (data != null) {
103 flush();
104 }
105 ds.close();
106 ds = null;
107 }
108 }
109
110 private void copy(byte[] bytes, int offset, int length) {
111 int index = data == null ? 0 : data.length;
112 byte[] copy = new byte[length + index];
113 if (data != null) {
114 System.arraycopy(data, 0, copy, 0, index);
115 }
116 System.arraycopy(bytes, offset, copy, index, length);
117 data = copy;
118 }
119 }