common/src/main/java/HTTPClient/Codecs.java
/**
     * Encodes data used the chunked encoding. <var>last</var> signales if
     * this is the last chunk, in which case the appropriate footer is
     * generated.
     *
     * @param data  the data to be encoded; may be null.
     * @param off   an offset into the <var>data</var>
     * @param len   the number of bytes to take from <var>data</var>
     * @param ftrs  optional headers to include in the footer (ignored if
     *              not last); may be null.
     * @param last  whether this is the last chunk.
     * @return an array of bytes containing the chunk
     */
Encodes data used the chunked encoding. last signales if
this is the last chunk, in which case the appropriate footer is
generated.

@param data  the data to be encoded; may be null.
@param off   an offset into the data
@param len   the number of bytes to take from data
@param ftrs  optional headers to include in the footer (ignored if
not last); may be null.
@param last  whether this is the last chunk.
@return an array of bytes containing the chunk
Encodes data used the chunked encoding . last signales if this is the last chunk in which case the appropriate footer is generated . @param data the data to be encoded ; may be null . @param off an offset into the data @param len the number of bytes to take from data @param ftrs optional headers to include in the footer ( ignored if not last ) ; may be null . @param last whether this is the last chunk . @return an array of bytes containing the chunk
public final static byte[] chunkedEncode(byte[] data, int off, int len,
					     NVPair[] ftrs, boolean last)
    {
	if (data == null)
	{
	    data = new byte[0];
	    len  = 0;
	}
	if (last  &&  ftrs == null) ftrs = new NVPair[0];
	String hex_len = Integer.toString(len, 16);
	int res_len = 0;
	if (len > 0)	
	    res_len += hex_len.length() + 2 + len + 2;
	if (last)
	{
	    res_len += 1 + 2;	
	    for (int idx=0; idx<ftrs.length; idx++)
		res_len += ftrs[idx].getName().length() + 2 +	
			   ftrs[idx].getValue().length() + 2;	
	    res_len += 2;	
	}
	byte[] res = new byte[res_len];
	int    r_off = 0;
	if (len > 0)
	{
	    int hlen = hex_len.length();
	    try
		{ System.arraycopy(hex_len.getBytes("8859_1"), 0, res, r_off, hlen); }
	    catch (UnsupportedEncodingException uee)
		{ throw new Error(uee.toString()); }
	    r_off += hlen;
	    res[r_off++] = (byte) '\r';
	    res[r_off++] = (byte) '\n';
	    System.arraycopy(data, off, res, r_off, len);
	    r_off += len;
	    res[r_off++] = (byte) '\r';
	    res[r_off++] = (byte) '\n';
	}
	if (last)
	{
	    res[r_off++] = (byte) '0';
	    res[r_off++] = (byte) '\r';
	    res[r_off++] = (byte) '\n';
	    for (int idx=0; idx<ftrs.length; idx++)
	    {
		int nlen = ftrs[idx].getName().length();
		try
		    { System.arraycopy(ftrs[idx].getName().getBytes("8859_1"),
				       0, res, r_off, nlen); }
		catch (UnsupportedEncodingException uee)
		    { throw new Error(uee.toString()); }
		r_off += nlen;
		res[r_off++] = (byte) ':';
		res[r_off++] = (byte) ' ';
		int vlen = ftrs[idx].getValue().length();
		try
		    { System.arraycopy(ftrs[idx].getValue().getBytes("8859_1"),
				       0, res, r_off, vlen); }
		catch (UnsupportedEncodingException uee)
		    { throw new Error(uee.toString()); }
		r_off += vlen;
		res[r_off++] = (byte) '\r';
		res[r_off++] = (byte) '\n';
	    }
	    res[r_off++] = (byte) '\r';
	    res[r_off++] = (byte) '\n';
	}
	if (r_off != res.length)
	    throw new Error("Calculated "+res.length+" bytes but wrote "+r_off+" bytes!");
	return res;
    }
public final static byte [ ] chunkedEncode ( byte [ ] data , int off , int len , NVPair [ ] ftrs , boolean last ) { if ( data == null ) { data = new byte [ 0 ] ; len = 0 ; } if ( last && ftrs == null ) ftrs = new NVPair [ 0 ] ; String hex_len = Integer . toString ( len , 16 ) ; int res_len = 0 ; if ( len > 0 ) res_len += hex_len . length ( ) + 2 + len + 2 ; if ( last ) { res_len += 1 + 2 ; for ( int idx = 0 ; idx < ftrs . length ; idx ++ ) res_len += ftrs [ idx ] . getName ( ) . length ( ) + 2 + ftrs [ idx ] . getValue ( ) . length ( ) + 2 ; res_len += 2 ; } byte [ ] res = new byte [ res_len ] ; int r_off = 0 ; if ( len > 0 ) { int hlen = hex_len . length ( ) ; try { System . arraycopy ( hex_len . getBytes ( "8859_1" ) , 0 , res , r_off , hlen ) ; } catch ( UnsupportedEncodingException uee ) { throw new Error ( uee . toString ( ) ) ; } r_off += hlen ; res [ r_off ++ ] = ( byte ) '\r' ; res [ r_off ++ ] = ( byte ) '\n' ; System . arraycopy ( data , off , res , r_off , len ) ; r_off += len ; res [ r_off ++ ] = ( byte ) '\r' ; res [ r_off ++ ] = ( byte ) '\n' ; } if ( last ) { res [ r_off ++ ] = ( byte ) '0' ; res [ r_off ++ ] = ( byte ) '\r' ; res [ r_off ++ ] = ( byte ) '\n' ; for ( int idx = 0 ; idx < ftrs . length ; idx ++ ) { int nlen = ftrs [ idx ] . getName ( ) . length ( ) ; try { System . arraycopy ( ftrs [ idx ] . getName ( ) . getBytes ( "8859_1" ) , 0 , res , r_off , nlen ) ; } catch ( UnsupportedEncodingException uee ) { throw new Error ( uee . toString ( ) ) ; } r_off += nlen ; res [ r_off ++ ] = ( byte ) ':' ; res [ r_off ++ ] = ( byte ) ' ' ; int vlen = ftrs [ idx ] . getValue ( ) . length ( ) ; try { System . arraycopy ( ftrs [ idx ] . getValue ( ) . getBytes ( "8859_1" ) , 0 , res , r_off , vlen ) ; } catch ( UnsupportedEncodingException uee ) { throw new Error ( uee . toString ( ) ) ; } r_off += vlen ; res [ r_off ++ ] = ( byte ) '\r' ; res [ r_off ++ ] = ( byte ) '\n' ; } res [ r_off ++ ] = ( byte ) '\r' ; res [ r_off ++ ] = ( byte ) '\n' ; } if ( r_off != res . length ) throw new Error ( "Calculated " + res . length + " bytes but wrote " + r_off + " bytes!" ) ; return res ; }
Encodes data used the chunked encoding.
Encodes data used the chunked encoding .
// get length of data as hex-string // calculate length of chunk // len CRLF data CRLF // 0 CRLF // name ": " // value CRLF // CRLF // allocate result // fill result