working on it ...

Filters

Explore Public Snippets

Sort by

Found 2,950 snippets matching: writer

    public by sTiLL-iLL modified Dec 27, 2014  307497  0  5  14

    my nonblocking, threadsafe file writer/appender for nodejs. works great with clustered servers, and child processes that write to shared or static file-stores

    my nonblocking, threadsafe file writer/appender for nodejs. works great with clustered servers, and child processes that write to shared or static file-stores: scribbles.js
    // scribbles.js a nonblocking, threadsafe file writer/appender for nodejs
    
    var fs = require('fs'),
    	pth = require('path'),
    	cueMngr = {};
    
    function Scribbles(fileNm) {
    	this.handle = fileNm;
    	this.canWrite = false;
    	this.actionsRoster = [];
    };
    
    var scribbles = Scribbles.prototype;
    
    scribbles.action = function (err, dta, actionCue) {
    	if (err) {
    		throw err;
    	}
    	return actionCue();
    }
    
    scribbles.assign = function (func) {
    	this.action = func;
    	return this;
    }
    
    scribbles.scribble = function (dta, func) {
    	if (this.canWrite) {
    		this.actionCue = dta;
    		if (func) {
    			this.actionsRoster.push(func);
    		}
    	} 
    	else {
    		this.canWrite = true;
    		var slf = this,
    			taskProxy = {};
    		fs.appendFile(this.handle, dta, function (err) {
    			function actionCue() {
    				slf.canWrite = false;
    				if (slf.actionCue) {
    					var dta = slf.actionCue;
    					slf.scribble(dta);
    					slf.actionCue = null;
    				}
    			}
    			slf.action(err, dta, actionCue);
    			while (taskProxy = slf.actionsRoster.shift()) {
    				return taskProxy(err);
    			}
    			if (func) {
    				return func(err);
    			}
    		});
    	}
    	return this;
    };
    
    module.exports = function (fil) {
    	var nm = pth.resolve(fil);
    	return (cueMngr[nm] = cueMngr[nm] || new Scribbles(fil));
    }
    
    

    public by pplant modified Jul 28, 2014  3289  3  6  1

    Read/Write File in Java

    An easy way to read a file and append text to it
    try {
      File aFile = new File("/Path/to/File/file_name.txt");
      
      //Set parameter true if you want to append content otherwise false
      BufferedWriter bW = new BufferedWriter(new FileWriter(aFile,true));
                
      String line = "Text to file";
    	bW.write(line);
      bW.newLine();
      bW.flush();
      bW.close();
    } catch (IOException e) {
    	//Put catching code here
    }

    public by sTiLL-iLL @ SniPitz-KND modified Jan 7, 2015  4004  1  8  25

    Scribbles.js

    A non-blocking, thread-safe, async file-writer / appender for node.js.... oh yeah, its also faster than grease. Try iT!
    // scribbles.js
    
    var fs = require('fs'),
    	path = require('path'),
    	scribe = null,
    	canWrite = true,
    	roster = [],
    	scribblesQue = {};
    
    function Scribbles(file) {
      this.fPath = file;
      this.react = null;
    }
    
    scribe = Scribbles.prototype;
    scribe.scribble = function(data, reaction) {
    	var proceed = function () {
    		canWrite = true;
    		if (proceed) {
    			var dta = proceed;
    			proceed = null;
    			scribble(dta);
    		}
    	},
    	react = function(err, data, proceed) {
    		if (err) {
    			throw err;
    		}
    		proceed();
    	},
    	assign = function(reaction) {
    		this.react = reaction;
    		return this;
    	};
    	
    	if (!this.canWrite) {
    		proceed = data;
    		if (reaction) {
    			this.roster.push(reaction);
    		}
    		return this;
    	}
    	else {
    		var lazyfunc = null,
    			slf = this;
    		canWrite = false;
    		fs.appendFile(slf.fPath, data, function(err) {
    			proceed();
    			react(err, data, proceed); 
    			while (lazyfunc = roster.shift()) {
    				lazyfunc(err);
    			}
    			if (reaction){
    				reaction(err);
    			}
    		});
    	}
    	return this;
    };
    
    module.exports = function(fileName) {
      var fnm = path.resolve(fileName);
      return scribblesQue[fnm] = scribblesQue[fnm] || new Scribbles(fileName);
    }
    

    public by sTiLL-iLL modified Dec 27, 2014  6785  9  4  1

    my nonblocking, threadsafe file writer/appender for nodejs. works great with clustered servers, and child processes that write to shared or static file-stores

    my nonblocking, threadsafe file writer/appender for nodejs. works great with clustered servers, and child processes that write to shared or static file-stores: scribbles.js
    // scribbles.js a nonblocking, threadsafe file writer/appender for nodejs
    
    var fs = require('fs'),
    	pth = require('path'),
    	cueMngr = {};
    
    function Scribbles(fileNm) {
    	this.handle = fileNm;
    	this.canWrite = false;
    	this.actionsRoster = [];
    };
    
    var scribbles = Scribbles.prototype;
    
    scribbles.action = function (err, dta, actionCue) {
    	if (err) {
    		throw err;
    	}
    	return actionCue();
    }
    
    scribbles.assign = function (func) {
    	this.action = func;
    	return this;
    }
    
    scribbles.scribble = function (dta, func) {
    	if (this.canWrite) {
    		this.actionCue = dta;
    		if (func) {
    			this.actionsRoster.push(func);
    		}
    	} 
    	else {
    		this.canWrite = true;
    		var slf = this,
    			taskProxy = {};
    		fs.appendFile(this.handle, dta, function (err) {
    			function actionCue() {
    				slf.canWrite = false;
    				if (slf.actionCue) {
    					var dta = slf.actionCue;
    					slf.scribble(dta);
    					slf.actionCue = null;
    				}
    			}
    			slf.action(err, dta, actionCue);
    			while (taskProxy = slf.actionsRoster.shift()) {
    				return taskProxy(err);
    			}
    			if (func) {
    				return func(err);
    			}
    		});
    	}
    	return this;
    };
    
    module.exports = function (fil) {
    	var nm = pth.resolve(fil);
    	return (cueMngr[nm] = cueMngr[nm] || new Scribbles(fil));
    }
    
    

    public by msdn modified Jan 12, 2015  83964  0  6  0

    WriteResponseStartElement: Writes the specified start tag for a response message and associates it with the given namespace and prefix.

    Writes the specified start tag for a response message and associates it with the given namespace and prefix. The instance which this method extends. The namespace prefix of the element. The local name of the element. The namespace URI to associate with the element.
    /// <summary>
    /// Writes the specified start tag for a response message and associates it with the given namespace and prefix.
    /// </summary>
    /// <param name="writer">The <see cref="System.Xml.XmlWriter"/> instance which this method extends.</param>
    /// <param name="prefix">The namespace prefix of the element.</param>
    /// <param name="localName">The local name of the element.</param>
    /// <param name="ns">The namespace URI to associate with the element.</param>
    public static void WriteResponseStartElement(this XmlWriter writer, string prefix, string localName, string ns)
    {
        writer.WriteStartElement(prefix, String.Concat(localName, WellKnownContractMember.ResponseMethodSuffix), ns);
    }

    public by msdn modified Jan 9, 2015  1135  0  7  0

    WriteRST: Writes RequestSecurityToken to writer

    Writes RequestSecurityToken to writer RequestSecurityToken to be written to XML XmlWriter XML is written to
    using System;
    using System.ServiceModel;
    using System.Xml;
    using System.Globalization;
    
    private const string AppliesTo = "AppliesTo";
    
    /// <summary>
    /// Writes RequestSecurityToken to writer
    /// </summary>
    /// <param name="rst">RequestSecurityToken to be written to XML</param>
    /// <param name="writer">XmlWriter XML is written to</param>
    public static void WriteRST( RequestSecurityToken rst, XmlWriter writer )
    {
        if ( rst.AppliesTo != null )
        {
            WriteAppliesTo( writer, rst.AppliesTo );
        }
        if ( !string.IsNullOrEmpty( rst.RequestType ) )
        {
            WriteRequestType( writer, rst.RequestType );
        }
        if ( null != rst.EntropyBytes )
        {
            WriteEntropy( writer, rst.EntropyBytes );
        }
        if ( !string.IsNullOrEmpty( rst.KeyType ) )
        {
            WriteKeyType( writer, rst.KeyType );
        }
        if ( null != rst.KeySizeInBits && rst.KeySizeInBits.HasValue )
        {
            WriteKeySizeInBits( writer, rst.KeySizeInBits.Value );
        }
    }

    public by msdn modified Jan 12, 2015  662  0  5  0

    WriteResponse: Delegate passed into the custom body writer to form the outgoing response.

    Delegate passed into the custom body writer to form the outgoing response.
    using System;
    using System.Diagnostics;
    using System.Net;
    using System.ServiceModel.Channels;
    using System.Xml;
    using Microsoft.Synchronization.Services.Formatters;
    using System.Collections.Specialized;
    
    /// <summary>
    /// Delegate passed into the custom body writer to form the outgoing response.
    /// </summary>
    /// <param name="writer"></param>
    /// <param name="syncWriter"></param>
    private static void WriteResponse(XmlDictionaryWriter writer, SyncWriter syncWriter)
    {
        try
        {
            syncWriter.WriteFeed(writer);
        }
        catch (Exception exception)
        {
            // An exception at this point seems to be unrecoverable but ideally we should not hit exceptions since we are only
            // writing to the XmlDictionaryWriter. 
            SyncServiceTracer.TraceError("Exception in WriteResponse method. Details: {0}", WebUtil.GetExceptionMessage(exception));
        }
    }

    public by msdn modified Jan 12, 2015  83733  0  6  0

    WriteHeader: Writes the header attributes.

    Writes the header attributes. The XmlWriter object where the element is being written. The ManifestInfo object with the header details. Specifies if the manifest will be compressed (supported from version 2).
    /// <summary>
    /// Writes the header attributes.
    /// </summary>
    /// <param name="writer">The XmlWriter object where the element is being written.</param>
    /// <param name="manifestInfo">The ManifestInfo object with the header details.</param>
    /// <param name="compressManifest">Specifies if the manifest will be compressed (supported from version 2).</param>
    private static void WriteHeader(XmlWriter writer, ManifestInfo manifestInfo, bool compressManifest)
    {
        int minorVersion = compressManifest ? (manifestInfo.MinorVersion < 2 ? 2 : manifestInfo.MinorVersion) : manifestInfo.MinorVersion;
    
        writer.WriteAttributeString("MajorVersion", manifestInfo.MajorVersion.ToString(CultureInfo.InvariantCulture));
        writer.WriteAttributeString("MinorVersion", minorVersion.ToString(CultureInfo.InvariantCulture));
        writer.WriteAttributeString("Duration", "{durationPlaceholder}");
    
        if (manifestInfo.IsLive)
        {
            writer.WriteAttributeString("IsLive", "TRUE");
            writer.WriteAttributeString("LookAheadFragmentCount", manifestInfo.LookAheadFragmentCount.ToString(CultureInfo.InvariantCulture));
            writer.WriteAttributeString("DVRWindowLength", manifestInfo.DvrWindowLength.ToString(CultureInfo.InvariantCulture));
        }
    }

    public by msdn modified Jan 12, 2015  892  0  5  0

    WriteReplicaIdToFile

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.IO;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Text;
    using System.Diagnostics;
    using Microsoft.WindowsAzure.StorageClient;
    using Microsoft.Synchronization;
    using Microsoft.Synchronization.MetadataStorage;
    using Microsoft.Synchronization.SimpleProviders;
    using Microsoft.Synchronization.Files;
    using System.Globalization;
    
    private static void WriteReplicaIdToFile(
        string file, 
        SyncId replicaId
        )
    {
        FileStream fs = new FileStream(file, FileMode.Create);
    
        // Construct a BinaryFormatter and use it to serialize the data to the stream.
        BinaryFormatter formatter = new BinaryFormatter();
        try
        {
            formatter.Serialize(fs, replicaId);
        }
        catch (SerializationException e)
        {
            Console.WriteLine("Failed to serialize replica id to file. Reason: " + e.Message);
            throw;
        }
        finally
        {
            fs.Close();
        }
    }

    public by msdn modified Jan 9, 2015  885  0  6  0

    WriteRequestType

    using System;
    using System.ServiceModel;
    using System.Xml;
    using System.Globalization;
    
    private static void WriteRequestType( XmlWriter writer, string requestType )
    {
        writer.WriteElementString( WSTrust13Constants.Prefix, WSTrust13Constants.ElementNames.RequestType, WSTrust13Constants.NamespaceURI.NamespaceName, requestType );
    }
    • Public Snippets
    • Channels Snippets