Java中字节顺序标记错误的文件读取

Java中字节顺序标记错误的文件读取,第1张

Java中字节顺序标记错误的文件读取
package com.stackoverflow.answer;import java.io.IOException;import java.io.InputStream;import java.io.PushbackInputStream;public class UnipreBOMInputStream extends InputStream{    public static final class BOM  {        public static final BOM NONE = new BOM(new byte[]{},"NONE");        public static final BOM UTF_8 = new BOM(new byte[]{(byte)0xEF,(byte)0xBB,(byte)0xBF},"UTF-8");        public static final BOM UTF_16_LE = new BOM(new byte[]{ (byte)0xFF,     (byte)0xFE},    "UTF-16 little-endian");        public static final BOM UTF_16_BE = new BOM(new byte[]{ (byte)0xFE,     (byte)0xFF},    "UTF-16 big-endian");        public static final BOM UTF_32_LE = new BOM(new byte[]{ (byte)0xFF,     (byte)0xFE,     (byte)0x00,     (byte)0x00},    "UTF-32 little-endian");        public static final BOM UTF_32_BE = new BOM(new byte[]{ (byte)0x00,     (byte)0x00,     (byte)0xFE,     (byte)0xFF},    "UTF-32 big-endian");        public final String toString()    {      return description;    }        public final byte[] getBytes()    {      final int     length = bytes.length;      final byte[]  result = new byte[length];      // Make a defensive copy      System.arraycopy(bytes,0,result,0,length);      return result;    }    private BOM(final byte bom[], final String description)    {      assert(bom != null)    : "invalid BOM: null is not allowed";      assert(description != null)       : "invalid description: null is not allowed";      assert(description.length() != 0) : "invalid description: empty string is not allowed";      this.bytes          = bom;      this.description  = description;    } final byte    bytes[];    private final String  description;  } // BOM    public UnipreBOMInputStream(final InputStream inputStream) throws  NullPointerException,    IOException  {    if (inputStream == null)      throw new NullPointerException("invalid input stream: null is not allowed");    in = new PushbackInputStream(inputStream,4);    final byte  bom[] = new byte[4];    final int   read  = in.read(bom);    switch(read)    {      case 4:        if ((bom[0] == (byte)0xFF) && (bom[1] == (byte)0xFE) && (bom[2] == (byte)0x00) && (bom[3] == (byte)0x00))        {          this.bom = BOM.UTF_32_LE;          break;        }        else        if ((bom[0] == (byte)0x00) && (bom[1] == (byte)0x00) && (bom[2] == (byte)0xFE) && (bom[3] == (byte)0xFF))        {          this.bom = BOM.UTF_32_BE;          break;        }      case 3:        if ((bom[0] == (byte)0xEF) && (bom[1] == (byte)0xBB) && (bom[2] == (byte)0xBF))        {          this.bom = BOM.UTF_8;          break;        }      case 2:        if ((bom[0] == (byte)0xFF) && (bom[1] == (byte)0xFE))        {          this.bom = BOM.UTF_16_LE;          break;        }        else        if ((bom[0] == (byte)0xFE) && (bom[1] == (byte)0xFF))        {          this.bom = BOM.UTF_16_BE;          break;        }      default:        this.bom = BOM.NONE;        break;    }    if (read > 0)      in.unread(bom,0,read);  }    public final BOM getBOM()  {    // BOM type is immutable.    return bom;  }    public final synchronized UnipreBOMInputStream skipBOM() throws IOException  {    if (!skipped)    {      in.skip(bom.bytes.length);      skipped = true;    }    return this;  }    public int read() throws IOException  {    return in.read();  }    public int read(final byte b[]) throws  IOException,         NullPointerException  {    return in.read(b,0,b.length);  }    public int read(final byte b[],       final int off,       final int len) throws IOException,       NullPointerException  {    return in.read(b,off,len);  }    public long skip(final long n) throws IOException  {    return in.skip(n);  }    public int available() throws IOException  {    return in.available();  }    public void close() throws IOException  {    in.close();  }    public synchronized void mark(final int readlimit)  {    in.mark(readlimit);  }    public synchronized void reset() throws IOException  {    in.reset();  }    public boolean markSupported()   {    return in.markSupported();  }  private final PushbackInputStream in;  private final BOM      bom;  private       boolean  skipped = false;} // UnipreBOMInputStream

你正在以这种方式使用它:

import java.io.BufferedReader;import java.io.FileInputStream;import java.io.InputStreamReader;public final class UnipreBOMInputStreamUsage{  public static void main(final String[] args) throws Exception  {    FileInputStream fis = new FileInputStream("test/offending_bom.txt");    UnipreBOMInputStream ubis = new UnipreBOMInputStream(fis);    System.out.println("detected BOM: " + ubis.getBOM());    System.out.print("Reading the content of the file without skipping the BOM: ");    InputStreamReader isr = new InputStreamReader(ubis);    BufferedReader br = new BufferedReader(isr);    System.out.println(br.readLine());    br.close();    isr.close();    ubis.close();    fis.close();    fis = new FileInputStream("test/offending_bom.txt");    ubis = new UnipreBOMInputStream(fis);    isr = new InputStreamReader(ubis);    br = new BufferedReader(isr);    ubis.skipBOM();    System.out.print("Reading the content of the file after skipping the BOM: ");    System.out.println(br.readLine());    br.close();    isr.close();    ubis.close();    fis.close();  }} // UnipreBOMInputStreamUsage


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4942894.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-13
下一篇 2022-11-13

发表评论

登录后才能评论

评论列表(0条)

保存