LotusScript base64 encoding is slow - other ways to encode?

I am using the DXLImporter to create programmatically an attachment in a Notes document.Domino requires the attachment to be base64 encoded.

I am calling a LotusScript hand-made function to perform this operation.

It works fine for small amounts of data, but when the attachment is larger, 400k let’s say, the encoding takes a very large amount of time.

Since the standard attach function is not that slow, I was wondering if anyone has a suggestion for a more efficient way - something internal to use that I am not aware of…

I haven’t found any documentation on that matter up to now.

Can somebody help?

Thanks!

Subject: LotusScript base64 encoding is slow - other ways to encode?

LotusScript is a relatively slow language, as most scripting languages are. Have you thought about making a C/C++ based encoder and calling it from LotusScript. Our Midas Rich Text LSX does base64 encoding in a slightly different context (part of sending e-mails or saving documents to MIME), and it handles even huge images with no noticeable delay. If it is not a problem having a DLL, the separate C/C++ encoding would be much, much faster.

Subject: RE: LotusScript base64 encoding is slow - other ways to encode?

Thank you for the suggestion, I am not allowed to use any additional software.

I have also tried with the MIME support that is pretty much quicker in encoding but I can’t find the attachment after that.

I was trying a half and half way:

  • MIME to encode the string

  • DXL to create the attachment

but I can’t get the encoded string from the MIMEEntity.

Any other suggestion?

Thanks.

Subject: RE: LotusScript base64 encoding is slow - other ways to encode?

I think you have the right idea. You can create your binary data into a NotesStream, then use the NotesStream to assign the binary data of a MIME entity (SetContentsFromBytes specifying ENC_NONE as the encoding). Then use NotesMIMEEntity.EncodeContent to change it to base64, then GetContentsAsText to get the encoded data. That should be fairly zippy.

If that’s what you’re doing and it’s not working, I suggest you post your code.

Subject: RE: LotusScript base64 encoding is slow - other ways to encode?

I am posting the code, since it is not working…I am getting an empty “encoded” string.

Here it is:

Function trymime(astring)

Dim session As New NotesSession

Dim dbCurrent As NotesDatabase

Set dbCurrent = session.currentdatabase



Dim stream As NotesStream

Dim streambyte As NotesStream

Dim mimeheader As NotesMIMEHeader

Dim body As NotesMIMEEntity



Set doc = dbCurrent.CreateDocument

session.ConvertMIME = False



Set body = doc.CreateMIMEEntity

Set mimeheader = body.CreateHeader("Content-Type")

Call mimeheader.SetHeaderVal("multipart/mixed")



Set stream = session.CreateStream

Call stream.Writetext(astring)

stream.Position = 0



Dim outstream As NotesStream

Set outstream = session.CreateStream



Do

	buffer = stream.Read(32767)

	Call outstream.Write(buffer)

Loop Until stream.IsEOS



outstream.Position = 0

Call body.SetContentFromBytes(outstream, "multipart/mixed" ,ENC_NONE)

doc.form = "TemporaryFile"



Call stream.close



Dim mime As NotesMIMEEntity



Set mime = doc.GetMIMEEntity("Body")



Call mime.EncodeContent( ENC_BASE64 )

Set streambyte = session.CreateStream

Call mime.GetContentAsText(streambyte, True)



Call doc.Save(True, True)



session.ConvertMIME = True



trymime = streambyte.ReadText()

End Function

Subject: RE: LotusScript base64 encoding is slow - other ways to encode?

It appears that you’re trying to write a function that accepts binary data stored in a text string. I don’t think this is going to work. String is not an appropriate datatype for working with binary data, and I think the problem is in your attempt to assign a stream from a string, and then read off the data as binary. It brings up considerations of what character set the NotesStream is using internally to represent the “text”, and in addition there are potential problems if the string contains null bytes, which various system functions and methods treat as string terminators.

Use a NotesStream in the first place to create or read your binary data.

Subject: Solution: LotusScript base64 encoding is slow - other ways to encode?

It works!I am appending the working code in order to share with anyone who could have the need:

Dim stream As NotesStream

Set stream = session.CreateStream	

Dim body As NotesMimeEntity



session.ConvertMIME = False



Call stream.WriteText(textToWrite)

Set body = doc.CreateMIMEEntity

Call body.SetContentFromText(stream, "", ENC_NONE)

Call body.EncodeContent(ENC_BASE64)



content = body.ContentAsText

At this point, ‘content’ contains my textToWrite encoded base64.

And is quick!!!

Thanks a lot for your contribution!

Subject: LotusScript base64 decoding

VERY GOOD, thanks to you all.Here is the other way around, decoding base64 text.

Dim stream As NotesStream

Dim doc As notesdocument

Dim db As notesdatabase

Dim session As New notessession

Set stream = session.CreateStream

Dim body As NotesMimeEntity

Set db = session.CurrentDatabase

Set doc = db.createdocument

session.ConvertMIME = False



Call stream.WriteText("a3JpcyB2YW4ga29vdGVu")

Set body = doc.CreateMIMEEntity

Call body.SetContentFromText(stream, "", ENC_BASE64)

Call body.DEcodeContent()



content = body.ContentAsText

Msgbox(content)

Subject: RE: Solution: LotusScript base64 encoding is slow - other ways to encode?

Some further enhancement on this…

If you have base64 content at your hand and you need to put this into an attachment in a NotesDocument without first saving it to disk, you should also add some mime header. Otherwise, the $FILE item created is not much properly treated as a normal attachment; for example @AttachmentNames gives empty result. Here’s a sample code excerpt:

'Some dims and sets first...

'...

Set stream = s.CreateStream

Call stream.WriteText(StringInBase64)	'Base64 encoded string is put into the stream

Set body = ResponseDoc.CreateMIMEEntity

Set header = body.createHeader("content-disposition")

Call header.setHeaderVal({attachment;filename="ABC.PDF"})

Call body.SetContentFromText(stream, "", ENC_BASE64)

Call stream.Close

Thanks for the above posts and also Jake Howlett’s post below which helped me to come to this point.

http://www.codestore.net/store.nsf/rsrc/bloggifs40/$file/generatetextfile.lss

Subject: RE: Solution: LotusScript base64 encoding is slow - other ways to encode?

OK, it worked perfectly for the single byte characters.Now I need to make it work also for Japanese, Chinese, and so on.

It does not work.

The DBCS characters are converted to ? (question marks) from the:

Call body.SetContentFromText(stream, “”, ENC_NONE)

Also tried:

Call body.SetContentFromText(stream, “charset=UTF-8”, ENC_NONE)

Nothing changes.

Does anyone have suggestions to solve this?

Is it possibly a Notes bug?

Thanks a lot for any comment that could help…

ciao