Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

v0.0.1

  • Loading branch information...
commit 1585f00558822005d24b20343599751f88e21d4c 0 parents
@gagle authored
2  .gitignore
@@ -0,0 +1,2 @@
+doc
+manage
21 LICENSE
@@ -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 README.md
@@ -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  build/buffered-writer.js
@@ -0,0 +1 @@
+"use strict";var EVENTS=require("events"),FS=require("fs"),BUFFER_SIZE=16384,EOL=process.platform.indexOf("win")!==-1?new Buffer([13,10]):new Buffer([10]),INVALID_BUFFER_SIZE=new Error("The buffer size must be greater than 0."),BufferedWriter=function(a,b,c,d){var e=arguments.length,f;e===1?(b=BUFFER_SIZE,c=null,d=!1):e===2?(f=typeof b,f==="string"?(c=b,b=BUFFER_SIZE,d=!1):f==="boolean"?(d=b,b=BUFFER_SIZE,c=null):(c=null,d=!1)):e===3&&(f=typeof b,f==="number"&&typeof c=="boolean"?(d=c,c=null):f==="string"?(d=c,c=b,b=BUFFER_SIZE):d=!1);if(b<1)throw INVALID_BUFFER_SIZE;this._settings={encoding:c,bufferSize:b,append:d?"a":"w"},this._fileName=a,this._fd=null,this._buffer=null,this._bufferOffset=0};BufferedWriter.prototype._getAvailableSpace=function(a){return a+this._bufferOffset>this._settings.bufferSize&&(a=this._settings.bufferSize-this._bufferOffset),a},BufferedWriter.prototype._open=function(a){if(this._fd)return a(null,this._fd);var b=this;FS.open(this._fileName,this._settings.append,function(c,d){if(c)return a(c,null);b._fd=d,b._buffer=new Buffer(b._settings.bufferSize),a(null,b._fd)})},BufferedWriter.prototype._write=function(a,b,c,d){var e=this;this._open(function(f,g){if(f){d&&d(f);return}var h=e._getAvailableSpace(c);a.copy(e._buffer,e._bufferOffset,b,b+h),e._bufferOffset+=h,b+=h,c-=h,e._bufferOffset===e._settings.bufferSize?e.flush(function(f){f?d&&d(f):c!==0&&e._write(a,b,c,d)}):d&&d(null)})},BufferedWriter.prototype.close=function(a){a&&(a=a.bind(this));if(!this._fd){a&&a(null);return}var b=function(){FS.close(c._fd,function(b){c._fd=null,c._buffer=null,a&&a(b)})},c=this;this._bufferOffset!==0?this.flush(function(c){c?a&&a(c):b()}):b()},BufferedWriter.prototype.flush=function(a){a&&(a=a.bind(this));if(!this._fd){a&&a(null);return}var b=this;FS.write(this._fd,this._buffer,0,this._bufferOffset,null,function(c){c?a&&a(c):(b._bufferOffset=0,a&&a(null))})},BufferedWriter.prototype.newLine=function(a){a&&(a=a.bind(this)),this._write(EOL,0,EOL.length,function(b){a&&a(b)})};var fixBufferType=function(a,b){var c=function(a){return Object.prototype.toString.call(a)==="[object Array]"};return b instanceof Buffer?b:c(b)?new Buffer(b):new Buffer([b])};BufferedWriter.prototype.write=function(a,b,c,d){if(typeof a=="string")d=b,b=0,c=a.length,a=new Buffer(a,this._settings.encoding);else{a=fixBufferType(this,a);var e=arguments.length;e===1?(b=0,c=1):e===2&&(d=b,b=0,c=1)}d&&(d=d.bind(this)),this._write(a,b,c,function(a){d&&d(a)})},module.exports=BufferedWriter;
22 build/package.json
@@ -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 examples/writeBytes.js
@@ -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 examples/writeText.js
@@ -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 src/buffered-writer.js
@@ -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;
22 src/package.json
@@ -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"
+}
1  test/playground.js
@@ -0,0 +1 @@
+var BufferedWriter = require ("../build/buffered-writer");
Please sign in to comment.
Something went wrong with that request. Please try again.