📄 mimemediatype.java
字号:
int calcedHash = type.hashCode() * 2467 + subtype.hashCode() * 3943 + myParams.hashCode(); cachedHashCode = (0 != calcedHash) ? calcedHash : 1; } return cachedHashCode; } /** * {@inheritDoc} */ @Override public String toString() { StringBuilder retValue = new StringBuilder(getMimeMediaType()); for (parameter parameter : parameters) { retValue.append(';'); parameter aParam = parameter; retValue.append(aParam.toString()); } return retValue.toString(); } /** * Returns the "base" MIME media type of this type. ie. with no parameters. * * @return The "base" MIME media type of this MimeMediaType. */ public MimeMediaType getBaseMimeMediaType() { return new MimeMediaType(type, subtype).intern(); } /** * Returns the "base" MIME media type of this type. ie. with no parameters. * * @return The "base" MIME media type of this type. ie. with no parameters. */ public String getMimeMediaType() { StringBuilder retValue = new StringBuilder(type.length() + 1 + subtype.length()); retValue.append(type); retValue.append('/'); retValue.append(subtype); return retValue.toString(); } /** * Get type of the mime-type * * @return type of the mime-type */ public String getType() { return type; } /** * Check if the mime-type is for provisional. See Section 2.1 of * {@link <a href=http://www.ietf.org/rfc/rfc2048.txt">IETF RFC 2048 <i>MIME : Registration Procedures</i></a>} * * @return boolean true if it is a provisional type */ public boolean isExperimentalType() { if ((null == type) || (type.length() < 2)) { return false; } if (type.startsWith("x-") || type.startsWith("x.")) { return true; } return null != subtype && subtype.length() >= 2 && (subtype.startsWith("x-") || subtype.startsWith("x.")); } /** * Set the type of MimeMediaType * * @param type type value */ private void setType(String type) { if (null == type) { throw new IllegalArgumentException("type cannot be null"); } String cleaned = type.trim().toLowerCase(Locale.US); if (0 == cleaned.length()) { throw new IllegalArgumentException("type cannot be null"); } if (-1 != findNextSeperator(cleaned)) { throw new IllegalArgumentException("type cannot contain a seperator"); } this.type = cleaned; } /** * Get the Subtype of the mime-type * * @return subtype of the mime-type */ public String getSubtype() { return subtype; } /** * Check if the mime-type is for debugging. This method will be * removed * * @return boolean true if it is a debugging type */ public boolean isExperimentalSubtype() { if ((null == subtype) || (subtype.length() < 2)) { return false; } return (('x' == subtype.charAt(0)) && ('-' == subtype.charAt(1))); } /** * Set the subtype of MimeMediaType * * @param subtype subtype value */ private void setSubtype(String subtype) { if (null == subtype) { throw new IllegalArgumentException("subtype cannot be null"); } String cleaned = subtype.trim().toLowerCase(Locale.US); if (0 == cleaned.length()) { throw new IllegalArgumentException("subtype cannot be null"); } if (-1 != findNextSeperator(cleaned)) { throw new IllegalArgumentException("subtype cannot contain a seperator"); } this.subtype = cleaned; } /** * Get the value of the first occurance of the specified parameter from the * parameter list. * * @param param the parameter to retrieve. * @return the value of the specifid parameter or null if the parameter was * not found. */ public String getParameter(String param) { for (parameter parameter : parameters) { parameter aParam = parameter; if (aParam.attribute.equalsIgnoreCase(param)) { return aParam.value; } } return null; } /** * Parses the parametes portion of a MIME Media Type specification. * * @param itsParams parse a string for mime parameters. * @param replace parameters if true then provided params should replace * existing params else they are accumulated. */ private void parseParams(String itsParams, boolean replace) { int currentCharIdx = 0; String currentAttribute = null; boolean inSeperator = true; boolean inComment = false; boolean inAttribute = false; StringBuffer currentValue = null; boolean inValue = false; boolean inQuoted = false; boolean nextEscaped = false; while (currentCharIdx < itsParams.length()) { char currentChar = itsParams.charAt(currentCharIdx); if (inSeperator) { if ('(' == currentChar) { inSeperator = false; inComment = true; } else if (-1 == param_sep.indexOf(currentChar)) { inSeperator = false; inAttribute = true; currentCharIdx--; // unget } } else if (inComment) { if (nextEscaped) { nextEscaped = false; } else { if ('\\' == currentChar) { nextEscaped = true; } else if (')' == currentChar) { inComment = false; inSeperator = true; } else if ('\r' == currentChar) { throw new IllegalArgumentException("malformed mime parameter at idx = " + currentCharIdx); } } } else if (inAttribute) { int endAttr = findNextSeperator(itsParams, currentCharIdx); if ((-1 == endAttr) || ('=' != itsParams.charAt(endAttr)) || (0 == (endAttr - currentCharIdx))) { throw new IllegalArgumentException("malformed mime parameter at idx = " + currentCharIdx); } currentAttribute = itsParams.substring(currentCharIdx, endAttr).toLowerCase(Locale.US); currentCharIdx = endAttr; // skip the equals. inAttribute = false; inValue = true; inQuoted = false; nextEscaped = false; currentValue = new StringBuffer(); } else if (inValue) { if (inQuoted) { if (nextEscaped) { currentValue.append(currentChar); nextEscaped = false; } else { if ('\\' == currentChar) { nextEscaped = true; } else if ('"' == currentChar) { inQuoted = false; } else if ('\r' == currentChar) { throw new IllegalArgumentException("malformed mime parameter at idx = " + currentCharIdx); } else { currentValue.append(currentChar); } } } else if (-1 == terminator.indexOf(currentChar)) { currentValue.append(currentChar); } else { if ('"' == currentChar) { inQuoted = true; } else { parameter newparam = new parameter(currentAttribute, currentValue.toString()); if (replace) { while (parameters.remove(newparam)) {} } parameters.add(newparam); inValue = false; inSeperator = true; currentCharIdx--; // unget } } } else { throw new IllegalArgumentException("malformed mime parameter at idx = " + currentCharIdx); } currentCharIdx++; } // finish off the last value. if (inValue) { if (nextEscaped || inQuoted) { throw new IllegalArgumentException("malformed mime parameter at idx = " + currentCharIdx); } parameter newparam = new parameter(currentAttribute, currentValue.toString()); if (replace) { while (parameters.remove(newparam)) {} } parameters.add(newparam); inValue = false; inSeperator = true; } if (!inSeperator) { throw new IllegalArgumentException("malformed mime parameter at idx = " + currentCharIdx); } } /** * Find next separator position in mime-type * * @param source source location * @return int separator location */ private static int findNextSeperator(String source) { return findNextSeperator(source, 0); } /** * Find next separator position in mime-type * * @param source source location * @param from from location * @return int separator location */ private static int findNextSeperator(String source, int from) { int seperator = -1; // find a seperator for (int eachChar = from; eachChar < source.length(); eachChar++) { if (-1 != terminator.indexOf(source.charAt(eachChar))) { seperator = eachChar; break; } } return seperator; } /** * Returns a MimeMediaType with a value represented by the specified String. * <p/> * <b>This method may produce better results than using the constructor * the same parameter set in that</b>: * <p/> * <code> * new MimeMediaType( string ) != new MimeMediaType( string ) * </code> * <p/> * while for common types: * <p/> * <code> * MimeMediaType.valueOf( string ) == MimeMediaType.valueOf( string ) * </code> */ public static MimeMediaType valueOf(String mimetype) { return new MimeMediaType(mimetype).intern(); } /** * Read this Object in for Java Serialization * * @param s The stream from which the Object will be read. * @throws IOException for errors reading from the input stream. * @throws ClassNotFoundException if the serialized representation contains * references to classes which cannot be found. */ private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException { s.defaultReadObject(); MimeMediaType readType = MimeMediaType.valueOf(s.readUTF()); type = readType.type; subtype = readType.subtype; parameters = readType.parameters; } /** * Return the interned form of the ID. */ private Object readResolve() throws ObjectStreamException { return intern(); } /** * Write this Object out for Java Serialization * * @param s The stream to which the Object will be written. * @throws IOException for errors writing to the output stream. */ private void writeObject(ObjectOutputStream s) throws IOException { s.defaultWriteObject(); s.writeUTF(toString()); } /** * Returns a canonical representation for the MimeMediaType object. * <p/> * <p/>A pool of MimeMediaType, is maintained privately by the class. * <p/> * <p/>When the intern method is invoked, if the pool already contains a * MimeMediaType equal to this MimeMediaType object as determined by the * equals(Object) method, then the MimeMediaType from the pool is returned. * Otherwise, this MimeMediaType object is added to the pool and a reference * to this MimeMediaType object is returned. * <p/> * <p/>It follows that for any two MimeMediaType <tt>s</tt> and <tt>t</tt>, * <tt>s.intern() == t.intern()</tt> is true if and only if <tt>s.equals(t)</tt> * is true. * * @return a MimeMediaType that has the same value as this type, but is * guaranteed to be from a pool of unique types. */ public MimeMediaType intern() { synchronized (MimeMediaType.class) { Reference<MimeMediaType> common = interned.get(this); MimeMediaType result; if (null == common) { common = new WeakReference<MimeMediaType>(this); interned.put(this, common); result = this; } else { result = common.get(); if (null == result) { interned.put(this, new WeakReference<MimeMediaType>(this)); result = this; } } return result; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -