//
// $Id: DataOutStream.h,v 1.10 2005/07/12 19:15:36 will_mason Exp $
//
// vi: set ft=objc:

/*
 * ObjectiveLib - a library of containers and algorithms for Objective-C
 *
 * Copyright (c) 2004
 * Will Mason
 *
 * Portions:
 *
 * Copyright (c) 1994
 * Hewlett-Packard Company
 *
 * Copyright (c) 1996,1997
 * Silicon Graphics Computer Systems, Inc.
 *
 * Copyright (c) 1997
 * Moscow Center for SPARC Technology
 *
 * Copyright (c) 1999 
 * Boris Fomitchev
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * You may contact the author at will_mason@users.sourceforge.net.
 */

#if !defined(DATAOUTSTREAM_OL_GUARD)
#define DATAOUTSTREAM_OL_GUARD

#import <ObjectiveLib/ConnectedOutStream.h>

@class NSMutableData;
@class NSData;

/**
 * @class OLDataOutStream DataOutStream.h ObjectiveLib/DataOutStream.h
 *
 * An out stream connected to a memory buffer. This is a concrete connected stream
 * that writes its data to a memory buffer.
 *
 * @sa OLDataInStream
 *
 * @ingroup Streams
 */
@interface OLDataOutStream : OLConnectedOutStream
{
@protected
    /**
     * The data object that contains the memory buffer
     */
    NSMutableData*  buffer;

    /**
     * The allocator used to allocate the data object
     */
    OLAllocator*    allocator;
}

/**
 * Create and return a new stream. The new stream uses the default memory allocator.
 * If the platform supports autorelease, then the stream will be autoreleased before
 * being returned.
 *
 * @return a reference to the newly created stream
 */
+ (id) stream;

/**
 * @name Initializers and Deallocators
 */
/* @{ */
/**
 * Initialize the stream. The stream uses the default memory allocator to manage its
 * underlying data object.
 *
 * @return a reference to this stream
 */
- (id) init;

/**
 * Initialize the stream. The stream uses the memory allocator @a alloc to manage its
 * underlying data object.
 *
 * @param alloc the memory allocator that the stream will use
 * @return a reference to this stream
 */
- (id) initWithAllocator: (OLAllocator*)alloc;

/**
 * Finalize the stream and deallocate any allocated memory.
 */
- (void) dealloc;
/* @} */

/**
 * Return the data object. This object contains all the bytes that have up the current
 * moment been written to the stream.
 *
 * @return the data object containing the stream's memory buffer
 */
- (NSData*) data;

/**
 * Write a sequence of bytes to the stream. Bytes are copied into the memory buffer
 * which grows as needed.
 *
 * @param bytes the bytes to write
 * @param count the number of bytes to write
 * @return the number of bytes written
 */
- (unsigned) writeBytes: (const uint8_t*)bytes count: (unsigned)count;

@end

#endif
