javacommons/fileutils/javasrc/com/nokia/mj/impl/fileutils/FileOutputStream.java
author Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
Wed, 13 Oct 2010 14:23:59 +0300
branchRCL_3
changeset 83 26b2b12093af
parent 19 04becd199f91
permissions -rw-r--r--
Revision: v2.2.17 Kit: 201041

/*
* Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description:
*
*/

package com.nokia.mj.impl.fileutils;

import java.io.IOException;
import java.io.OutputStream;

class FileOutputStream extends OutputStream
{
    private FileStreamHandler iDataSink;
    private final Object iCloseLock;
    private boolean iIsClosed;
    private byte[] iSingleByte;

    private FileOutputStream()
    {
        iCloseLock = null;
    }

    public FileOutputStream(FileStreamHandler aDataSink)
    {
        iDataSink = aDataSink;
        iCloseLock = new Object();
        iIsClosed = false;
        iSingleByte = new byte[1];
    }

    public void write(int aByte) throws IOException
    {
        iSingleByte[0] = (byte)(aByte & 0xFF);
        write(iSingleByte, 0, 1);
    }

    public synchronized void write(byte[] aBytes, int aOffset, int aLength)
    throws IOException
    {
        synchronized (iCloseLock)
        {
            if (iIsClosed)
            {
                throw new IOException("OutputStream closed");
            }
            FileStreamHandler.checkIOParams(aBytes, aOffset, aLength);

            // Data given by user is immediately sent to be written.
            iDataSink.writeBytes(aBytes, aOffset, aLength);
        }
    }

    public synchronized void flush() throws IOException
    {
        if (iDataSink != null)
        {
            iDataSink.flush();
        }
    }

    public void close() throws IOException
    {
        // Closing OutputStream without flushing is against spec.
        // See javadoc of DataOutputStream.
        flush();
        synchronized (iCloseLock)
        {
            if (!iIsClosed)
            {
                iIsClosed = true;
                iDataSink.stopWrite();
                iDataSink = null;
            }
        }
    }

    boolean isClosed()
    {
        synchronized (iCloseLock)
        {
            return iIsClosed;
        }
    }
}