Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6108419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:17:40+00:00 2026-05-23T14:17:40+00:00

I have a ByteBuffer that contains a large file (100 MB): java.nio.ByteBuffer byteBuffer =

  • 0

I have a ByteBuffer that contains a large file (100 MB):

    java.nio.ByteBuffer byteBuffer = ByteBuffer.wrap(multipartFile.getBytes());
    writeChannel.write(byteBuffer);
    writeChannel.closeFinally();

I can only legally write 1 MB to the writeChannel at a time.

How do I slice up the contents of the ByteBuffer and write only a 1 MB slice at a time into the writeChannel?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:17:40+00:00Added an answer on May 23, 2026 at 2:17 pm

    You can use ByteBuffer#slice() to get a duplicate view of your base ByteBuffer instance, then bump the position along to expose a sliding window of content. Alternately, you can just do the same to your base buffer if you don’t need to expose it to any other consumers.

    You can change the starting position of your view of the content via the single-argument Buffer#position(int) method, and change the end position of your view via Buffer#limit(int). So long as you’re mindful not to push the view beyond the limit of the underlying buffer, you can do the following:

    final ByteBuffer view = base.slice();
    for (int start = base.position(), end = base.limit(), stride = 1000000;
         start != end;
         start = view.limit())
      consume(view.position(start)
                  .limit(start + Math.min(end - start, stride)));
    

    I didn’t test it, but it looks correct. It’s possible to rewrite to avoid the initial setting of the position, which isn’t strictly necessary here, but it incurs either some repetition or more awkward special case treatment of the first time through.

    I left it this way to preserve the basic for loop structure.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following in a java file (MyRtmpClient.java): import org.apache.mina.common.ByteBuffer; and ByteBuffer is
I have a ByteBuffer containing bytes that were derived by String.getBytes(charsetName) , where containing
I have some image processing Java code in Android that acts upon two large
I have a file that contains about 200,000 long values that I want to
I have a Java program that opens a socket connection to a server that
I have a class for IO that uses ByteBuffer to buffer access to a
I have question on usage java nio and hope someone who has a lot
I have a large (3Gb) binary file of doubles which I access (more or
I have a Java client that connects to a C++ server using TCP Sockets
I have a ByteBuffer called buffer. I want to generate a new buffer that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.