aboutsummaryrefslogtreecommitdiff
path: root/src/tests/streamstack.cpp
blob: 4a0e128bf1346593cf6dfd589e32cc21f31a052d (plain)
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
94
95
96
97
98
99
100
/*
 * Copyright (C) 2007-2011 Xagasoft, All rights reserved.
 *
 * This file is part of the libbu++ library and is released under the
 * terms of the license contained in the file LICENSE.
 */

#include "bu/streamstack.h"

#include "bu/file.h"
#include "bu/base64.h"
#include "bu/bzip2.h"

#include "bu/sio.h"

#include <time.h>

using namespace Bu;

class DoStuff
{
public:
	DoStuff( Bu::Stream &rStream ) :
		rStream( rStream )
	{
	}

	virtual ~DoStuff()
	{
	}

	void write()
	{
		Bu::String s;
		time_t tNow = time( NULL );
		s = ctime( &tNow );
		long lSize = s.getSize()-1;
		rStream.write( &lSize, sizeof(long) );
		rStream.write( s.getStr(), lSize );
	}

	void read()
	{
		Bu::String s;
		long lSize;
		rStream.read( &lSize, sizeof(long) );
		s.setSize( lSize );
		rStream.read( s.getStr(), lSize );
		sio << "Read str(" << lSize << ") = '" << s << "'" << sio.nl;
	}

private:
	Bu::Stream &rStream;
};

int main()
{
	Bu::StreamStack ss;

	DoStuff ds( ss );

	try
	{
		ds.write();
		sio << "This shouldn't have worked." << sio.nl;
	}
	catch( Bu::ExceptionBase &e )
	{
		sio << "Got exception, this is good:  " << e.what() << sio.nl;
	}

	ss.setStream( new Bu::File("Hello.test", Bu::File::WriteNew ) );

	ds.write();

	ss.pushFilter<Bu::Base64>();

	ds.write();

	ss.pushFilter<Bu::BZip2>();

	ds.write();

	ss.clear();

	ss.setStream( new Bu::File("Hello.test", Bu::File::Read ) );

	ds.read();

	ss.pushFilter<Bu::Base64>();

	ds.read();

	ss.pushFilter<Bu::BZip2>();

	ds.read();

	return 0;
}