Wednesday, May 25, 2016

Convert InputStream to String in Java

Sometimes in Java world it is necessary to convert an InputStream to a String for further processing. There are several ways to do it, but here is a fantastic trick I recently discovered in this article:
final String text = new Scanner(inputStream)
        .useDelimiter("\\A").next();
The idea behind this trick is regex \A. It matches the beginning of input. Since there is only one beginning of input the whole contents of the stream is returned as the first (and the last) token.

There is a problem with the code above. It would throw an exception if the provided stream is empty. However this is easy to fix:
final Scanner scanner = new Scanner(inputStream)
        .useDelimiter("\\A");
final String text = scanner.hasNext() ? scanner.next() : "";
If a specific charset must be used it could be passed as the second argument to the Scanner constructor:
final Scanner scanner = new Scanner(inputStream, "ISO-8859-1")
        .useDelimiter("\\A");
final String text = scanner.hasNext() ? scanner.next() : "";
That's all there is to it.
Thanks for reading!

24 comments:

  1. Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.
    hadoop training in bangalore

    ReplyDelete
  2. Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.

    R Programming Training in Chennai

    ReplyDelete
  3. I ‘d mention that most of us visitors are endowed to exist in a fabulous place with very many wonderful individuals with very helpful things.
    AWS Training in Bangalore

    ReplyDelete
  4. I just found this blog and have high hopes for it to continue. Keep up the great work, its hard to find good ones. I have added to my favorites. Thank You.
    ME/M.Tech Project Center in Chennai | ME/M.Tech Project Center in Velachery

    ReplyDelete
  5. Awesome post. Really you are shared very informative concept.. Thank you for sharing. Keep on updating..
    Best Microsoft Azure Training Institute in Chennai | Best Microsoft Azure Training Institute in Velachery

    ReplyDelete
  6. I have to voice my passion for your kindness giving support to those people that should have guidance on this important matter.
    safety professional course in chennai

    ReplyDelete
  7. The information you shared is very effective for learners.

    Visit for a real time training for Java Training in Chennai | Best Java Training Institute in Chennai | Java Master Program Training in Chennai

    ReplyDelete
  8. Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here
    fire and safety course in chennai

    ReplyDelete
  9. AWS Training in Bangalore - Live Online & Classroom
    myTectra Amazon Web Services (AWS) certification training helps you to gain real time hands on experience on AWS. myTectra offers AWS training in Bangalore using classroom and AWS Online Training globally. AWS Training at myTectra delivered by the experienced professional who has atleast 4 years of relavent AWS experince and overall 8-15 years of IT experience. myTectra Offers AWS Training since 2013 and retained the positions of Top AWS Training Company in Bangalore and India.


    IOT Training in Bangalore - Live Online & Classroom
    IOT Training course observes iot as the platform for networking of different devices on the internet and their inter related communication. Reading data through the sensors and processing it with applications sitting in the cloud and thereafter passing the processed data to generate different kind of output is the motive of the complete curricula. Students are made to understand the type of input devices and communications among the devices in a wireless media.

    ReplyDelete
  10. Extraordinary blog. you put Good stuff. All the themes were clarified briefly.Thanks for sharing that post.Keep in blogging…
    No.1 Software Testing Training Institute in Chennai | Best Selenium Training Institute in Chennai | Web Designing Training Institute in Chennai

    ReplyDelete
  11. Nice and informative article. Thanks for sharing such nice article, keep on updating.
    Graphic Design Training Institute in Chennai | Graphic Design Training Institute in Velachery

    ReplyDelete
  12. This article is very much helpful and i hope this will be an useful information for the needed one.Keep on updating
    Certified Ethical Hacking Training in Chennai | Hacking Training in Saidapet

    ReplyDelete
  13. Nice blog. Thank you for sharing. The information you shared is very effective for learners I have got some important suggestions from it.
    Cisco CCNP Certification Exam Center in Chennai | Best CCNP Training in Alandur

    ReplyDelete
  14. I simply wanted to write down a quick word to say thanks to you for those wonderful tips and hints you are showing on this site.
    No.1 Microsoft Azure Training Institute in Chennai | No.1 Microsoft Azure Training Institute in Velachery

    ReplyDelete
  15. Your blog is really amazing with smart and cute content.keep updating such an excellent article
    CLOUD COMPUTING Classes in Chennai | CLOUD COMPUTING Courses in Velachery

    ReplyDelete
  16. Thanks for sharing wonderful post..What a innovative post..keep update post regularly..
    Selenium Testing Course in Chennai | Selenium Testing Course in Velachery

    ReplyDelete
  17. Nice blog. Thank you for sharing. The information you shared is very effective for learners I have got some important suggestions from it…Best Dotnet Training Institute in Chennai|Best Dotnet Training Institute in Velachery

    ReplyDelete
  18. I simply wanted to write down a quick word to say thanks to you for those wonderful tips and hints you are showing on this site.
    No.1 Blue Prism Training Institute in Chennai | No.1 Blue Prism Training Institute in Velachery | No.1 Blue Prism Training Institute in Kanchipuram

    ReplyDelete
  19. I feel really happy to have seen your webpage and look forward to so many more entertaining times reading here. Thanks once more for all the details.
    Automation Anywhere Training with Placement in Chennai | Automation Anywhere Training with Placement in Tambaram

    ReplyDelete