Business Central 2023 wave 1 (BC22): Better InStream support (InStream.Length & InStream.Position)

Dynamics 365 Business Central

Hi, Readers.
Dynamics 365 Business Central 2023 wave 1 (BC22) is generally available last week. More details: Dynamics 365 Business Central 2023 release wave 1 (BC22)

I will continue to test and share some new features that I hope will be helpful.

Better InStream support:

Business value:
When working with stream objects, AL developers can get the size of the stream object without having to convert it to a blob. Removing the conversion to a blob makes their code run faster.

https://learn.microsoft.com/en-us/dynamics365/release-plan/2023wave1/smb/dynamics365-business-central/better-instream-support

Until this release, an AL developer who needed to know the size of an InStream object had to convert it to a blob and then ask for the size. This caused unwanted resource consumption on the server because it had to allocate memory to the blob object (that is likely just thrown away after asking for the size of it).

InStream Data Type: Is a generic stream object that you can use to read from or write to files and BLOBs. You can define the internal structure of a stream as a flat stream of bytes. You can assign one stream to another. Reading from and writing to a stream occurs sequentially.

In this release, Microsoft added two new properties to the InStream datatype:

  • Length: Returns the length in bytes of the stream.
  • Position: Returns the position within the current stream.

And there are three new methods (runtime version 11.0):

In this post, I would like to briefly talk about new InStream.Length() Method. Let’s see some details.

If we want to get the size of the stream object, as of now, we can only use procedure Length() in codeunit 4100 “Temp Blob”.

Determines the length of the data stored in the TempBlob.
Returns: The number of bytes stored in the BLOB.

As mentioned by Microsoft, we have to first convert the stream object into a blob.
For example, I want to get the size of the current uploaded file.

In previous versions:

Source Code:

pageextension 50112 MyExtension extends "Customer List"
{
    actions
    {
        addafter("C&ontact")
        {
            action(CheckFileSize)
            {
                Caption = 'Check File Size';
                ApplicationArea = All;
                Promoted = true;
                PromotedCategory = Process;
                PromotedIsBig = true;

                trigger OnAction()
                var
                    FromFile: Text[100];
                    IStream: InStream;
                    OStream: OutStream;
                    UploadMsg: Label 'Please Choose the file.';
                    TempBlob: Codeunit "Temp Blob";
                begin
                    UploadIntoStream(UploadMsg, '', '', FromFile, IStream);
                    TempBlob.CreateOutStream(OStream);
                    CopyStream(OStream, IStream);
                    Message(Format(TempBlob.Length()));
                end;
            }
        }
    }
}

In BC 22 (runtime version 11.0): We can get the length directly.👏👏👏

Source Code:

pageextension 50112 MyExtension extends "Customer List"
{
    actions
    {
        addafter("C&ontact")
        {
            action(CheckFileSize)
            {
                Caption = 'Check File Size';
                ApplicationArea = All;
                Promoted = true;
                PromotedCategory = Process;
                PromotedIsBig = true;

                trigger OnAction()
                var
                    FromFile: Text[100];
                    IStream: InStream;
                    UploadMsg: Label 'Please Choose the file.';
                begin
                    UploadIntoStream(UploadMsg, '', '', FromFile, IStream);
                    Message(Format(IStream.Length));
                end;
            }
        }
    }
}

The data returned by both ways above is the number of bytes.

Very nice.

Also, we have discussed how to use Excel Buffer to Import data before. What should we do if the client add another requirement and need to record the size imported into Excel? It’s easy to get it using this method.

PS:
1. For the Position property, you can do something like the following

2. AL Language extension changelog version 11.0:

Give it a try!!!😁

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL