Logo Search packages:      
Sourcecode: darkice version File versions

unsigned int BufferedSink::store ( const void *  buffer,
unsigned int  bufferSize 
) throw ( Exception ) [protected]

Store data in the internal buffer. If there is not enough space, discard all in the buffer and the beginning of the supplied buffer if needed.

Parameters:
buffer the data to store.
bufferSize the amount of data to store in bytes.
Returns:
number of bytes really stored.

Definition at line 164 of file BufferedSink.cpp.

References buffer, bufferEnd, bufferSize, chunkSize, inp, outp, slidePointer(), and updatePeak().

Referenced by write().

{
    const unsigned char   * buf;
    unsigned int            size;
    unsigned int            i;
    unsigned char         * oldInp;

    if ( !buffer ) {
        throw Exception( __FILE__, __LINE__, "buffer is null");
    }

    if ( !bufferSize ) {
        return 0;
    }

    oldInp = inp;
    buf    = (const unsigned char *) buffer;
    
    // adjust so it is a multiple of chunkSize
    bufferSize -= bufferSize % chunkSize;

    // cut the front of the supplied buffer if it wouldn't fit
    if ( bufferSize > this->bufferSize ) {
        size  = this->bufferSize - 1;
        size -= size % chunkSize;       // keep it a multiple of chunkSize
        buf  += bufferSize - size;
    } else {
        size = bufferSize;
    }

    // copy the data into the buffer
    i = bufferEnd - inp;
    if ( (i % chunkSize) != 0 ) {
        throw Exception( __FILE__, __LINE__, "copy quantity not aligned", i);
    }

    if ( size <= i ) {
        // the place between inp and bufferEnd is
        // big enough to hold the data
        
        memcpy( inp, buf, size);
        inp = slidePointer( inp, size);

        // adjust outp, lose the data that was overwritten, if any
        if ( outp > oldInp && outp <= inp ) {
            outp = slidePointer( inp, chunkSize);
        }

    } else {
        // the place between inp and bufferEnd is not
        // big enough to hold the data
        // writing will take place in two turns, once from
        // inp -> bufferEnd, then from buffer ->

        memcpy( inp, buf, i);
        i = size - i;
        if ( (i % chunkSize) != 0 ) {
            throw Exception(__FILE__, __LINE__, "copy quantity not aligned", i);
        }
        memcpy( this->buffer, buf, i);
        inp = slidePointer( this->buffer, i);
        
        // adjust outp, lose the data that was overwritten, if any
        if ( outp <= oldInp ) {
            if ( outp < inp ) {
                outp = slidePointer( inp, chunkSize);
            }
        } else {
            outp = slidePointer( inp, chunkSize);
        }
    }

    updatePeak();

    if ( ((inp - this->buffer) % chunkSize) != 0 ) {
        throw Exception( __FILE__, __LINE__,
                         "inp not aligned", inp - this->buffer);
    }
    if ( ((outp - this->buffer) % chunkSize) != 0 ) {
        throw Exception( __FILE__, __LINE__,
                         "outp not aligned", outp - this->buffer);
    }

    return size;
}


Generated by  Doxygen 1.6.0   Back to index