栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

字节顺序标记会破坏Java中的文件读取

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

字节顺序标记会破坏Java中的文件读取

编辑:我已经在GitHub上发布了正确的版本:https : //github.com/gpakosz/UnipreBOMInputStream

这是我之前编码的一个类,我只是在粘贴之前编辑了程序包名称。没什么特别的,它与SUN的错误数据库中发布的解决方案非常相似。将其合并到您的代码中就可以了。

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/392240.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号