@@ -728,9 +728,7 @@ Buffer.prototype.fill = function fill(val, start, end, encoding) {
728
728
Buffer . prototype . write = function ( string , offset , length , encoding ) {
729
729
// Buffer#write(string);
730
730
if ( offset === undefined ) {
731
- encoding = 'utf8' ;
732
- length = this . length ;
733
- offset = 0 ;
731
+ return this . utf8Write ( string , 0 , this . length ) ;
734
732
735
733
// Buffer#write(string, encoding)
736
734
} else if ( length === undefined && typeof offset === 'string' ) {
@@ -743,12 +741,17 @@ Buffer.prototype.write = function(string, offset, length, encoding) {
743
741
offset = offset >>> 0 ;
744
742
if ( isFinite ( length ) ) {
745
743
length = length >>> 0 ;
746
- if ( encoding === undefined )
747
- encoding = 'utf8' ;
748
744
} else {
749
745
encoding = length ;
750
746
length = undefined ;
751
747
}
748
+
749
+ var remaining = this . length - offset ;
750
+ if ( length === undefined || length > remaining )
751
+ length = remaining ;
752
+
753
+ if ( string . length > 0 && ( length < 0 || offset < 0 ) )
754
+ throw new RangeError ( 'Attempt to write outside buffer bounds' ) ;
752
755
} else {
753
756
// if someone is still calling the obsolete form of write(), tell them.
754
757
// we don't want eg buf.write("foo", "utf8", 10) to silently turn into
@@ -757,50 +760,51 @@ Buffer.prototype.write = function(string, offset, length, encoding) {
757
760
'is no longer supported' ) ;
758
761
}
759
762
760
- var remaining = this . length - offset ;
761
- if ( length === undefined || length > remaining )
762
- length = remaining ;
763
-
764
- if ( string . length > 0 && ( length < 0 || offset < 0 ) )
765
- throw new RangeError ( 'Attempt to write outside buffer bounds' ) ;
766
-
767
- if ( ! encoding )
768
- encoding = 'utf8' ;
769
-
770
- var loweredCase = false ;
771
- for ( ; ; ) {
772
- switch ( encoding ) {
773
- case 'hex' :
774
- return this . hexWrite ( string , offset , length ) ;
775
-
776
- case 'utf8' :
777
- case 'utf-8' :
778
- return this . utf8Write ( string , offset , length ) ;
779
-
780
- case 'ascii' :
781
- return this . asciiWrite ( string , offset , length ) ;
782
-
783
- case 'latin1' :
784
- case 'binary' :
763
+ if ( ! encoding ) return this . utf8Write ( string , offset , length ) ;
764
+
765
+ encoding += '' ;
766
+ switch ( encoding . length ) {
767
+ case 4 :
768
+ if ( encoding === 'utf8' ) return this . utf8Write ( string , offset , length ) ;
769
+ if ( encoding === 'ucs2' ) return this . ucs2Write ( string , offset , length ) ;
770
+ encoding = encoding . toLowerCase ( ) ;
771
+ if ( encoding === 'utf8' ) return this . utf8Write ( string , offset , length ) ;
772
+ if ( encoding === 'ucs2' ) return this . ucs2Write ( string , offset , length ) ;
773
+ break ;
774
+ case 5 :
775
+ if ( encoding === 'utf-8' ) return this . utf8Write ( string , offset , length ) ;
776
+ if ( encoding === 'ascii' ) return this . asciiWrite ( string , offset , length ) ;
777
+ if ( encoding === 'ucs-2' ) return this . ucs2Write ( string , offset , length ) ;
778
+ encoding = encoding . toLowerCase ( ) ;
779
+ if ( encoding === 'utf-8' ) return this . utf8Write ( string , offset , length ) ;
780
+ if ( encoding === 'ascii' ) return this . asciiWrite ( string , offset , length ) ;
781
+ if ( encoding === 'ucs-2' ) return this . ucs2Write ( string , offset , length ) ;
782
+ break ;
783
+ case 7 :
784
+ if ( encoding === 'utf16le' || encoding . toLowerCase ( ) === 'utf16le' )
785
+ return this . ucs2Write ( string , offset , length ) ;
786
+ break ;
787
+ case 8 :
788
+ if ( encoding === 'utf-16le' || encoding . toLowerCase ( ) === 'utf-16le' )
789
+ return this . ucs2Write ( string , offset , length ) ;
790
+ break ;
791
+ case 6 :
792
+ if ( encoding === 'latin1' || encoding === 'binary' )
785
793
return this . latin1Write ( string , offset , length ) ;
786
-
787
- case 'base64' :
788
- // Warning: maxLength not taken into account in base64Write
794
+ if ( encoding === 'base64' )
789
795
return this . base64Write ( string , offset , length ) ;
790
-
791
- case 'ucs2' :
792
- case 'ucs-2' :
793
- case 'utf16le' :
794
- case 'utf-16le' :
795
- return this . ucs2Write ( string , offset , length ) ;
796
-
797
- default :
798
- if ( loweredCase )
799
- throw new TypeError ( 'Unknown encoding: ' + encoding ) ;
800
- encoding = ( '' + encoding ) . toLowerCase ( ) ;
801
- loweredCase = true ;
802
- }
796
+ encoding = encoding . toLowerCase ( ) ;
797
+ if ( encoding === 'latin1' || encoding === 'binary' )
798
+ return this . latin1Write ( string , offset , length ) ;
799
+ if ( encoding === 'base64' )
800
+ return this . base64Write ( string , offset , length ) ;
801
+ break ;
802
+ case 3 :
803
+ if ( encoding === 'hex' || encoding . toLowerCase ( ) === 'hex' )
804
+ return this . hexWrite ( string , offset , length ) ;
805
+ break ;
803
806
}
807
+ throw new TypeError ( 'Unknown encoding: ' + encoding ) ;
804
808
} ;
805
809
806
810
0 commit comments