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

  • SEARCH
  • Home
  • 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 191547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:21:02+00:00 2026-05-11T16:21:02+00:00

After writing out some processed content to an output stream, I need to revisit

  • 0

After writing out some processed content to an output stream, I need to revisit the beginning of the stream and write out some content metadata. The data I’m writing is very large, as much as 4Gb, and may be written either directly to a file or to an in-memory buffer, depending on various environmental factors.

How can I implement an OutputStream that allows me to write out headers after completing the writing of content?

  • 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-11T16:21:02+00:00Added an answer on May 11, 2026 at 4:21 pm

    Here’s a random access file output stream.

    Note that if using it for a large amount of streamed output you can temporarily wrap it in a BufferedOutputStream to avoid lots of small writes (just be very sure to flush it before discarding the wrapper or using the underlying stream directly).

    import java.io.*;
    
    /**
     * A positionable file output stream.
     * <p>
     * Threading Design : [x] Single Threaded  [ ] Threadsafe  [ ] Immutable  [ ] Isolated
     */
    
    public class RandomFileOutputStream
    extends OutputStream
    {
    
    // *****************************************************************************
    // INSTANCE PROPERTIES
    // *****************************************************************************
    
    protected RandomAccessFile              randomFile;                             // the random file to write to
    protected boolean                       sync;                                   // whether to synchronize every write
    
    // *****************************************************************************
    // INSTANCE CONSTRUCTION/INITIALIZATON/FINALIZATION, OPEN/CLOSE
    // *****************************************************************************
    
    public RandomFileOutputStream(String fnm) throws IOException {
        this(fnm,false);
        }
    
    public RandomFileOutputStream(String fnm, boolean syn) throws IOException {
        this(new File(fnm),syn);
        }
    
    public RandomFileOutputStream(File fil) throws IOException {
        this(fil,false);
        }
    
    public RandomFileOutputStream(File fil, boolean syn) throws IOException {
        super();
    
        File                                par;                                    // parent file
    
        fil=fil.getAbsoluteFile();
        if((par=fil.getParentFile())!=null) { IoUtil.createDir(par); }
        randomFile=new RandomAccessFile(fil,"rw");
        sync=syn;
        }
    
    // *****************************************************************************
    // INSTANCE METHODS - OUTPUT STREAM IMPLEMENTATION
    // *****************************************************************************
    
    public void write(int val) throws IOException {
        randomFile.write(val);
        if(sync) { randomFile.getFD().sync(); }
        }
    
    public void write(byte[] val) throws IOException {
        randomFile.write(val);
        if(sync) { randomFile.getFD().sync(); }
        }
    
    public void write(byte[] val, int off, int len) throws IOException {
        randomFile.write(val,off,len);
        if(sync) { randomFile.getFD().sync(); }
        }
    
    public void flush() throws IOException {
        if(sync) { randomFile.getFD().sync(); }
        }
    
    public void close() throws IOException {
        randomFile.close();
        }
    
    // *****************************************************************************
    // INSTANCE METHODS - RANDOM ACCESS EXTENSIONS
    // *****************************************************************************
    
    public long getFilePointer() throws IOException {
        return randomFile.getFilePointer();
        }
    
    public void setFilePointer(long pos) throws IOException {
        randomFile.seek(pos);
        }
    
    public long getFileSize() throws IOException {
        return randomFile.length();
        }
    
    public void setFileSize(long len) throws IOException {
        randomFile.setLength(len);
        }
    
    public FileDescriptor getFD() throws IOException {
        return randomFile.getFD();
        }
    
    } // END PUBLIC CLASS
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When writing server-side code you need to explicitly stop execution after sending a Location:
I am writing an application that interprets a time delayed stream of data. The
The request channel timed out while waiting for a reply after 00:00:59.9970702. Increase the
Suppose, I have a connected socket after writing this code.. if ((sd = accept(socket_d,
I am using JQuery to execute an operation within a web service. After writing
I'm writing a tool which will push changes to database after commit to svn.
I'm writing an application that displays Google Map right after the login screen. The
I'm writing a tic tac toe program that plays throuh the terminal/console After Player
I'm writing a web application which contains a kind of schedule. After a certain
I was writing a script that kept giving me errors. After tracking it down

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.