001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.net;
015
016import java.io.ByteArrayOutputStream;
017import java.io.IOException;
018import java.io.OutputStream;
019import java.net.DatagramPacket;
020import java.net.DatagramSocket;
021import java.net.InetAddress;
022import java.net.SocketException;
023import java.net.UnknownHostException;
024
025/**
026 * SyslogOutputStream is a wrapper around the {@link DatagramSocket} class so
027 * that it behaves like an {@link OutputStream}.
028 */
029public class SyslogOutputStream extends OutputStream {
030
031    /**
032     * The maximum length after which we discard the existing string buffer and
033     * start anew.
034     */
035    private static final int MAX_LEN = 1024;
036
037    private InetAddress address;
038    private DatagramSocket ds;
039    private ByteArrayOutputStream baos = new ByteArrayOutputStream();
040    final private int port;
041
042    public SyslogOutputStream(String syslogHost, int port) throws UnknownHostException, SocketException {
043        this.address = InetAddress.getByName(syslogHost);
044        this.port = port;
045        this.ds = new DatagramSocket();
046    }
047
048    public void write(byte[] byteArray, int offset, int len) throws IOException {
049        baos.write(byteArray, offset, len);
050    }
051
052    public void flush() throws IOException {
053        byte[] bytes = baos.toByteArray();
054        DatagramPacket packet = new DatagramPacket(bytes, bytes.length, address, port);
055
056        // clean up for next round
057        if (baos.size() > MAX_LEN) {
058            baos = new ByteArrayOutputStream();
059        } else {
060            baos.reset();
061        }
062
063        // after a failure, it can happen that bytes.length is zero
064        // in that case, there is no point in sending out an empty message/
065        if (bytes.length == 0) {
066            return;
067        }
068        if (this.ds != null) {
069            ds.send(packet);
070        }
071
072    }
073
074    public void close() {
075        address = null;
076        ds = null;
077    }
078
079    public int getPort() {
080        return port;
081    }
082
083    @Override
084    public void write(int b) throws IOException {
085        baos.write(b);
086    }
087
088    int getSendBufferSize() throws SocketException {
089        return ds.getSendBufferSize();
090    }
091}