⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mimeheaders.java

📁 低版本的tomcat 对于有些老版本的应用还真的需要老版的中间件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    }

    public void addIntHeader(String name, int i) {
        addHeader(name).setIntValue(i);
    }

    /**
     * Creates a new header field whose value is the specified time.
     * The encoding uses RFC 822 date format, as updated by RFC 1123.
     * @param name the header name
     * @param t the time in number of milliseconds since the epoch
     */
    public void putDateHeader(String name, long t) {
	putHeader(name).setDateValue(t);
    }

    public void addDateHeader(String name, long t) {
        addHeader(name).setDateValue(t);
    }

    /**
     * Returns the string value of one of the headers with the
     * specified name.
     * @see getHeaders
     * @param name the header field name
     * @return the string value of the field, or null if none found
     */
    public String getHeader(String name) {
	MimeHeaderField mh = find(name);

	return mh != null ? mh.getValue() : null;
    }

    /**
     * Returns the string value of all of the headers with the
     * specified name.
     * @see getHeader
     * @param name the header field name
     * @return array values of the fields, or null if none found
     */
    public String[] getHeaders(String name) {
	Vector values = getHeadersVector(name);

	if (values.size() > 0) {
	    String retval[] = new String[values.size()];

	    for (int i = 0; i < retval.length; i++)
		retval[i] = (String)values.elementAt(i);
	    return retval;
	}
	return null;
    }

    /** Same as getHeaders, return a Vector - avoid Vector-[]-Vector conversion
     */
    public Vector getHeadersVector(String name) {
	Vector values = new Vector();

	for (int i = 0; i < count; i++) {
	    if (headers[i].nameEquals(name))
		values.addElement(headers[i].getValue());
	}

	return values;
    }

    /**
     * Returns the integer value of a header with the specified name.
     * @param name the header field name
     * @return the integer value of the header field, or -1 if the header
     *	       was not found
     * @exception NumberFormatException if the integer format was invalid
     */

    public int getIntHeader(String name) throws NumberFormatException {
	MimeHeaderField mh = find(name);

	return mh != null ? mh.getIntValue() : -1;
    }

    /**
     * Returns the date value of a header with the specified name.
     * @param name the header field name
     * @return the date value of the header field in number of milliseconds
     *	       since the epoch, or -1 if the header was not found
     * @exception IllegalArgumentException if the date format was invalid
     */
    public long getDateHeader(String name) throws IllegalArgumentException {
	MimeHeaderField mh = find(name);

	return mh != null ? mh.getDateValue() : -1;
    }

    /**
     * Returns the name of the nth header field where n >= 0. Returns null
     * if there were fewer than (n + 1) fields. This can be used to iterate
     * through all the fields in the header.
     */
    public String getHeaderName(int n) {
	return n >= 0 && n < count ? headers[n].getName() : null;
    }

    /**
     * Returns the body of the nth header field where n >= 0. Returns null
     * if there were fewer than (n + 1) fields. This can be used along
     * with getHeaderName to iterate through all the fields in the header.
     */
    public String getHeader(int n) {
	return n >= 0 && n < count ? headers[n].getValue() : null;
    }

    /**
     * Returns the number of fields using a given field name.
     */
    public int getFieldCount (String name) {
	int retval = 0;

	for (int i = 0; i < count; i++)
	    if (headers [i].nameEquals (name))
		retval++;

	return retval;
    }

    /**
     * Removes a header field with the specified name.  Does nothing
     * if such a field could not be found.
     * @param name the name of the header field to be removed
     */
    public void removeHeader(String name) {
        // XXX
        // warning: rather sticky code; heavily tuned

        for (int i = 0; i < count; i++) {
	    if (headers[i].nameEquals(name)) {
	        // reset and swap with last header
	        MimeHeaderField mh = headers[i];

		mh.reset();
		headers[i] = headers[count - 1];
		headers[count - 1] = mh;

		count--;
		i--;
	    }
	}
    }

    /**
     * Returns true if the specified field is contained in the header,
     * otherwise returns false.
     * @param name the field name
     */
    public boolean containsHeader(String name) {
	return find(name) != null;
    }


    /**
     * Finds a header field given name.  If the header doesn't exist,
     * it will create a new one.
     * @param name the header field name
     * @return the new field
     */
    protected MimeHeaderField putHeader(String name) {
        if (containsHeader(name)) {
	    removeHeader(name);
	}

	return addHeader(name);
    }

    protected MimeHeaderField addHeader(String name) {
 	MimeHeaderField mh = putHeader();

	mh.setName(name);

	return mh;
    }
    
    /**
     * Creates a new header with given name, and add it to the headers.
     * @param name the header field name
     * @param s the header value
     * @return the new field
     */
    public void appendHeader(String name, String s) {
	MimeHeaderField mh = putHeader();

	mh.setName(name);
	mh.setValue(s);
    }
    
    
    /**
     * Returns a lengthly string representation of the current header fields.
     */
    public String toString() {
	StringBuffer sb = new StringBuffer();

	sb.append("{");

	for (int i = 0; i < count; i++) {
	    sb.append("{");
	    sb.append(headers[i].toString());
	    sb.append("}");

	    if (i < count - 1) {
		sb.append(",");
	    }
	}

	sb.append("}");

	return sb.toString();
    }

    /**
     * Dumps current headers to specified PrintStream for debugging.
     */

    public void dump(PrintStream out) {
	for (int i = 0; i < count; i++) {
	    out.println(headers[i]);
	}
    }
}

class MimeHeadersEnumerator implements Enumeration {
    private static StringManager sm =
        StringManager.getManager("org.apache.tomcat.util");
    private Hashtable hash;
    private Enumeration delegate;

    MimeHeadersEnumerator(MimeHeaders headers) {
        // Store header names in a Hashtable to guarantee uniqueness
        // This has the side benefit of letting us use Hashtable's enumerator
        hash = new Hashtable();
        int size = headers.size();
        for (int i = 0; i < size; i++) {
            hash.put(headers.getHeaderName(i), "");
        }
        delegate = hash.keys();
    }

    public boolean hasMoreElements() {
	return delegate.hasMoreElements();
    }

    public Object nextElement() {
        try {
            return delegate.nextElement();
        }
        catch (NoSuchElementException e) {
            String msg = sm.getString("mimeHeaderEnumerator.next.nse");
	    throw new NoSuchElementException(msg);
	}
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -