-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathdata_input_stream.js
More file actions
154 lines (139 loc) · 3.95 KB
/
data_input_stream.js
File metadata and controls
154 lines (139 loc) · 3.95 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*!
* node-hbase-client - lib/data_input_stream.js
* Copyright(c) 2013 fengmk2 <fengmk2@gmail.com>
* MIT Licensed
*/
"use strict";
/**
* Module dependencies.
*/
var debug = require('debug')('hbase:data_input_stream');
var Readable = require('readable-stream').Readable;
var Bytes = require('./util/bytes');
var WritableUtils = require('./writable_utils');
function DataInputStream(io) {
this.in = io;
if (typeof io.read !== 'function') {
this.in = new Readable();
this.in.wrap(io);
}
this.bytearr = new Buffer(80);
}
DataInputStream.prototype.read = function (b, callback) {
return this.in.read(b, 0, b.length);
};
DataInputStream.prototype.readBytes = function (size, callback) {
var buf = this.in.read(size);
debug('readBytes: %d size, Got %s, socket total read bytes: %d', size, buf ? 'Buffer' : null, this.in.bytesRead);
if (buf === null) {
return this.in.once('readable', this.readBytes.bind(this, size, callback));
}
callback(null, buf);
};
DataInputStream.prototype.readFields = function (fields, callback, startIndex, data) {
var self = this;
var lastError = null;
data = data || {};
var next = function (index) {
if (index === fields.length) {
return callback(lastError, data);
}
var field = fields[index];
var nextIndex = index + 1;
var value = null;
try {
value = self[field.method]();
} catch (e) {
self.in.emit("error", e);
}
debug('readFields: %s index %d, name: %s, got %s, data %j, socket total read bytes: %d',
field.method, index, field.name, value, data, self.in.bytesRead);
if (value === null) {
// TODO: listeners too much
return self.in.once('readable', self.readFields.bind(self, fields, callback, index, data));
}
data[field.name] = value;
next(nextIndex);
};
startIndex = startIndex || 0;
next(startIndex);
};
/**
* See the general contract of the <code>readFully</code>
* method of <code>DataInput</code>.
* <p>
* Bytes
* for this operation are read from the contained
* input stream.
*
* @param len the number of bytes to read.
*/
DataInputStream.prototype.readFully = function (len, callback) {
var buf = this.in.read(len);
if (buf === null) {
return this.in.once('readable', this.readFully.bind(this, len, callback));
}
callback(null, buf);
};
/**
* See the general contract of the <code>readBoolean</code>
* method of <code>DataInput</code>.
* <p>
* Bytes for this operation are read from the contained
* input stream.
*
* @return the <code>boolean</code> value read.
*/
DataInputStream.prototype.readBoolean = function () {
var buf = this.in.read(1);
return buf ? buf[0] !== 0 : null;
};
/**
* See the general contract of the <code>readByte</code>
* method of <code>DataInput</code>.
* <p>
* Bytes
* for this operation are read from the contained
* input stream.
*
* @return the next byte of this input stream as a signed 8-bit
* <code>byte</code>.
*/
DataInputStream.prototype.readByte = function () {
var buf = this.in.read(1);
return buf ? buf.readInt8(0) : null;
};
/**
* See the general contract of the <code>readInt</code>
* method of <code>DataInput</code>.
* <p>
* Bytes
* for this operation are read from the contained
* input stream.
*
* @return the next four bytes of this input stream, interpreted as an
* <code>int</code>.
*/
DataInputStream.prototype.readInt = function () {
var buf = this.in.read(4);
return buf ? buf.readInt32BE(0) : null;
};
/**
* See the general contract of the <code>readLong</code>
* method of <code>DataInput</code>.
* <p>
* Bytes
* for this operation are read from the contained
* input stream.
*
* @return the next eight bytes of this input stream, interpreted as a
* <code>long</code>.
*/
DataInputStream.prototype.readLong = function () {
var buf = this.in.read(8);
if (buf === null) {
return buf;
}
return WritableUtils.toLong(buf);
};
module.exports = DataInputStream;