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 7916729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:54:37+00:00 2026-06-03T14:54:37+00:00

In Java network inputstream why is it not recommended that this be done :

  • 0

In Java network inputstream why is it not recommended that this be done :

 PdfReader localPdfReader = new PdfReader(item.getinputStream());

Where item is a FileItem uploaded from a client.
Instead this is recommended:

ByteArrayOutputStream output = new ByteArrayOutputStream();
IOUtils.copy(item.getInputStream(), output);

What is the diference between the two?
EDIT
I came to know that it is because inputstream here is a network stream. So once a byte is read you cannot read it anymore. So you have to copy it to memory(IOUtils) before using it. My question is while copying to memory also you have to read the stream byte by byte right? then why doesn’t the inputstream get closed there?

  • 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-06-03T14:54:40+00:00Added an answer on June 3, 2026 at 2:54 pm

    I guess the difference is of Reader and Inputstream. In your example, a PDF doc is binary data which should not be transferred character by character but byte by byte. Check this link in the same forum for more on Reader and InputStream. Even though it mentioned wrapping of Stream by Reader, as mentioned earlier for binary data this should be discouraged.

    EDIT: 1

    Lets check the way Reader and InputStream’s read method works

    Reader.read() returns integer in the range 0 to 65535 (single 16-bit Unicode character)

    InputStream.read() returns byte (8-bit signed two’s complement integer) of data

    Now imagine if you use Reader to read binary data (which is sequence of 8 bit integer), you will end up reading two bytes (8*2) instead of one assuming it to be a character.

    I have not seen the code for PdfReader so not sure if it uses java.io.Reader. This explaination is purly for java.io.Reader/InputStream. I would appreciate if you share some link or post which which says the the PdfReader if used in a manner you mentioned, is not good.

    EDIT:2

    Remember:

    1. From a network, you can read the stream bytes only once.
    2. If you need those bytes for multiple tasks, better store those bytes in array and used the same array multiple time

    If you use

    PdfReader localPdfReader = new PdfReader(item.getinputStream());

    then PdfReader internally reads the bytes from stream and uses it to validate. It does not store it for any further usage.

    If you use

    IOUtils
    

    It copies the bytes from network to a byte array which later can be used in PdfReader as well as JDBC call to store it in DB.

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

Sidebar

Related Questions

Possible Duplicate: Not possible to launch a file on a network using Java Desktop?
I have two Java applications in my network that talk to each other over
I have developed a java servlet that monitors a folder on a network drive
I found this article about simple proxy server implemented in JAVA: http://www.java2s.com/Code/Java/Network-Protocol/Asimpleproxyserver.htm The code
Why is it that java.util.List does not implement Serializable while subclasses like LinkedList ,
I am new to C++ network programming but have experience with Java sockets etc.
Is there a Java library that provides functionality for developing social network applications such
I'm trying to AES encode data in java, send it over the network and
Java's checked exceptions sometimes force you to catch a checked exception that you believe
Java and .NET are two languages targeted at removing platform dependence. This is achieved

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.