Skip to main content link. Accesskey S
  • Help
  • HCL Logo
  • HCL Notes and Domino Application Development wiki
  • THIS WIKI IS READ-ONLY. Individual names altered for privacy purposes.
  • HCL Forums and Blogs
  • Home
  • Product Documentation
  • Community Articles
  • Learning Center
  • API Documentation
Search
Community Articles > Developing Applications > Developing XPage Applications > Developing XPage Applications for Notes Client > NotesStream sample JavaScript code for XPages
  • Share Show Menu▼
  • Subscribe Show Menu▼

Recent articles by this author

NotesName sample JavaScript code for XPages

Here is sample JavaScript code for the class NotesName. Eventually this sample code will find its way into the documentation. Corrections and comments are appreciated.

NotesMIMEEntity sample JavaScript code for XPages

Here is sample JavaScript code for the class NotesMIMEEntity. Eventually this sample code will find its way into the documentation. Corrections and comments are appreciated.

NotesViewEntryCollection sample JavaScript code for XPages

Here is sample JavaScript code for the class NotesViewEntryCollection. Eventually this sample code will find its way into the documentation. Corrections and comments are appreciated.

NotesViewNavigator sample JavaScript code for XPages

Here is sample JavaScript code for the class NotesViewNavigator. Eventually this sample code will find its way into the documentation. Corrections and comments are appreciated.

NotesViewEntry sample JavaScript code for XPages

Here is sample JavaScript code for the class NotesViewEntry. Eventually this sample code will find its way into the documentation. Corrections and comments are appreciated.
Community articleNotesStream sample JavaScript code for XPages
Added by ~Elizabeth Umkroskiettu | Edited by ~Elizabeth Umkroskiettu on April 15, 2011 | Version 2
  • Actions Show Menu▼
expanded Abstract
collapsed Abstract
Here is sample JavaScript code for the class NotesStream. Eventually this sample code will find its way into the documentation. Corrections and comments are appreciated.
ShowTable of Contents
HideTable of Contents
    • 0.1 Bytes, close
    • 0.2 Charset
    • 0.3 IsEOS, read, write
    • 0.4 IsReadOnly, writeText
    • 0.5 Position
    • 0.6 getContents
    • 0.7 open, truncate, writeText
    • 0.8 readText
    • 0.9 setContents

Bytes, close


This button opens, collects information, and closes the files in a directory.

var filepath:string = requestScope.filepath;
var stream:NotesStream = session.createStream();
var bytes:int = 0;
var files = (new java.io.File(filepath)).listFiles();
for (var i = 0; i < files.length; i++) {
	requestScope.status += "\n" + files[i];
	if (files[i].isFile()) {
		try {
			if (stream.open(files[i].getPath())) {
				bytes += stream.getBytes();
				requestScope.status += ", " + stream.getBytes() + " bytes";
				stream.close();
			} else {
				requestScope.status += ", cannot open";
			}
		} catch(e) {
			requestScope.status += ", cannot open";
		}
	}
}
requestScope.status += "\n" + "Number of files = " + files.length;
requestScope.status += "\n" + "Total bytes = " + bytes;


Charset


This button uses Turkish (ISO-8859-4) characters to create a document. To correctly display the document, your machine must be configured to display Unicode in Notes.

var inPath:string = "c:\\turkish.txt";
var charset:string = "ISO-8859-4";
session.setConvertMIME(false);
var inStream:NotesStream = session.createStream();
if (inStream.open(inPath, charset)) {
	if (inStream.getBytes() > 0) {
		var doc:NotesDocument = database.createDocument();
		doc.replaceItemValue("Form", "main");
		doc.replaceItemValue("subject", inPath);
		var mime:NotesMIMEEntity = doc.createMIMEEntity();
		if (mime != null) {
			mime.setContentFromText(inStream,
			"text/plain; charset=" + charset, NotesMIMEEntity.ENC_NONE);
			mime.encodeContent(NotesMIMEEntity.ENC_QUOTED_PRINTABLE);
			requestScope.status = "Charset = " + mime.getCharset();
		}
		doc.save(true, true);
	} else requstScope.status = "Input file has no content";
	inStream.close();
} else requstScope.status = "Input file open failed";
session.setConvertMIME(true);


IsEOS, read, write


This button makes an exact copy of a file.

var inPath:string = requestScope.filepath;
var n:int = inPath.lastIndexOf(".");
var outPath:string = inPath.left(n) + "Copy" + inPath.right(inPath.length - n);
var inStream:NotesStream = session.createStream();
if (inStream.open(inPath, "binary")) {
	if (inStream.getBytes() > 0) {
		var outStream:NotesStream = session.createStream();
		if (outStream.open(outPath, "binary")) {
			if (!outStream.isReadOnly()) {
				do {
					var buffer = inStream.read(32767);
					outStream.write(buffer);
				} while (!inStream.isEOS());
			} else requestScope.status = "Output file exists and is read-only";
			outStream.close();
		} else requestScope.status = "Output file open failed";
	} else requestScope.status = "Input file has no content";
	inStream.close();
} else requestScope.status = "Input file open failed";


IsReadOnly, writeText


This button writes the subject and body items of the current document to a text file. The button first ensures that the file does not exist as a read-only file.

var filepath:string = database.getFileName();
filepath = "c:\\" + filepath.left(filepath.length - 3) + "txt";
requestScope.status = "Output file: " + filepath;
var stream:NotesStream = session.createStream();
if (stream.open(filepath, "ASCII")) {
	if (stream.isReadOnly()) {
		requestScope.status = filepath + " is read-only";
		return;
	}
	stream.truncate();
	requestScope.status += "\nBytes written: " +
	stream.writeText(currentDocument.getItemValueString("subject"),
	NotesStream.EOL_CRLF);
	if (document1.hasItem("body")) {
		requestScope.status += "\nBytes written: " +
		stream.writeText(document1.getItemValue("body").firstElement());
	} else {
		requestScope.status += "\nBytes written: 0";
	}
	stream.close();
} else {
	requestScope.status = "Output file open failed";
}


Position


This button writes the content of an item to a stream, then positions the stream to the beginning (0) before reading from it.

var doc:NotesDocument = currentDocument.getDocument();
// Create stream and write text of Body item to it
var stream:NotesStream = session.createStream();
stream.writeText(doc.getItemValueString("body"));
// Reset position to beginning and read text
stream.setPosition(0);
requestScope.status = stream.readText();


getContents


This button creates a file from the subject and body content of a document.

var doc:NotesDocument = currentDocument.getDocument();
var outPath:string = "c:\\" + doc.getItemValueString("subject") + ".txt";
var fos = new java.io.FileOutputStream(outPath);
var osw = new java.io.OutputStreamWriter(fos);
var outStream:NotesStream = session.createStream();
outStream.writeText(doc.getItemValueString("body"));
outStream.getContents(osw);
osw.close();
outStream.close();


open, truncate, writeText


This button creates a note collection from the documents in the current database and exports it to a text file as DXL.

var stream:NotesStream = session.createStream();
var filename:string = "c:\\dxl\\";
filename = filename + database.getFileName();
filename = filename.substring(0, filename.length() - 3) + "dxl";
if (stream.open(filename)) {
	requestScope.status = "Opened " + filename;
	stream.truncate();
        
	// Create note collection
	var nc:NotesNoteCollection = database.createNoteCollection(false);
	nc.setSelectDocuments(true);
	nc.buildCollection();
        
	// Export note collection as DXL
	var exporter:NotesDxlExporter = session.createDxlExporter();
	var output:string = exporter.exportDxl(nc);
	stream.writeText(output);
	requestScope.status = "Exported note collection as DXL ";
	stream.close();
} else {
	requestScope.status = "Unable to open " + filename;
}


readText


This button creates a document whose body contains the content of a file.

var inPath:string = requestScope.filepath;
var inStream:NotesStream = session.createStream();
if (inStream.open(inPath, "ASCII")) {
	if (inStream.getBytes() > 0) {
		var doc = database.createDocument();
		doc.replaceItemValue("Form", "main");
		doc.replaceItemValue("subject", inPath);
		doc.replaceItemValue("body", inStream.readText());
		doc.save(true, true);
	} else requestScope.status = "Input file has no content";
	inStream.close();
} else requestScope.status = "Input file open failed";


This button is the same except that it processes the file a line at a time.

var inPath:string = requestScope.filepath;
var inStream:NotesStream = session.createStream();
if (inStream.open(inPath, "ASCII")) {
	if (inStream.getBytes() > 0) {
		var doc = database.createDocument();
		doc.replaceItemValue("Form", "main");
		doc.replaceItemValue("subject", inPath);
		var body:NotesRichTextItem = doc.createRichTextItem("body");
		do {
			body.appendText(inStream.readText(NotesStream.STMREAD_LINE,
				NotesStream.EOL_CRLF));
		} while (!inStream.isEOS());
		doc.save(true, true);
	} else requestScope.status = "Input file has no content";
	inStream.close();
} else requestScope.status = "Input file open failed";


setContents


This button creates a document from the name and content of a file.

var inPath:string = requestScope.filename;
var fis = new java.io.FileInputStream(inPath);
var isr = new java.io.InputStreamReader(fis, "ASCII");
var inStream:NotesStream = session.createStream();
inStream.setContents(isr);
var doc:NotesDocument = database.createDocument();
doc.replaceItemValue("Form", "main");
doc.replaceItemValue("subject", inPath);
doc.replaceItemValue("body", inStream.readText());
inStream.close();
doc.save(true, true);

  • Actions Show Menu▼


expanded Attachments (0)
collapsed Attachments (0)
Edit the article to add or modify attachments.
expanded Versions (2)
collapsed Versions (2)
Version Comparison     
VersionDateChanged by              Summary of changes
This version (2)Apr 15, 2011, 2:32:22 PM~Elizabeth Umkroskiettu  
1Jan 31, 2011, 7:20:23 PM~Ben Eljipypulettu  
expanded Comments (0)
collapsed Comments (0)
Copy and paste this wiki markup to link to this article from another article in this wiki.
Go ElsewhereStay ConnectedAbout
  • HCL Software
  • HCL Digital Solutions community
  • HCL Software support
  • BlogsDigital Solutions blog
  • Community LinkHCL Software forums and blogs
  • About HCL
  • Privacy
  • Accessibility