Skip to content

Commit

Permalink
v0.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Llamas committed Apr 28, 2012
0 parents commit 1585f00
Show file tree
Hide file tree
Showing 10 changed files with 386 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
doc
manage
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License.

Copyright (c) 2012. Gabriel Llamas.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<a name="start"></a>

Node BufferedWriter
===================

#### Fully configurable buffered writer for node.js ####

[Availability](#availability) | [Compatibility](#compatibility) | [Documentation](#documentation)

Version: 0.0.1

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.

This library allows you to write files using internal buffers, so you don't have to worry about them.

<a name="availability"></a>
#### Availability [](#start) ####

Via npm:

```
npm install buffered-writer
```

Or simply copying `build/buffered-writer.js` into your project's directory and `require()` accordingly.

***

<a name="compatibility"></a>
#### Compatibility [](#start) ####

✔ Node *

***

<a name="documentation"></a>
#### Documentation [](#start) ####

[Reference](https://github.com/Gagle/Node-BufferedWriter/wiki/Reference)
[Examples](https://github.com/Gagle/Node-BufferedWriter/tree/master/examples)
[Change log](https://github.com/Gagle/Node-BufferedWriter/wiki/Change-log)
[MIT License](https://github.com/Gagle/Node-BufferedWriter/blob/master/LICENSE)
1 change: 1 addition & 0 deletions build/buffered-writer.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions build/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "buffered-writer",
"version": "0.0.1",
"description": "Fully configurable buffered writer.",
"keywords": ["buffer", "writer", "line", "write line", "file", "write file", "write text file",
"write binary file", "binary"],
"author": {
"name": "Gabriel Llamas"
},
"repository": {
"type": "git",
"url": "git://github.com/Gagle/Node-BufferedWriter.git"
},
"engines": {
"node": "*"
},
"licenses": [{
"type": "MIT",
"url": "http://www.opensource.org/licenses/mit-license.html"
}],
"main": "buffered-writer"
}
39 changes: 39 additions & 0 deletions examples/writeBytes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
var BufferedWriter = require ("../build/buffered-writer");

//The BufferedWriter truncates the file because append == false
new BufferedWriter ("file")

//From the beginning of the file:
//Writes 0x00, 0x01, 0x02
.write ([0x00, 0x01, 0x02], 0, 3, function (error){
if (error) return console.log (error);

//Writes 0x04
this.write (new Buffer ([0x03, 0x04]), 1, 1, function (error){
if (error) return console.log (error);

//Writes 0x05
this.write (0x05, function (error){
if (error) return console.log (error);

//Closes the writer. A flush is implicitly done.
this.close (function (error){
if (error) return console.log (error);

//The BufferedWriter appends content to the end of the file because append == true
//From the end of the file:
//Writes 0xFF
new BufferedWriter ("file", true).write (0xFF, function (error){
if (error) return console.log (error);

//Closes the writer. A flush is implicitly done.
this.close (function (error){
if (error) console.log (error);

//The file contains: 0x00, 0x01, 0x02, 0x04, 0x05, 0xFF
});
});
});
});
});
});
31 changes: 31 additions & 0 deletions examples/writeText.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var BufferedWriter = require ("../build/buffered-writer");

//The BufferedWriter truncates the file because append == false
new BufferedWriter ("file", "utf8")

//From the beginning of the file:
//Writes "First line"
.write ("First line", function (error){
if (error) console.log (error);

//Writes EOL (OS dependent; \r\n on Windows, otherwise \n)
this.newLine (function (error){
if (error) console.log (error);

//Writes "Second line"
this.write ("Second line", function (error){
if (error) console.log (error);

//Closes the writer. A flush is implicitly done.
this.close (function (error){
if (error) console.log (error);

//The file contains:
/*
First line
Second line
*/
});
});
});
});
205 changes: 205 additions & 0 deletions src/buffered-writer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
/**
* @name BufferedWriter.
* @description Fully configurable buffered writer for node.js.
*
* @author Gabriel Llamas
* @created 27/04/2012
* @modified 28/04/2012
* @version 0.0.1
*/
"use strict";

var EVENTS = require ("events");
var FS = require ("fs");

var BUFFER_SIZE = 16384;
var EOL = process.platform.indexOf ("win") !== -1 ? new Buffer ([0x0D, 0x0A]) : new Buffer ([0x0A]);

var INVALID_BUFFER_SIZE = new Error ("The buffer size must be greater than 0.");

var BufferedWriter = function (fileName, bufferSize, encoding, append){
var argsLen = arguments.length;
var type;
if (argsLen === 1){
bufferSize = BUFFER_SIZE;
encoding = null;
append = false;
}else if (argsLen === 2){
type = typeof bufferSize;
if (type === "string"){
encoding = bufferSize;
bufferSize = BUFFER_SIZE;
append = false;
}else if (type === "boolean"){
append = bufferSize;
bufferSize = BUFFER_SIZE;
encoding = null;
}else{
encoding = null;
append = false;
}
}else if (argsLen === 3){
type = typeof bufferSize;
if (type === "number" && typeof encoding === "boolean"){
append = encoding;
encoding = null;
}else if (type === "string"){
append = encoding;
encoding = bufferSize;
bufferSize = BUFFER_SIZE;
}else{
append = false;
}
}

if (bufferSize < 1) throw INVALID_BUFFER_SIZE;

this._settings = {
encoding: encoding,
bufferSize: bufferSize,
append: append ? "a" : "w"
};

this._fileName = fileName;
this._fd = null;
this._buffer = null;
this._bufferOffset = 0;
};

BufferedWriter.prototype._getAvailableSpace = function (n){
if (n + this._bufferOffset > this._settings.bufferSize){
n = this._settings.bufferSize - this._bufferOffset;
}
return n;
};

BufferedWriter.prototype._open = function (cb){
if (this._fd) return cb (null, this._fd);

var me = this;
FS.open (this._fileName, this._settings.append, function (error, fd){
if (error) return cb (error, null);

me._fd = fd;
me._buffer = new Buffer (me._settings.bufferSize);
cb (null, me._fd);
});
};

BufferedWriter.prototype._write = function (data, offset, length, cb){
var me = this;
this._open (function (error, fd){
if (error){
if (cb) cb (error);
return;
}

var bytes = me._getAvailableSpace (length);
data.copy (me._buffer, me._bufferOffset, offset, offset + bytes);
me._bufferOffset += bytes;
offset += bytes;
length -= bytes;
if (me._bufferOffset === me._settings.bufferSize){
me.flush (function (error){
if (error){
if (cb) cb (error);
}else if (length !== 0){
me._write (data, offset, length, cb);
}
});
}else{
if (cb) cb (null);
}
});
};

BufferedWriter.prototype.close = function (cb){
if (cb) cb = cb.bind (this);
if (!this._fd){
if (cb) cb (null);
return;
}

var close = function (){
FS.close (me._fd, function (error){
me._fd = null;
me._buffer = null;
if (cb) cb (error);
});
};

var me = this;
if (this._bufferOffset !== 0){
this.flush (function (error){
if (error){
if (cb) cb (error);
}else{
close ();
}
});
}else{
close ();
}
};

BufferedWriter.prototype.flush = function (cb){
if (cb) cb = cb.bind (this);
if (!this._fd){
if (cb) cb (null);
return;
}
var me = this;
FS.write (this._fd, this._buffer, 0, this._bufferOffset, null, function (error){
if (error){
if (cb) cb (error);
}else{
me._bufferOffset = 0;
if (cb) cb (null);
}
});
};

BufferedWriter.prototype.newLine = function (cb){
if (cb) cb = cb.bind (this);
this._write (EOL, 0, EOL.length, function (error){
if (cb) cb (error);
});
};

var fixBufferType = function (bw, buffer){
var isArray = function (array){
return Object.prototype.toString.call (array) === "[object Array]";
};

if (buffer instanceof Buffer) return buffer;
if (isArray (buffer)) return new Buffer (buffer);
return new Buffer ([buffer]);
};

BufferedWriter.prototype.write = function (buffer, offset, length, cb){
if (typeof buffer === "string"){
cb = offset;
offset = 0;
length = buffer.length;
buffer = new Buffer (buffer, this._settings.encoding)
}else{
buffer = fixBufferType (this, buffer);
var argsLen = arguments.length;
if (argsLen === 1){
offset = 0;
length = 1;
}else if (argsLen === 2){
cb = offset;
offset = 0;
length = 1;
}
}

if (cb) cb = cb.bind (this);

this._write (buffer, offset, length, function (error){
if (cb) cb (error);
});
};

module.exports = BufferedWriter;
Loading

0 comments on commit 1585f00

Please sign in to comment.