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.IOException;
017import java.io.ObjectOutputStream;
018
019/**
020 * Automatically flushes the underlying {@link java.io.ObjectOutputStream}
021 * immediately after calling it's
022 * {@link java.io.ObjectOutputStream#writeObject(Object)} method.
023 *
024 * @author Sebastian Gröbler
025 */
026public class AutoFlushingObjectWriter implements ObjectWriter {
027
028    private final ObjectOutputStream objectOutputStream;
029    private final int resetFrequency;
030    private int writeCounter = 0;
031
032    /**
033     * Creates a new instance for the given {@link java.io.ObjectOutputStream}.
034     *
035     * @param objectOutputStream the stream to write to
036     * @param resetFrequency     the frequency with which the given stream will be
037     *                           automatically reset to prevent a memory leak
038     */
039    public AutoFlushingObjectWriter(ObjectOutputStream objectOutputStream, int resetFrequency) {
040        this.objectOutputStream = objectOutputStream;
041        this.resetFrequency = resetFrequency;
042    }
043
044    @Override
045    public void write(Object object) throws IOException {
046        objectOutputStream.writeObject(object);
047        objectOutputStream.flush();
048        preventMemoryLeak();
049    }
050
051    /**
052     * Failing to reset the object output stream every now and then creates a
053     * serious memory leak which is why the underlying stream will be reset
054     * according to the {@code resetFrequency}.
055     */
056    private void preventMemoryLeak() throws IOException {
057        if (++writeCounter >= resetFrequency) {
058            objectOutputStream.reset();
059            writeCounter = 0;
060        }
061    }
062}