Tips for dynamically concatenating a block of byte stream from an HTTP response using JavaScript

Alright,

I am looking for a way to deliver a continuous stream of bytes to the browser, such as a PDF file, so it can be displayed to the user.

if (bytesToRead == -1) {
                bytesToRead = (int)fullPathFile.length();}
            byte[] buffer = new byte[bytesToRead];
            int bytesRead = -1;     
            if((inputFileInputStream != null) && ((bytesRead = inputFileInputStream.read(buffer)) != -1)){ 

                if (codec.equals("base64")) {
                    String streamLength = Base64.encodeBytes(buffer, 0, bytesToRead);
                    response.setContentLength(streamLength.length());
                    outputFileOutputStream.write(Base64.encodeBytes(buffer, 0, bytesToRead).getBytes());

                } else {
                    outputFileOutputStream.write(buffer, 0, bytesToRead);
                }
            }
            inputFileInputStream.close();
            outputFileOutputStream.flush();
            outputFileOutputStream.close();

I have successfully achieved this with the option -1 to get the entire byte stream at once. However, I now want to send smaller chunks of 2kb each. To accomplish this, I understand that I need to send multiple 2kb responses and concatenate them in the JavaScript code. But how exactly do I go about doing this?

Answer №1

To iterate over arrays, use the concat() method...

var allBytes = [];

for (byteArray in allByteArray){
    allBytes = allBytes.concat(byteArray);
}

However, if you need to merge multiple PDF pages, this approach will not be effective...

Answer №2

Within my code, I utilized a for loop to iterate through the data points and increment the offset.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Converting an ArrayList instanceObject to a string representation

I am attempting to change the representation of a class instance within an ArrayList to a string using my code below: Here is the User class: public class User { // ... public void setName(String name) { this.fullname = name; } ...

Encountering a VueJS error with Google Cloud Logging Winston: "TypeError: The 'original' argument must be a Function."

I have been attempting to integrate @google-cloud/logging-winston into my VueJS project by closely following the guidelines provided in both the npm package and Google Cloud docs. However, I am encountering an error message stating "TypeError: The &q ...

Tips for displaying a React component with ReactDOM Render

_Header (cshtml) <div id="Help"></div> export default class Help { ReactDOM.render( <Help/>, document.getElementById('Help') ); } Help.js (component) } My objective is to di ...

Nesting / Mulled / JS - Uploading Files - Form's end is unexpectedly reached

I have successfully implemented the upload file functionality in my Nest.js server application, but I am facing an issue when trying to use it with JavaScript/React. @Post('upload') @UseInterceptors(FileInterceptor('file')) upl ...

Disable page scrolling while enabling scrolling for specific elements with overflow using JQM

Is there a way to stop the page from scrolling on a jQuery Mobile page while still allowing a specific element with overflow set to scroll to be scrolled by the user? The challenge is that the length of the page may vary slightly, exceeding 100% on differe ...

The order of items in MongoDB can be maintained when using the $in operator by integrating Async

It's common knowledge that using {$in: {_id: []}} in MongoDB doesn't maintain order. To address this issue, I am considering utilizing Async.js. Let's consider an example: const ids = [3,1,2]; // Initial ids retrieved from aggregation con ...

What is the best way to iterate through files within a directory and its nested subdirectories using electron?

I am currently working on developing a desktop application that involves scanning through a directory, including all subdirectories, to locate files containing specific characters in their filenames. Is it feasible to accomplish this task using Electron? ...

What is preventing my content from showing up when clicked like this?

I have created a javascript script to reveal hidden content, but for some reason it is not working when I click on it. Can anyone provide assistance? Below is the script I am using: <script src="style/jquery/jquery.js"></script> <script> ...

Is there a more efficient alternative to replacing a collection proxy with a transient collection in Hibernate?

Consider this scenario where I have a small object graph: user user.someCollection The someCollection property is set to lazy loading, resulting in a collection proxy being created. Now, let's say I want to add an item to this collection: user.som ...

Most effective method for modifying a value within a MongoDB object

My approach to storing each element in my mongodb collection looks like this. { _id: 'iTIBHxAb8', title: 'happy birthday', votesObject: { happy: 0, birthday: 0 } } I came up with a workaround that I'm not particularly proud ...

How to utilize jQuery's each function to parse JSON from an array composed of multiple arrays

Note: This information has been heavily updated, although progress is slow but steady. I am working with an array of JSON arrays that I need to parse from a remote source. However, for illustrative purposes, I have included a structural example in my Java ...

When using TypeScript in combination with Rxjs, the SwitchMap operator and tap function may not function

After calling setUsersData() and addNewUser(), they should return an Observable<void> once their tasks are complete. Surprisingly, despite the code seeming to work correctly, the tap operator's function is not being executed as expected. The sam ...

Retrieving feedback and scores from the Bazzarvoice API

I am a beginner exploring Bazzarvoice for the first time. I have obtained a Bazaarvoice API key and would like to showcase ratings and reviews using it. Could someone provide guidance on how to effectively display reviews and ratings? Thank you, Irshad ...

Still Facing the 'appendChild' Error Even After Defining it

Looking for assistance in creating new elements to display information on a Discord bot list I'm currently developing. var btn = document.createElement("BUTTON"); btn.innerHTML = "Try It"; document.body.appendChild(btn); ...

Vanishing Items - Three.js CanvasRenderer

I'm in a bit of a pickle here. I can't figure out why my objects are disappearing when using the canvas renderer. Strangely enough, everything works fine with the webGL renderer. Unfortunately, I need to make sure this displays properly on mobile ...

MongoDB aggregation function returning zero rather than the correct sum of numbers within an array

Here is the structure of my document: { "_id": "2023-04-08", "dailyNotes": [ { "therapyDiscipline": "PT", "individual": [ 60, 45, 30, 75, ...

jQuery is malfunctioning following an AJAX request

Can anyone help me fix an issue with my jQuery code? I have a hidden div that should be shown when the mouse hovers over a sibling element. However, it seems like after my AJAX function is executed, the jQuery stops working. <div class="parent"> ...

stop automatic resizing of windows

My CSS is written using percentages. I need to maintain the percentages intact. Is there a way to stop the layout from changing when the browser window is resized? I want the percentages to remain unaffected, even when the browser window is being resized ...

Is there a way to link the IDs in the data with Vue.js to access the corresponding names?

In the project I'm currently working on, I have two endpoints that provide different information. The 'categories' endpoint supplies me with category names and their corresponding id values, while the 'products' endpoint only gives ...

Exploring Java EE capabilities with application servers: What are the possibilities?

After exploring some Java EE techniques within Java SE, I have decided to delve deeper into the world of Java EE. While I am familiar with using JPA and JMS in Java SE, I believe that utilizing Java EE and an application server could help solve some of the ...