Macro/Basic: SequenceInputStream.createStreamFromSequence(): runtime error: not found

According to LibreOffice: SequenceInputStream Service Reference, the SequenceInputStream service should have a createStreamFromSequence() method, which I would like to use to initialize an instance with some byte data. Unfortunately, when I do actually try to call that method, the mentioned runtime error is triggered.

function BytesToString(data() as variant, _
optional encoding as string) as string
	dim istrm as object
	dim tstrm as object
	dim var as variant

	if IsMissing(encoding) then
		encoding = "utf-8"
	end if

	istrm = CreateUnoService("com.sun.star.io.SequenceInputStream")

	'- runtime ERROR:  property or method not found: createStreamFromSequence
	istrm.createStreamFromSequence(data)

	tstrm = CreateUnoService("com.sun.star.io.TextInputStream")
	tstrm.setInputStream(istrm)
	tstrm.setEncoding(encoding)
	var = tstrm.isEOF()

	'var = tstrm.readLine()
	var = tstrm.readString(Array(), false)
	tstrm.closeInput()

	BytesToString = var
end function
sub TestSub()
	dim var as variant
	
	var = Array(97,98,99)
	var = BytesToString(var)
	exit sub
end sub

When I write the value of istrm.dbg_methods to a file, I can confirm that the createStreamFromSequence() method is not among the methods in that listing. However, there is an initialize() method which seems to originate from a base class - calling it using “initialize(data)” will fail due to an IllegalArgumentException.

So what is the problem?!?

  • Do I need to call the createStreamFromSequence() method in a different way because it is a “Public Member Function” that isn’t part of an actual interface? That method seems to be a direct service method?
  • Is it a bug because that method isn’t available, even though it should be?
Version: 24.8.0.3 (X86_64) / LibreOffice Community
Build ID: 0bdf1299c94fe897b119f97f3c613e9dca6be583
CPU threads: 4; OS: Linux 5.15; UI render: default; VCL: gtk3
Locale: de-DE (en_US.UTF-8); UI: en-US
Calc: threaded

One example for SequenceInputStream Apache OpenOffice Community Forum - Insert SVG via macro into a writer document - (View topic)

If you intended to suggest using pipes, then yes that works as an alternative.
However, I wanted to know how to get it to work with the “special” createStreamFromSequence() routine, or in general get an answer on how to use SequenceInputStreams.
The difference between the two is that pipes may block the entire application if you forget to close the output stream after writing the initial data.

try
istrm = com.sun.star.io.SequenceInputStream.createStreamFromSequence(data)

1 Like

For completeness, here are another methods for calling the service constructor.

Sub TestStreamFromSequence() 
  Dim aByte(1) as Byte, oSIS As Object
  aByte(0)=97
  aByte(1)=98
  oSIS=GetProcessServiceManager().createInstanceWithArguments("com.sun.star.io.SequenceInputStream", Array(aByte))
End Sub
Sub Test2StreamFromSequence() 
  Dim aByte(1) as Byte, oSIS As Object
  aByte(0)=97
  aByte(1)=98
  oSIS=CreateUnoService("com.sun.star.io.SequenceInputStream")
  oSIS.initialize Array(aByte)
End Sub
1 Like

@ms777 A simple and straight forward answer. Thanks.
I will try to keep in mind that I can call these kind of functions in a static-like fashion.

@sokol92 Thanks for reminding me that the process manager might provide a workaround.
Well, despite my years of experience with VBA, I’m still quite new with LibreOffice Basic.

What I didn’t know when I tried the initialize() method was, that you could use the Array() function to wrap up the byte array, and make it work this way. Thanks for your answer.