📄 mimemediatype.java
字号:
currentAttribute = itsParams.substring( currentCharIdx, endAttr ).toLowerCase(); 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 ); } } /** * {@inheritDoc} **/ public boolean equals(Object obj) { if (this == obj) { return true; } if( !(obj instanceof MimeMediaType) ) { return false; } MimeMediaType asMMT = (MimeMediaType) obj; if( !type.equalsIgnoreCase( asMMT.type ) ) { return false; } if( !subtype.equalsIgnoreCase( asMMT.subtype ) ) { return false; } List myParams = new ArrayList( parameters ); List itsParams = new ArrayList( asMMT.parameters ); Collections.sort( myParams ); Collections.sort( itsParams ); return myParams.equals( itsParams ); } /** * Similar to {@link #equals(Object)}, but ignores any parameters. Compares * only the type and sub-type. **/ public boolean equalsIngoringParams(Object obj) { if (this == obj) { return true; } if( !(obj instanceof MimeMediaType) ) { return false; } MimeMediaType likeMe = (MimeMediaType) obj; boolean retValue = getType().equalsIgnoreCase( likeMe.getType() ) && getSubtype().equalsIgnoreCase( likeMe.getSubtype() ); return retValue; } /** * {@inheritDoc} **/ public int hashCode() { List myParams = new ArrayList( parameters ); Collections.sort( myParams ); return type.hashCode() * 2467 + subtype.hashCode() * 3943 + myParams.hashCode(); } /** * {@inheritDoc} **/ public String toString() { StringBuffer retValue = new StringBuffer( getMimeMediaType() ); Iterator eachParameter = parameters.iterator(); while( eachParameter.hasNext() ) { retValue.append( ';' ); parameter aParam = (parameter)eachParameter.next(); retValue.append( aParam.toString() ); } return retValue.toString(); } /** * Get the "root" mime-type/subtype without any of the parameters. * * @return full mime-type/subtype **/ public String getMimeMediaType() { StringBuffer retValue = new StringBuffer( 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; } if( (null == subtype) || (subtype.length() < 2) ) { return false; } return ( 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(); 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(); 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( Iterator eachParam = parameters.iterator(); eachParam.hasNext(); ) { parameter aParam = (parameter) eachParam.next(); if ( aParam.attribute.equalsIgnoreCase( param ) ) { return aParam.value; } } return null; } /** * 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. * * <b>This method may produce better results than using the constructor * the same parameter set in that</b>: * * <code> * new MimeMediaType( string ) != new MimeMediaType( string ) * </code> * * while for common types: * * <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; } /** * Normalize any common types. **/ 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/>A pool of MimeMediaType, is maintained privately by the class. * * <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/>It follows that for any two MimeMediaType s and t, s.intern() == * t.intern() is true if and only if s.equals(t) 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 ) { MimeMediaType common = (MimeMediaType) interned.get( this ); if( null == common ) { interned.put( this, this ); common = this; } return common; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -