new Buffer(obj)
Most data in vert.x is shuffled around using buffers.
A Buffer represents a sequence of zero or more bytes that can be written to
or read from, and which expands automatically as necessary to accomodate any
bytes written to it. You can perhaps think of a buffer as smart byte array.
The methods documented for the Java Buffer objects
are applicable to Javascript Buffer instances as well.
Parameters:
| Name | Type | Argument | Description |
|---|---|---|---|
obj |
string | number | undefined |
<optional> |
The paramater from which the buffer will be created. If a string is given it create a new Buffer which contains the string. If a number is given it will create a new Buffer which has the given initial size. if no parameter is defined a new empty Buffer will be created. |
- Source:
- See:
Examples
var Buffer = require('vertx/buffer');
var buff = new Buffer('Hello!');
Creating Buffers
var Buffer = require('vertx/buffer');
// Create a buffer from a string with UTF-8 encoding (the default)
var buff = new Buffer('Now is the winter of our discontent made glorioius summer');
// Create a buffer from a string and specify an encoding
buff = new Buffer('Too hot, too hot!', 'UTF-16');
// etc etc
// TODO: Finish these examples
Methods
-
appendBuffer(buf, offset, length) → {module:vertx/buffer~Buffer}
-
Append the contents of the provided buffer to this buffer.
Parameters:
Name Type Argument Description bufmodule:vertx/buffer~Buffer a buffer offsetnumber <optional>
the offset in the provided buffer from which point bytes should be read. Defaults to 0. lengthnumber <optional>
the number of bytes to read from the provided buffer. Defaults to buf.length() - offset. - Source:
Returns:
this -
appendByte(b) → {module:vertx/buffer~Buffer}
-
Append to the buffer.
Parameters:
Name Type Description bnumber a valid signed 8 bit integer - Source:
Returns:
this -
appendBytes(b, o, l) → {module:vertx/buffer~Buffer}
-
Append a byte array to this Buffer.
Parameters:
Name Type Argument Description barray an array of bytes onumber <optional>
offset in the byte array from which to start reading lnumber <optional>
the number of bytes to read from the byte array - Source:
Returns:
this -
appendDouble(d) → {module:vertx/buffer~Buffer}
-
Append to the buffer.
Parameters:
Name Type Description dnumber a valid signed 64 bit double - Source:
Returns:
this -
appendFloat(f) → {module:vertx/buffer~Buffer}
-
Append to the buffer.
Parameters:
Name Type Description fnumber a valid signed 32 bit integer - Source:
Returns:
this -
appendInt(i) → {module:vertx/buffer~Buffer}
-
Append to the buffer.
Parameters:
Name Type Description inumber a valid signed 32 bit integer - Source:
Returns:
this -
appendShort(s) → {module:vertx/buffer~Buffer}
-
Append to the buffer.
Parameters:
Name Type Description snumber a valid signed 16 bit short - Source:
Returns:
this -
appendString(str, enc) → {module:vertx/buffer~Buffer}
-
Append to the buffer.
Parameters:
Name Type Argument Description strstring the string encstring <optional>
the encoding to use or {undefined} if the default should be used. - Source:
Returns:
this -
copy() → {module:vertx/buffer~Buffer}
-
Create a copy of this buffer and its content.
- Source:
Returns:
-
equals(other) → {boolean}
-
Determines if this buffer is equal to the other
Parameters:
Name Type Description othermodule:vertx/buffer~Buffer the buffer to compare to this one - Source:
Returns:
true if the buffers are equal- Type
- boolean
-
getBuffer(start, end) → {module:vertx/buffer~Buffer}
-
Get a {module:vertx/buffer~Buffer} for the given range from the buffer.
Parameters:
Name Type Description startnumber of the range endnumber of the range, exclusive - Source:
Returns:
-
getByte(pos) → {number}
-
Get an unsigned 8 bit integer from the buffer.
Parameters:
Name Type Description posnumber the position - Source:
Returns:
- Type
- number
-
getDouble(pos) → {number}
-
Get a signed 64 bit double from the buffer.
Parameters:
Name Type Description posnumber the position - Source:
Returns:
- Type
- number
-
getFloat(pos) → {number}
-
Get a signed 32 bit float from the buffer.
Parameters:
Name Type Description posnumber the position - Source:
Returns:
- Type
- number
-
getInt(pos) → {number}
-
Get a signed 32 bit integer from the buffer.
Parameters:
Name Type Description posnumber the position - Source:
Returns:
- Type
- number
-
getShort(pos) → {number}
-
Get a signed 16 bit short from the buffer.
Parameters:
Name Type Description posnumber the position - Source:
Returns:
- Type
- number
-
getString(start, end, enc) → {string}
-
Get a string for the given range from the buffer.
Parameters:
Name Type Argument Description startnumber of the range endnumber of the range, exclusive encstring <optional>
An optional encoding for the returned string - Source:
Returns:
- Type
- string
-
length() → {number}
-
Get the length of the buffer
- Source:
Returns:
The buffer length. Note this is the actual length of the data in the buffer, not an allocated length. For example,- Type
- number
Example
var b = new Buffer(1024); b.length(); // => 0 b = new Buffer("Hello world!"); b.length(); // => 12 -
setBuffer(position, buffer, offset, length) → {module:vertx/buffer~Buffer}
-
Sets the bytes from the provided buffer onto this buffer.
Parameters:
Name Type Argument Description positionnumber the position on which to set the buffer buffermodule:vertx/buffer~Buffer the buffer to read from offsetnumber <optional>
the point at which bytes should be read from the provided buffer. Defaults to 0. lengthnumber <optional>
the number of bytes to read from the provided buffer. Defaults to buffer.length() - offset. - Source:
Returns:
this -
setByte(pos, b) → {module:vertx/buffer~Buffer}
-
Set on the buffer at the given position.
Parameters:
Name Type Description posnumber the position on which to set b bnumber a valid signed 8 bit integer - Source:
Returns:
this -
setBytes(p, b, o, l) → {module:vertx/buffer~Buffer}
-
Sets a byte array on this Buffer at the given position.
Parameters:
Name Type Description pnumber the position to begin writing in this buffer barray an array of bytes onumber offset in the byte array from which to start reading lnumber the number of bytes to read from the byte array - Source:
Returns:
this -
setDouble(pos, d) → {module:vertx/buffer~Buffer}
-
Set on the buffer at the given position.
Parameters:
Name Type Description posnumber the position on which to set d dnumber a valid signed 64 bit double - Source:
Returns:
this -
setFloat(pos, f) → {module:vertx/buffer~Buffer}
-
Set on the buffer at the given position.
Parameters:
Name Type Description posnumber the position on which to set f fnumber a valid signed 32 bit integer - Source:
Returns:
this -
setInt(pos, i) → {module:vertx/buffer~Buffer}
-
Set on the buffer at the given position.
Parameters:
Name Type Description posnumber the position on which to set i inumber a valid signed 32 bit integer - Source:
Returns:
this -
setShort(pos, s) → {module:vertx/buffer~Buffer}
-
Set on the buffer at the given position.
Parameters:
Name Type Description posnumber the position on which to set s snumber a valid signed 16 bit short - Source:
Returns:
this -
setString(pos, str, enc) → {module:vertx/buffer~Buffer}
-
Set a string in the buffer at the given position.
Parameters:
Name Type Argument Description posnumber the position on which to set str strstring the string encstring <optional>
an optional encoding to use - Source:
Returns:
this -
toString(encoding) → {string}
-
Returns this buffer as a string. The default encoding is UTF-8.
Parameters:
Name Type Argument Description encodingstring <optional>
An optional encoding - Source:
Returns:
This buffer as a string- Type
- string