Restore CZipOutputStream from MP3 prototype

Former-commit-id: 529c359935
This commit is contained in:
Phillip Stephens 2022-12-05 21:30:20 -08:00
parent 914ef4a37c
commit 1e7e5887d2
4 changed files with 83 additions and 4 deletions

View File

@ -608,6 +608,7 @@ LIBS = [
["Kyoto/Streams/CMemoryStreamOut", True], ["Kyoto/Streams/CMemoryStreamOut", True],
["Kyoto/Streams/COutputStream", True], ["Kyoto/Streams/COutputStream", True],
["Kyoto/Streams/CZipInputStream", True], ["Kyoto/Streams/CZipInputStream", True],
["Kyoto/Streams/CZipOutputStream", True],
["Kyoto/Streams/CZipSupport", True], ["Kyoto/Streams/CZipSupport", True],
"Kyoto/CFactoryStore", "Kyoto/CFactoryStore",
"Kyoto/CObjectReference", "Kyoto/CObjectReference",

View File

@ -11,15 +11,14 @@ template < typename T >
void coutput_stream_helper(const T& t, COutputStream& out); void coutput_stream_helper(const T& t, COutputStream& out);
class COutputStream { class COutputStream {
void DoPut(const void* ptr, size_t len);
void DoFlush();
public: public:
COutputStream(int len); COutputStream(int len);
virtual ~COutputStream(); virtual ~COutputStream();
virtual void Write(const void* ptr, size_t len) = 0; virtual void Write(const void* ptr, size_t len) = 0;
void WriteBits(uint val, uint bitCount); void WriteBits(uint val, uint bitCount);
void DoPut(const void* ptr, size_t len);
void DoFlush();
void Flush(); void Flush();
void FlushShiftRegister(); void FlushShiftRegister();
void Put(const void* ptr, size_t len) { void Put(const void* ptr, size_t len) {
@ -48,7 +47,6 @@ public:
*(reinterpret_cast< u8* >(mBufPtr) + mUnwrittenLen++) = c; *(reinterpret_cast< u8* >(mBufPtr) + mUnwrittenLen++) = c;
} }
private: private:
uint mUnwrittenLen; uint mUnwrittenLen;
uint mBufLen; uint mBufLen;

View File

@ -0,0 +1,27 @@
#ifndef _CZIPOUTPUTSTREAM
#define _CZIPOUTPUTSTREAM
#include "Kyoto/Streams/COutputStream.hpp"
#include "rstl/auto_ptr.hpp"
#include "zlib/zlib.h"
class CZipOutputStream : public COutputStream {
public:
CZipOutputStream(COutputStream* out, int level);
~CZipOutputStream();
void Finish();
bool Process(bool v);
void Write(const void* ptr, size_t len);
private:
COutputStream* mOutput;
int mCompressedBytesWritten;
rstl::auto_ptr<z_stream> mZStream;
int mUnk;
};
#endif // _CZIPOUTPUTSTREAM

View File

@ -0,0 +1,53 @@
#include "Kyoto/Streams/CZipOutputStream.hpp"
#include "rstl/math.hpp"
#include "Kyoto/Streams/CZipSupport.hpp"
CZipOutputStream::CZipOutputStream(COutputStream* out, int level)
: COutputStream(1024)
, mOutput(out)
, mCompressedBytesWritten(0)
, mZStream(new z_stream)
, mUnk(0) {
mZStream->zalloc = CZipSupport::Alloc;
mZStream->zfree = CZipSupport::Free;
mZStream->opaque = nullptr;
int useLevel = 9;
if (level < 10) {
useLevel = level;
}
deflateInit(mZStream.get(), level);
}
CZipOutputStream::~CZipOutputStream() {
DoFlush();
while (!Process(true))
;
}
void CZipOutputStream::Write(const void* buf, size_t len) {
mZStream->next_in = (Bytef*)buf;
mZStream->avail_in = len;
while (mZStream->avail_in != 0) {
Process(false);
}
}
bool CZipOutputStream::Process(bool flush) {
Bytef outBuf[1024];
mZStream->avail_out = 1024;
mZStream->next_out = outBuf;
int ret = deflate(mZStream.get(), flush);
/* assertion failure logic */
if (mZStream->avail_out != 1024) {
mOutput->DoPut(outBuf, 1024 - mZStream->avail_out);
mCompressedBytesWritten += 1024 - mZStream->avail_out;
}
return ret == Z_STREAM_END;
}