893N/A/*
3909N/A * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
893N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
893N/A *
893N/A * This code is free software; you can redistribute it and/or modify it
893N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
893N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
893N/A *
893N/A * This code is distributed in the hope that it will be useful, but WITHOUT
893N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
893N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
893N/A * version 2 for more details (a copy is included in the LICENSE file that
893N/A * accompanied this code).
893N/A *
893N/A * You should have received a copy of the GNU General Public License version
893N/A * 2 along with this work; if not, write to the Free Software Foundation,
893N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
893N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
893N/A */
893N/A
893N/Apackage sun.nio.fs;
893N/A
3471N/Aimport java.nio.file.Path;
893N/Aimport java.nio.file.spi.FileTypeDetector;
1319N/Aimport java.util.Locale;
893N/Aimport java.io.IOException;
893N/A
893N/A/**
893N/A * Base implementation of FileTypeDetector
893N/A */
893N/A
893N/Apublic abstract class AbstractFileTypeDetector
893N/A extends FileTypeDetector
893N/A{
893N/A protected AbstractFileTypeDetector() {
893N/A super();
893N/A }
893N/A
893N/A /**
1319N/A * Invokes the appropriate probe method to guess a file's content type,
1319N/A * and checks that the content type's syntax is valid.
893N/A */
893N/A @Override
3471N/A public final String probeContentType(Path file) throws IOException {
893N/A if (file == null)
893N/A throw new NullPointerException("'file' is null");
893N/A String result = implProbeContentType(file);
1319N/A return (result == null) ? null : parse(result);
893N/A }
893N/A
893N/A /**
893N/A * Probes the given file to guess its content type.
893N/A */
3471N/A protected abstract String implProbeContentType(Path file)
893N/A throws IOException;
1319N/A
1319N/A /**
1319N/A * Parses a candidate content type into its type and subtype, returning
1319N/A * null if either token is invalid.
1319N/A */
1319N/A private static String parse(String s) {
1319N/A int slash = s.indexOf('/');
1319N/A int semicolon = s.indexOf(';');
1319N/A if (slash < 0)
1319N/A return null; // no subtype
1319N/A String type = s.substring(0, slash).trim().toLowerCase(Locale.ENGLISH);
1319N/A if (!isValidToken(type))
1319N/A return null; // invalid type
1319N/A String subtype = (semicolon < 0) ? s.substring(slash + 1) :
1319N/A s.substring(slash + 1, semicolon);
1319N/A subtype = subtype.trim().toLowerCase(Locale.ENGLISH);
1319N/A if (!isValidToken(subtype))
1319N/A return null; // invalid subtype
1319N/A StringBuilder sb = new StringBuilder(type.length() + subtype.length() + 1);
1319N/A sb.append(type);
1319N/A sb.append('/');
1319N/A sb.append(subtype);
1319N/A return sb.toString();
1319N/A }
1319N/A
1319N/A /**
1319N/A * Special characters
1319N/A */
1319N/A private static final String TSPECIALS = "()<>@,;:/[]?=\\\"";
1319N/A
1319N/A /**
1319N/A * Returns true if the character is a valid token character.
1319N/A */
1319N/A private static boolean isTokenChar(char c) {
1319N/A return (c > 040) && (c < 0177) && (TSPECIALS.indexOf(c) < 0);
1319N/A }
1319N/A
1319N/A /**
1319N/A * Returns true if the given string is a legal type or subtype.
1319N/A */
1319N/A private static boolean isValidToken(String s) {
1319N/A int len = s.length();
1319N/A if (len == 0)
1319N/A return false;
1319N/A for (int i = 0; i < len; i++) {
1319N/A if (!isTokenChar(s.charAt(i)))
1319N/A return false;
1319N/A }
1319N/A return true;
1319N/A }
893N/A}