GithubHelp home page GithubHelp logo

gpakosz / unicodebominputstream Goto Github PK

View Code? Open in Web Editor NEW
37.0 6.0 12.0 12 KB

Doing things right, in the name of Sun / Oracle

License: Other

Java 100.00%
java unicode bom inputstream utf-8 jdk

unicodebominputstream's Introduction

UnicodeBOMInputStream

A helper class to skip Unicode BOMs at the beginning of input streams.

I initially released this class as a Stack Overflow answer and it apparently got copy-pasted into several Java projects already. However, code put as answers on Stack Overflow is licensed under CC-BY-SA 3.0 which may not suit everybody.


Why?

Many years have passed since I wrote this class and today Java still doesn't properly deal with UTF-8 Unicode Byte Order Marks (BOMs) at the beginning of data.

In 2001, someone opened bug JDK-4508058 with the sound expectation that Java should detect and skip UTF-8 BOMs at the beginning of UTF-8 streams, the same way it does for e.g. UTF-16.

Bug JDK-4508058 remained open for a while, got fixed, and ultimately got reverted because existing Java code relied on UTF-8 BOMs not being skipped, see JDK-6378911:

the Java EE 5 RI and SJSAS 9.0 has been relying on detecting a BOM, setting the appropriate encoding, and discarding the BOM bytes before reading the input

The problem is that we cannot implement different behaviour depending on the JRE version we're running against.

In the end, instead of fixing JDK-4508058 and accept this would be an annoyance only for Java EE 5 RI and SJSAS 9.0 users, people in charge at Sun couldn't be bothered supporting 2 JDK versions and decided we're all living in a better world if JDK-4508058 gets closed as "won't fix".

In 2010, someone opened bug JDK-6959785 to reconsider the decision...

For more than 20 years now, every now and then, someone in the world edits an XML file with Notepad.exe which adds useless UTF-8 BOMs, and breaks their favorite Java XML parser.

Meanwhile, just skip the BOM yourself.


Usage

Wrap any InputStream with UnicodeBOMInputStream and use the getBOM() and/or skipBOM() methods. See UnicodeBOMInputStreamUsage.java.


If you find this library useful and decide to use it in your own projects please drop me a line @gpakosz.

unicodebominputstream's People

Contributors

benmccann avatar gpakosz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.