001 /*
002 * This file is part of muCommander, http://www.mucommander.com
003 * Copyright (C) 2002-2008 Maxence Bernard
004 *
005 * muCommander is free software; you can redistribute it and/or modify
006 * it under the terms of the GNU General Public License as published by
007 * the Free Software Foundation; either version 3 of the License, or
008 * (at your option) any later version.
009 *
010 * muCommander is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU General Public License for more details.
014 *
015 * You should have received a copy of the GNU General Public License
016 * along with this program. If not, see <http://www.gnu.org/licenses/>.
017 */
018
019 package com.mucommander.io.bom;
020
021 /**
022 * BOM represents a Byte-Order Mark, a byte sequence that can be found at the beginning of a Unicode text stream
023 * which indicates the encoding of the text that follows.
024 *
025 * @see BOMInputStream
026 * @author Maxence Bernard
027 */
028 public class BOM {
029
030 /** the byte sequence that identifies this BOM */
031 private byte[] sig;
032
033 /** the character encoding designated by this BOM */
034 private String encoding;
035
036
037 /**
038 * Creates a new <code>BOM</code> instance identified by the given signature and designating the specified
039 * character encoding.
040 *
041 * @param signature the byte sequence that identifies this BOM
042 * @param encoding the character encoding designated by this BOM
043 */
044 public BOM(byte signature[], String encoding) {
045 this.sig = signature;
046 this.encoding = encoding;
047 }
048
049 /**
050 * Returns the byte sequence that identifies this BOM at the beginning of a byte stream.
051 *
052 * @return the byte sequence that identifies this BOM at the beginning of a byte stream
053 */
054 public byte[] getSignature() {
055 return sig;
056 }
057
058 /**
059 * Returns the character encoding that this BOM denotes.
060 *
061 * @return the character encoding that this BOM denotes
062 */
063 public String getEncoding() {
064 return encoding;
065 }
066
067 /**
068 * Returns <code>true</code> if this BOM's signature starts with the given byte sequence.
069 *
070 * @param bytes the byte sequence to compare against this BOM's signature
071 * @return true if this BOM's signature starts with the given byte sequence
072 */
073 public boolean sigStartsWith(byte bytes[]) {
074 int bytesLen = bytes.length;
075 if(bytesLen>sig.length)
076 return false;
077
078 for(int i=0; i<bytesLen; i++) {
079 if(bytes[i]!= sig[i])
080 return false;
081 }
082
083 return true;
084 }
085
086 /**
087 * Returns <code>true</code> if this BOM's signature matches the given byte sequence.
088 *
089 * @param bytes the byte sequence to compare against this BOM's signature
090 * @return true if this BOM's signature matches the given byte sequence
091 */
092 public boolean sigEquals(byte bytes[]) {
093 return bytes.length==sig.length && sigStartsWith(bytes);
094 }
095
096
097 ////////////////////////
098 // Overridden methods //
099 ////////////////////////
100
101 /**
102 * Returns <code>true</code> if and only if the given Object is a <code>BOM</code> instance with the same
103 * signature as this instance. *
104 *
105 * @param o the Object to test for equality
106 * @return true if the specified Object is a BOM instance with the same signature as this instance
107 */
108 public boolean equals(Object o) {
109 return (o instanceof BOM) && ((BOM)o).sigEquals(sig);
110 }
111
112 /**
113 * Returns a String representation of this <code>BOM</code>.
114 *
115 * @return returns a String representation of this <code>BOM</code>.
116 */
117 public String toString() {
118 String sigRep = "{";
119 for(int i=0; i<sig.length; i++)
120 sigRep += (0xFF&sig[i])+(i==sig.length-1?"}":", ");
121
122 return super.toString()+", signature="+sigRep+", encoding="+encoding;
123 }
124 }