1 | /** |
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
3 | * or more contributor license agreements. See the NOTICE file |
---|
4 | * distributed with this work for additional information |
---|
5 | * regarding copyright ownership. The ASF licenses this file |
---|
6 | * to you under the Apache License, Version 2.0 (the |
---|
7 | * "License"); you may not use this file except in compliance |
---|
8 | * with the License. You may obtain a copy of the License at |
---|
9 | * |
---|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
11 | * |
---|
12 | * Unless required by applicable law or agreed to in writing, software |
---|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
15 | * See the License for the specific language governing permissions and |
---|
16 | * limitations under the License. |
---|
17 | */ |
---|
18 | #ifndef HADOOP_SERIAL_UTILS_HH |
---|
19 | #define HADOOP_SERIAL_UTILS_HH |
---|
20 | |
---|
21 | #include <string> |
---|
22 | |
---|
23 | namespace HadoopUtils { |
---|
24 | |
---|
25 | /** |
---|
26 | * A simple exception class that records a message for the user. |
---|
27 | */ |
---|
28 | class Error { |
---|
29 | private: |
---|
30 | std::string error; |
---|
31 | public: |
---|
32 | |
---|
33 | /** |
---|
34 | * Create an error object with the given message. |
---|
35 | */ |
---|
36 | Error(const std::string& msg); |
---|
37 | |
---|
38 | /** |
---|
39 | * Construct an error object with the given message that was created on |
---|
40 | * the given file, line, and functino. |
---|
41 | */ |
---|
42 | Error(const std::string& msg, |
---|
43 | const std::string& file, int line, const std::string& function); |
---|
44 | |
---|
45 | /** |
---|
46 | * Get the error message. |
---|
47 | */ |
---|
48 | const std::string& getMessage() const; |
---|
49 | }; |
---|
50 | |
---|
51 | /** |
---|
52 | * Check to make sure that the condition is true, and throw an exception |
---|
53 | * if it is not. The exception will contain the message and a description |
---|
54 | * of the source location. |
---|
55 | */ |
---|
56 | #define HADOOP_ASSERT(CONDITION, MESSAGE) \ |
---|
57 | { \ |
---|
58 | if (!(CONDITION)) { \ |
---|
59 | throw HadoopUtils::Error((MESSAGE), __FILE__, __LINE__, \ |
---|
60 | __PRETTY_FUNCTION__); \ |
---|
61 | } \ |
---|
62 | } |
---|
63 | |
---|
64 | /** |
---|
65 | * An interface for an input stream. |
---|
66 | */ |
---|
67 | class InStream { |
---|
68 | public: |
---|
69 | /** |
---|
70 | * Reads len bytes from the stream into the buffer. |
---|
71 | * @param buf the buffer to read into |
---|
72 | * @param buflen the length of the buffer |
---|
73 | * @throws Error if there are problems reading |
---|
74 | */ |
---|
75 | virtual void read(void *buf, size_t len) = 0; |
---|
76 | virtual ~InStream() {} |
---|
77 | }; |
---|
78 | |
---|
79 | /** |
---|
80 | * An interface for an output stream. |
---|
81 | */ |
---|
82 | class OutStream { |
---|
83 | public: |
---|
84 | /** |
---|
85 | * Write the given buffer to the stream. |
---|
86 | * @param buf the data to write |
---|
87 | * @param len the number of bytes to write |
---|
88 | * @throws Error if there are problems writing |
---|
89 | */ |
---|
90 | virtual void write(const void *buf, size_t len) = 0; |
---|
91 | /** |
---|
92 | * Flush the data to the underlying store. |
---|
93 | */ |
---|
94 | virtual void flush() = 0; |
---|
95 | virtual ~OutStream() {} |
---|
96 | }; |
---|
97 | |
---|
98 | /** |
---|
99 | * A class to read a file as a stream. |
---|
100 | */ |
---|
101 | class FileInStream : public InStream { |
---|
102 | public: |
---|
103 | FileInStream(); |
---|
104 | bool open(const std::string& name); |
---|
105 | bool open(FILE* file); |
---|
106 | void read(void *buf, size_t buflen); |
---|
107 | bool skip(size_t nbytes); |
---|
108 | bool close(); |
---|
109 | virtual ~FileInStream(); |
---|
110 | private: |
---|
111 | /** |
---|
112 | * The file to write to. |
---|
113 | */ |
---|
114 | FILE *mFile; |
---|
115 | /** |
---|
116 | * Does is this class responsible for closing the FILE*? |
---|
117 | */ |
---|
118 | bool isOwned; |
---|
119 | }; |
---|
120 | |
---|
121 | /** |
---|
122 | * A class to write a stream to a file. |
---|
123 | */ |
---|
124 | class FileOutStream: public OutStream { |
---|
125 | public: |
---|
126 | |
---|
127 | /** |
---|
128 | * Create a stream that isn't bound to anything. |
---|
129 | */ |
---|
130 | FileOutStream(); |
---|
131 | |
---|
132 | /** |
---|
133 | * Create the given file, potentially overwriting an existing file. |
---|
134 | */ |
---|
135 | bool open(const std::string& name, bool overwrite); |
---|
136 | bool open(FILE* file); |
---|
137 | void write(const void* buf, size_t len); |
---|
138 | bool advance(size_t nbytes); |
---|
139 | void flush(); |
---|
140 | bool close(); |
---|
141 | virtual ~FileOutStream(); |
---|
142 | private: |
---|
143 | FILE *mFile; |
---|
144 | bool isOwned; |
---|
145 | }; |
---|
146 | |
---|
147 | /** |
---|
148 | * A stream that reads from a string. |
---|
149 | */ |
---|
150 | class StringInStream: public InStream { |
---|
151 | public: |
---|
152 | StringInStream(const std::string& str); |
---|
153 | virtual void read(void *buf, size_t buflen); |
---|
154 | private: |
---|
155 | const std::string& buffer; |
---|
156 | std::string::const_iterator itr; |
---|
157 | }; |
---|
158 | |
---|
159 | void serializeInt(int32_t t, OutStream& stream); |
---|
160 | int32_t deserializeInt(InStream& stream); |
---|
161 | void serializeLong(int64_t t, OutStream& stream); |
---|
162 | int64_t deserializeLong(InStream& stream); |
---|
163 | void serializeFloat(float t, OutStream& stream); |
---|
164 | float deserializeFloat(InStream& stream); |
---|
165 | void serializeString(const std::string& t, OutStream& stream); |
---|
166 | void deserializeString(std::string& t, InStream& stream); |
---|
167 | } |
---|
168 | |
---|
169 | #endif |
---|