2006-04-18 17:28:38 +00:00
|
|
|
/* <!-- copyright */
|
|
|
|
/*
|
2006-09-21 15:31:24 +00:00
|
|
|
* aria2 - The high speed download utility
|
2006-04-18 17:28:38 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* This program 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2010-01-05 16:01:46 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2006-09-21 15:31:24 +00:00
|
|
|
*
|
|
|
|
* In addition, as a special exception, the copyright holders give
|
|
|
|
* permission to link the code of portions of this program with the
|
|
|
|
* OpenSSL library under certain conditions as described in each
|
|
|
|
* individual source file, and distribute linked combinations
|
|
|
|
* including the two.
|
|
|
|
* You must obey the GNU General Public License in all respects
|
|
|
|
* for all of the code used other than OpenSSL. If you modify
|
|
|
|
* file(s) with this exception, you may extend this exception to your
|
|
|
|
* version of the file(s), but you are not obligated to do so. If you
|
|
|
|
* do not wish to do so, delete this exception statement from your
|
|
|
|
* version. If you delete this exception statement from all source
|
|
|
|
* files in the program, then also delete it here.
|
2006-04-18 17:28:38 +00:00
|
|
|
*/
|
|
|
|
/* copyright --> */
|
|
|
|
#include "ByteArrayDiskWriter.h"
|
2008-05-13 14:15:23 +00:00
|
|
|
#include "A2STR.h"
|
2011-08-10 14:58:49 +00:00
|
|
|
#include "DlAbortEx.h"
|
|
|
|
#include "fmt.h"
|
2006-04-18 17:28:38 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
namespace aria2 {
|
|
|
|
|
2011-08-10 14:58:49 +00:00
|
|
|
ByteArrayDiskWriter::ByteArrayDiskWriter(size_t maxLength)
|
|
|
|
: maxLength_(maxLength)
|
|
|
|
{}
|
2006-04-18 17:28:38 +00:00
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
ByteArrayDiskWriter::~ByteArrayDiskWriter() {}
|
2006-04-18 17:28:38 +00:00
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
void ByteArrayDiskWriter::clear()
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
buf_.str(A2STR::NIL);
|
2006-04-18 17:28:38 +00:00
|
|
|
}
|
|
|
|
|
2012-07-23 12:36:24 +00:00
|
|
|
void ByteArrayDiskWriter::initAndOpenFile(int64_t totalLength)
|
2007-10-11 16:58:24 +00:00
|
|
|
{
|
2006-04-18 17:28:38 +00:00
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2012-07-23 12:36:24 +00:00
|
|
|
void ByteArrayDiskWriter::openFile(int64_t totalLength) {}
|
2007-01-08 00:13:25 +00:00
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
void ByteArrayDiskWriter::closeFile() {}
|
2006-04-18 17:28:38 +00:00
|
|
|
|
2012-07-23 12:36:24 +00:00
|
|
|
void ByteArrayDiskWriter::openExistingFile(int64_t totalLength)
|
2007-10-11 16:58:24 +00:00
|
|
|
{
|
2009-05-04 07:50:38 +00:00
|
|
|
openFile();
|
2006-04-18 17:28:38 +00:00
|
|
|
}
|
|
|
|
|
2012-07-23 12:36:24 +00:00
|
|
|
void ByteArrayDiskWriter::writeData(const unsigned char* data,
|
|
|
|
size_t dataLength, int64_t offset)
|
2007-10-11 16:58:24 +00:00
|
|
|
{
|
2012-07-23 12:36:24 +00:00
|
|
|
if(offset+dataLength > maxLength_) {
|
2011-08-10 14:58:49 +00:00
|
|
|
throw DL_ABORT_EX(fmt("Maximum length(%lu) exceeded.",
|
|
|
|
static_cast<unsigned long>(maxLength_)));
|
|
|
|
}
|
2012-07-23 12:36:24 +00:00
|
|
|
int64_t length = size();
|
|
|
|
if(length < offset) {
|
2010-06-21 13:51:56 +00:00
|
|
|
buf_.seekp(length, std::ios::beg);
|
2012-07-23 12:36:24 +00:00
|
|
|
for(int64_t i = length; i < offset; ++i) {
|
2010-06-21 13:51:56 +00:00
|
|
|
buf_.put('\0');
|
2007-06-09 10:06:53 +00:00
|
|
|
}
|
|
|
|
} else {
|
2012-07-23 12:36:24 +00:00
|
|
|
buf_.seekp(offset, std::ios::beg);
|
2006-04-18 17:28:38 +00:00
|
|
|
}
|
2010-06-21 13:51:56 +00:00
|
|
|
buf_.write(reinterpret_cast<const char*>(data), dataLength);
|
2006-04-18 17:28:38 +00:00
|
|
|
}
|
|
|
|
|
2012-07-23 12:36:24 +00:00
|
|
|
ssize_t ByteArrayDiskWriter::readData(unsigned char* data, size_t len,
|
|
|
|
int64_t offset)
|
2007-10-11 16:58:24 +00:00
|
|
|
{
|
2012-07-23 12:36:24 +00:00
|
|
|
buf_.seekg(offset, std::ios::beg);
|
2010-06-21 13:51:56 +00:00
|
|
|
buf_.read(reinterpret_cast<char*>(data), len);
|
|
|
|
buf_.clear();
|
|
|
|
return buf_.gcount();
|
2006-04-18 17:28:38 +00:00
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
|
2012-07-23 12:36:24 +00:00
|
|
|
int64_t ByteArrayDiskWriter::size()
|
2008-11-17 12:07:24 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
buf_.seekg(0, std::ios::end);
|
|
|
|
buf_.clear();
|
|
|
|
return buf_.tellg();
|
2008-11-17 12:07:24 +00:00
|
|
|
}
|
|
|
|
|
2010-11-14 07:17:55 +00:00
|
|
|
void ByteArrayDiskWriter::setString(const std::string& s)
|
|
|
|
{
|
|
|
|
buf_.str(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ByteArrayDiskWriter::getString() const
|
|
|
|
{
|
|
|
|
return buf_.str();
|
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
} // namespace aria2
|