+ inline void writeByteArray(const void *buf, size_t length) {
+ os.put(GLOBAL);
+ os << "__builtin__\nbytearray\n";
+ os.put(BINPUT);
+ os.put(1);
+ writeString(static_cast<const char *>(buf), length);
+ os.put(TUPLE1);
+ os.put(REDUCE);
+ }
+
+protected:
+ inline void putInt16(uint16_t i) {
+ os.put( i & 0xff);
+ os.put( i >> 8 );
+ }
+
+ inline void putInt32(uint32_t i) {
+ os.put( i & 0xff);
+ os.put((i >> 8) & 0xff);
+ os.put((i >> 16) & 0xff);
+ os.put( i >> 24 );
+ }
+
+ template< class T >
+ inline void writeLong(T l) {
+ os.put(LONG1);
+
+ if (l == 0) {
+ os.put(0);
+ return;
+ }
+
+ // Same as l >> (8 * sizeof l), but without the warnings
+ T sign = l < 0 ? ~0 : 0;
+
+ T sl = l;
+ unsigned c = 0;
+ do {
+ ++c;
+ } while (sl >>= 8 != sign);
+
+ // Add an extra byte if sign bit doesn't match
+ if (((l >> (8 * c - 1)) & 1) != ((l >> (8 * sizeof l - 1)) & 1)) {
+ ++c;
+ }
+ os.put(c);
+
+ for (unsigned i = 0; i < c; ++ i) {
+ os.put(l & 0xff);
+ l >>= 8;
+ }