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

📄 syncsettingwriter.java

📁 本文档讲解了OTA的概念
💻 JAVA
字号:
/**
 * 
 */
package com.ultrapower.syncSettingsXML;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.kxml.wap.WbxmlWriter;

/**
 * @author VictorZheng
 *
 */
public class SyncSettingWriter  extends WbxmlWriter {
    
	int nOutBufferLength = 0;
	public int size()
	{
		return nOutBufferLength;
	}
	
	ByteArrayOutputStream out = new ByteArrayOutputStream();
	ByteArrayOutputStream buf = new ByteArrayOutputStream();
	
    // ------------------------------------------------------------ Private data
    
    /**
     * Calls constructor of WbxmlWriter, sets then the appropriate tag table
     * and puts the requested DTD into string table. The DTD is choosen 
     * accordingly to the given namespace.
     *
     * @param namespace the namespace that identifies the DTD to use
     * @param version the DTD version to use
     * 
     * @param out an <code>OutputStream</code> value
     * @exception IOException if an error occurs
     */
    public SyncSettingWriter() throws IOException {
		
        super(new ByteArrayOutputStream());
		
		nOutBufferLength = 0;
    }
    
    /**
     * 填写每一个短信的头部
     *
     * @exception IOException if an error occurs
     */
    void writeHeader(int  nTotalNoSegments, int nSegmentCount) throws IOException {
        out.write(WBXMLToken.WDP_UDHL_LENGTH); 
        out.write(WBXMLToken.UDH_IE_PORT_NUMBERS); 
        out.write(WBXMLToken.UDH_PORT_NUMBER_IE_LENGTH); 
        out.write(WBXMLToken.DEST_PORT_HIGH); 
		out.write(WBXMLToken.DEST_PORT_LOW); 
        out.write(WBXMLToken.ORIG_PORT_HIGH); 
        out.write(WBXMLToken.ORIG_PORT_LOW); 
        out.write(WBXMLToken.UDH_IE_SAR); 
		out.write(WBXMLToken.UDH_SAR_IE_LENGTH); 
        out.write(WBXMLToken.DATAGRAM_REF_NO); 
		
		// 写入这次短信共有几个包,本短信是第几个包
		out.write(nTotalNoSegments);
		out.write(nSegmentCount);
		
		// 前面写入了12个字节
		nOutBufferLength += 12;
		
		// 如果是第一个短信包,还需要写入WSP Layer
		if(nSegmentCount == 1)
		{
			out.write(WBXMLToken.WSP_PDUID); 
	        out.write(WBXMLToken.PDU_TYPE); 
			out.write(WBXMLToken.HEADERS_LENGTH); 
	        out.write(WBXMLToken.VALUE_LENGTH); 
			out.write(WBXMLToken.SHORT_LENGTH); 
	        out.write(WBXMLToken.CONTENT_CODE1); 
			out.write(WBXMLToken.CONTENT_CODE2); 
	        out.write(WBXMLToken.CHARSET); 
			out.write(WBXMLToken.WSP_UTF_8); 
			
			/*
			 * WBXML头
			 */
			out.write(WBXMLToken.WBXML_VERSION_11); 
			out.write(WBXMLToken.WBXML_PUBLICID); 
	        out.write(WBXMLToken.WBXML_CHARSET_UTF8); 
			out.write(WBXMLToken.SWITCH_PAGE); 
			
			// 前面写入了12个字节
			nOutBufferLength += 13;
		}
    }
        
    /**
     * Writes a string as an OPAQUE field. An OPAQUE field is defined by WBXML
     * specification as follows:
     * <pre>
     *  opaque	= OPAQUE length *byte
     * </pre>
     * where OPAQUE is the byte 0xC3.
     *
     * @param str the string to write
     *
     * @throws IOException in case <i>super.write()</i> throws it
     */
    public void writeOpaque(String str) throws IOException {
        char[] chars = new char[(int)str.length()];
        
        str.getChars(0, chars.length, chars, 0);
        
        write(chars, 0, chars.length, true);
    }
	
	/*
	 * 向缓冲区里面写入integer数据
	 */
	public void writeBuffer(int nData) throws IOException {
        buf.write(nData);
		// 前面写入了1个字节
		nOutBufferLength++;		
    }
	/*
	 * 向缓冲区里面写入string数据
	 */
	public void writeBuffer(String strData) throws IOException {
        buf.write(strData.getBytes());
		// 前面写入了多少个字节
		nOutBufferLength += strData.getBytes().length;		
    }
    
    /**
     * Writes out the whole WBXML stream.
     *
     * @exception IOException if an error occurs while writing
     */
    public void close(int  nTotalNoSegments, int nSegmentCount) throws IOException {
        this.writeHeader(nTotalNoSegments, nSegmentCount);
        out.write(buf.toByteArray());
        out.flush(); // ready!
    }
    
    /**
     * Writes the content of this writer to another writer
     *
     * @param the writer to write to
     *
     */
    public byte[] getBytes() {
        return ((ByteArrayOutputStream)out).toByteArray();
    }
}

⌨️ 快捷键说明

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