1 11 package org.eclipse.core.internal.content; 12 13 import java.io.*; 14 import org.eclipse.core.runtime.QualifiedName; 15 import org.eclipse.core.runtime.content.IContentDescription; 16 import org.eclipse.core.runtime.content.ITextContentDescriber; 17 18 29 public class TextContentDescriber implements ITextContentDescriber { 30 31 private final static QualifiedName[] SUPPORTED_OPTIONS = {IContentDescription.BYTE_ORDER_MARK}; 32 33 37 public int describe(Reader contents, IContentDescription description) throws IOException { 38 return INDETERMINATE; 40 } 41 42 46 public int describe(InputStream contents, IContentDescription description) throws IOException { 47 if (description == null || !description.isRequested(IContentDescription.BYTE_ORDER_MARK)) 48 return INDETERMINATE; 49 byte[] bom = getByteOrderMark(contents); 50 if (bom != null) 51 description.setProperty(IContentDescription.BYTE_ORDER_MARK, bom); 52 return INDETERMINATE; 54 } 55 56 60 public QualifiedName[] getSupportedOptions() { 61 return SUPPORTED_OPTIONS; 62 } 63 64 byte[] getByteOrderMark(InputStream input) throws IOException { 65 int first = (input.read() & 0xFF); int second = (input.read() & 0xFF); 67 if (first == -1 || second == -1) 68 return null; 69 if (first == 0xFE && second == 0xFF) 71 return IContentDescription.BOM_UTF_16BE; 72 if (first == 0xFF && second == 0xFE) 73 return IContentDescription.BOM_UTF_16LE; 74 int third = (input.read() & 0xFF); 75 if (third == -1) 76 return null; 77 if (first == 0xEF && second == 0xBB && third == 0xBF) 79 return IContentDescription.BOM_UTF_8; 80 return null; 81 } 82 83 } 84 | Popular Tags |