-
Notifications
You must be signed in to change notification settings - Fork 0
/
BitWriterStream.cpp
59 lines (49 loc) · 999 Bytes
/
BitWriterStream.cpp
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
/*
* BitWriterStream.cpp
*
* Created on: May 3, 2018
* Author: Yuun
*/
#include "BitWriterStream.h"
using namespace std;
BitWriterStream::BitWriterStream() {
buffer_ = 0;
buffer_size = 0;
outputStream.open("/Users/Yuun/Desktop/assignment2/Assignment2/src/wordcount.txt");
}
BitWriterStream::BitWriterStream(std::string outputfile){
buffer_ = 0;
buffer_size = 0;
outputStream.open(outputfile.c_str());
}
BitWriterStream::~BitWriterStream() {
flush();
outputStream.close();
}
int BitWriterStream::putBit(unsigned int bit) {
if (bit > 1) {
return 0;
}
if (bit == 0 || bit == 1) {
buffer_ = (buffer_ << 1) + bit;
buffer_size++;
}
if (buffer_size == BYTE) {
flush();
}
return 1;
}
int BitWriterStream::putByte(unsigned char byte) {
buffer_ = byte;
buffer_size = BYTE;
flush();
return 1;
}
void BitWriterStream::flush() {
if (buffer_size != 0) {
buffer_ = buffer_ << (8 - buffer_size);
outputStream.put(buffer_);
buffer_size = 0;
buffer_ = 0;
}
}