@@ -49,17 +49,24 @@ function Buffer(subject, encoding) {
49
49
if ( ! util . isBuffer ( this ) )
50
50
return new Buffer ( subject , encoding ) ;
51
51
52
- if ( util . isNumber ( subject ) )
52
+ if ( util . isNumber ( subject ) ) {
53
53
this . length = subject > 0 ? subject >>> 0 : 0 ;
54
- else if ( util . isString ( subject ) )
55
- this . length = Buffer . byteLength ( subject , encoding = encoding || 'utf8' ) ;
56
- else if ( util . isObject ( subject ) ) {
54
+
55
+ } else if ( util . isString ( subject ) ) {
56
+ if ( ! util . isString ( encoding ) || encoding . length === 0 )
57
+ encoding = 'utf8' ;
58
+ this . length = Buffer . byteLength ( subject , encoding ) ;
59
+
60
+ // Handle Arrays, Buffers, Uint8Arrays or JSON.
61
+ } else if ( util . isObject ( subject ) ) {
57
62
if ( subject . type === 'Buffer' && util . isArray ( subject . data ) )
58
63
subject = subject . data ;
59
-
64
+ // Must use floor() because array length may be > kMaxLength.
60
65
this . length = + subject . length > 0 ? Math . floor ( + subject . length ) : 0 ;
61
- } else
66
+
67
+ } else {
62
68
throw new TypeError ( 'must start with number, buffer, array or string' ) ;
69
+ }
63
70
64
71
if ( this . length > kMaxLength ) {
65
72
throw new RangeError ( 'Attempt to allocate Buffer larger than maximum ' +
@@ -79,25 +86,31 @@ function Buffer(subject, encoding) {
79
86
alloc ( this , this . length ) ;
80
87
}
81
88
82
- if ( ! util . isNumber ( subject ) ) {
83
- if ( util . isString ( subject ) ) {
84
- // In the case of base64 it's possible that the size of the buffer
85
- // allocated was slightly too large. In this case we need to rewrite
86
- // the length to the actual length written.
87
- var len = this . write ( subject , encoding ) ;
88
-
89
- // Buffer was truncated after decode, realloc internal ExternalArray
90
- if ( len !== this . length ) {
91
- this . length = len ;
92
- truncate ( this , this . length ) ;
93
- }
94
- } else {
95
- if ( util . isBuffer ( subject ) )
96
- subject . copy ( this , 0 , 0 , this . length ) ;
97
- else if ( util . isNumber ( subject . length ) || util . isArray ( subject ) )
98
- for ( var i = 0 ; i < this . length ; i ++ )
99
- this [ i ] = subject [ i ] ;
89
+ if ( util . isNumber ( subject ) ) {
90
+ return ;
91
+ }
92
+
93
+ if ( util . isString ( subject ) ) {
94
+ // In the case of base64 it's possible that the size of the buffer
95
+ // allocated was slightly too large. In this case we need to rewrite
96
+ // the length to the actual length written.
97
+ var len = this . write ( subject , encoding ) ;
98
+ // Buffer was truncated after decode, realloc internal ExternalArray
99
+ if ( len !== this . length ) {
100
+ var prevLen = this . length ;
101
+ this . length = len ;
102
+ truncate ( this , this . length ) ;
103
+ poolOffset -= ( prevLen - len ) ;
100
104
}
105
+
106
+ } else if ( util . isBuffer ( subject ) ) {
107
+ subject . copy ( this , 0 , 0 , this . length ) ;
108
+
109
+ } else if ( util . isNumber ( subject . length ) || util . isArray ( subject ) ) {
110
+ // Really crappy way to handle Uint8Arrays, but V8 doesn't give a simple
111
+ // way to access the data from the C++ API.
112
+ for ( var i = 0 ; i < this . length ; i ++ )
113
+ this [ i ] = subject [ i ] ;
101
114
}
102
115
}
103
116
0 commit comments