Permalink
Browse files

v0.1.12

  • Loading branch information...
Gabriel Llamas
Gabriel Llamas committed Oct 9, 2012
1 parent af1a73d commit 0a8842df97ab8a1bbbdc446b376df0fb216ebcb3
Showing with 37 additions and 31 deletions.
  1. +1 −1 README.md
  2. +1 −1 build/buffered-writer.js
  3. +31 −25 src/buffered-writer.js
  4. +4 −4 test/tests/test1.js
View
@@ -7,7 +7,7 @@ Node BufferedWriter
[Show me!](#showme) | [Availability](#availability) | [Compatibility](#compatibility) | [Documentation](#documentation)
-Version: 0.1.11
+Version: 0.1.12
When you need to write a file you typically write the content in little chunks at a time. To avoid multiple calls to the underlying I/O layer you need to use a piece of memory called "buffer", so instead of writting directly to the disk, you write to the buffer and when it is filled the content is written to disk. Doing this you win performance.
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -4,8 +4,8 @@
*
* @author Gabriel Llamas
* @created 27/04/2012
- * @modified 17/08/2012
- * @version 0.1.11
+ * @modified 09/10/2012
+ * @version 0.1.12
*/
"use strict";
@@ -46,23 +46,21 @@ var BufferedWriter = function (fileName, settings){
this._fileName = fileName;
this._stream = null;
this._buffer = null;
- this._bufferOffset = 0;
+ //Marca la primera posicion libre del buffer donde escribir bytes
+ this._offset = 0;
+ this._closing = false;
};
BufferedWriter.prototype = Object.create (EVENTS.EventEmitter.prototype);
BufferedWriter.prototype.constructor = BufferedWriter;
-BufferedWriter.prototype._canWrite = function (n){
- if (n + this._bufferOffset > this._settings.bufferSize){
- n = this._settings.bufferSize - this._bufferOffset;
- }
- return n;
-};
-
BufferedWriter.prototype._flush = function (){
- this._stream.write (new Buffer (this._bufferOffset !== this._settings.bufferSize ?
- this._buffer.slice (0, this._bufferOffset) : this._buffer));
- this._bufferOffset = 0;
+ //Cuando se escribe un buffer en el stream internamente no se crea una copia, solo se guarda
+ //la referencia, por lo que si el buffer se modifica desde fuera internamente tambien
+ //se habra modificado. Es por eso que se instancia un nuevo buffer para escribir datos.
+ this._stream.write (new Buffer (this._offset !== this._settings.bufferSize ?
+ this._buffer.slice (0, this._offset) : this._buffer));
+ this._offset = 0;
};
BufferedWriter.prototype._write = function (data, offset, length){
@@ -76,34 +74,40 @@ BufferedWriter.prototype._write = function (data, offset, length){
if (length === 0) return;
- var bytes = this._canWrite (length);
- data.copy (this._buffer, this._bufferOffset, offset, offset + bytes);
- this._bufferOffset += bytes;
+ var bytes = length + this._offset > this._settings.bufferSize ?
+ this._settings.bufferSize - this._offset :
+ length;
+
+ data.copy (this._buffer, this._offset, offset, offset + bytes);
+ this._offset += bytes;
offset += bytes;
length -= bytes;
- if (this._bufferOffset === this._settings.bufferSize){
+ if (this._offset === this._settings.bufferSize){
this._flush ();
if (length !== 0){
+ //Mientras queden bytes por escribir en el buffer se van haciendo llamadas recursivas
this._write (data, offset, length);
}
}
};
BufferedWriter.prototype.close = function (cb){
- if (!this._stream) return;
+ if (this._closing || !this._stream) return;
+ if (cb) cb = cb.bind (this);
- if (this._bufferOffset !== 0){
+ if (this._offset !== 0){
this._flush ();
}
var me = this;
this._stream.on ("close", function (){
- if (cb) cb.call (this);
+ me._stream = null;
+ me._buffer = null;
+ me._closing = false;
+ if (cb) cb ();
});
this._stream.destroySoon ();
- this._stream = null;
- this._fd = null;
- this._buffer = null;
+ this._closing = true;
};
BufferedWriter.prototype.newLine = function (){
@@ -112,7 +116,7 @@ BufferedWriter.prototype.newLine = function (){
};
BufferedWriter.prototype.touch = function (_append){
- if (this._stream) return this;
+ if (this._closing) return this;
_append = _append || "w";
var me = this;
@@ -128,6 +132,8 @@ BufferedWriter.prototype.touch = function (_append){
};
BufferedWriter.prototype.write = function (buffer, offset, length){
+ if (this._closing) return this;
+
var type = typeof buffer;
if (type === "number"){
offset = 0;
@@ -145,7 +151,7 @@ BufferedWriter.prototype.write = function (buffer, offset, length){
var argsLen = arguments.length;
if (argsLen === 1){
offset = 0;
- length = buffer.length == 0 ? 0 : 1;
+ length = buffer.length === 0 ? 0 : 1;
}
}
View
@@ -1,8 +1,8 @@
var BufferedWriter = require ("../../build/buffered-writer");
-var BufferedReader = require ("buffered-reader");
+var BinaryReader = require ("buffered-reader").BinaryReader;
var writeFile = function (file, cb){
- new BufferedWriter (file, { encoding: "utf8" })
+ new BufferedWriter (file, { encoding: "utf8", bufferSize: 1 })
.on ("error", function (error){
cb (error);
})
@@ -16,8 +16,8 @@ var writeFile = function (file, cb){
};
var readFile = function (file, cb){
- new BufferedReader (file, { encoding: "utf8" })
- .readBytes (11, function (error, bytes, bytesRead){
+ new BinaryReader (file, { encoding: "utf8" })
+ .read (11, function (error, bytes, bytesRead){
if (error) return cb (error);
console.log (bytes.toString ());

0 comments on commit 0a8842d

Please sign in to comment.