1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
/*
* bytestream.cpp
*
* (c) 2003,2008 by Jeremy Bowman <jmbowman@alum.mit.edu>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
/** @file bytestream.cpp
* Source file for ByteStream
*/
#include <qstring.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bytestream.h"
/**
* Constructor for reading.
*
* @param data The byte array to read from
*/
ByteStream::ByteStream(const QByteArray &data) : content(data), location(0),
writing(false)
{
}
/**
* Constructor for writing.
*/
ByteStream::ByteStream() : location(0), writing(true)
{
}
/**
* Destructor.
*/
ByteStream::~ByteStream()
{
}
/**
* Fetch some bytes sequentially.
*
* @param buffer A pointer to the location to begin copying bytes to
* @param length The maximum number of bytes to copy
* @return The number of bytes actually copied
*/
int ByteStream::Read(void *buffer, int length)
{
if (content.size() == 0) {
return 0;
}
int size = length;
if (location + length > content.size()) {
size = content.size() - location;
}
memcpy(buffer, content.data() + location, size);
location += size;
return size;
}
/**
* Store some bytes sequentially.
*
* @param buffer A pointer to the byte array to copy from
* @param length The number of bytes to copy
* @return True if the bytes were successfully stored
*/
bool ByteStream::Write(const void *buffer, int length)
{
content.append(QByteArray((const char*)buffer, length));
location += length;
return true;
}
/**
* Get the written content.
*
* @return The written content
*/
QByteArray ByteStream::getContent()
{
return content;
}
|