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

📄 payload.java

📁 Open DMT GPS server source code
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            // nothing to read            return dft;        } else {            byte b[] = this._getBytes();            double val = Payload.decodeDouble(b, this.index, maxLen, this.bigEndian, dft);            this.index += maxLen;            return val;        }    }    /* read double value from payload */    public double readDouble(int length)    {        // 'length' must be at least 4        return this.readDouble(length, 0.0);    }    // ------------------------------------------------------------------------    /* read a variable length string from the payload */    public String readString(int length)    {        return this.readString(length, true);    }        /* read a string from the payload */    public String readString(int length, boolean varLength)    {        // Read until (whichever comes first):        //  1) length bytes have been read        //  2) a null (0x00) byte is found (if 'varLength==true')        //  3) end of data is reached        int maxLen = ((this.index + length) <= this.size)? length : (this.size - this.index);        if (maxLen <= 0) {            // no room left            return "";        } else {            int m;            byte b[] = this._getBytes();            if (varLength) {                // look for the end-of-data, or a terminating null (0x00)                for (m = 0; (m < maxLen) && ((this.index + m) < this.size) && (b[this.index + m] != 0); m++);            } else {                // look for end of data only                m = ((this.index + maxLen) < this.size)? maxLen : (this.size - this.index);            }            String s = StringTools.toStringValue(b, this.index, m);            this.index += m;            if (m < maxLen) { this.index++; }            return s;        }    }        // ------------------------------------------------------------------------    /* read an encoded GPS point (latitude,longitude) from the payload */    public GeoPoint readGPS(int length)    {        int maxLen = ((this.index + length) <= this.size)? length : (this.size - this.index);        if (maxLen < 6) {            // not enough bytes to decode GeoPoint            GeoPoint gp = new GeoPoint();            if (maxLen > 0) { this.index += maxLen; }            return gp;        } else        if (length < 8) {            // 6 <= len < 8            GeoPoint gp = GeoPoint.decodeGeoPoint(this._getBytes(), this.index, length);            this.index += maxLen; // 6            return gp;        } else {            // 8 <= len            GeoPoint gp = GeoPoint.decodeGeoPoint(this._getBytes(), this.index, length);            this.index += maxLen; // 8            return gp;        }    }    // ------------------------------------------------------------------------    /* encode a long value into the specified byte array */    public static int encodeLong(byte data[], int ofs, int len, boolean bigEndian, long val)    {        if ((data != null) && (data.length >= (ofs + len))) {            long n = val;            if (bigEndian) {                // Big-Endian order                for (int i = (ofs + len - 1); i >= ofs; i--) {                    data[i] = (byte)(n & 0xFF);                    n >>>= 8;                }            } else {                // Little-Endian order                for (int i = ofs; i < ofs + len; i++) {                    data[i] = (byte)(n & 0xFF);                    n >>>= 8;                }            }            return len;        } else {            return 0;        }    }    /* write the specified long to the payload */    public int writeLong(long val, int wrtLen)    {        /* check for nothing to write */        if (wrtLen <= 0) {            // nothing to write            return 0;        }        /* write float/double */        byte b[] = this._getBytes();        int maxLen = ((this.index + wrtLen) <= b.length)? wrtLen : (b.length - this.index);        if (maxLen < wrtLen) {            // not enough bytes to encode long            return 0;        }        /* write long */        Payload.encodeLong(b, this.index, maxLen, this.bigEndian, val);        this.index += maxLen;        if (this.size < this.index) { this.size = this.index; }        return maxLen;            }    /* write the specified long (as unsigned) to the payload */    public int writeULong(long val, int length)    {        return this.writeLong(val, length);    }    // ------------------------------------------------------------------------    /* encode a long value into the specified byte array */    public static int encodeDouble(byte data[], int ofs, int len, boolean bigEndian, double val)    {        // 'len' must be at least 4        if ((data != null) && (len >= 4) && (data.length >= (ofs + len))) {            int flen = (len >= 8)? 8 : 4;            long n = (flen == 8)? Double.doubleToRawLongBits(val) : (long)Float.floatToRawIntBits((float)val);            if (bigEndian) {                // Big-Endian order                for (int i = (ofs + flen - 1); i >= ofs; i--) {                    data[i] = (byte)(n & 0xFF);                    n >>>= 8;                }            } else {                // Little-Endian order                for (int i = ofs; i < ofs + flen; i++) {                    data[i] = (byte)(n & 0xFF);                    n >>>= 8;                }            }            return len;        } else {            return 0;        }    }    /* write the specified long to the payload */    public int writeDouble(double val, int wrtLen)    {        // 'wrtLen' should be either 4 or 8        /* check for nothing to write */        if (wrtLen <= 0) {            // nothing to write            return 0;        }        /* write float/double */        byte b[] = this._getBytes();        int maxLen = ((this.index + wrtLen) <= b.length)? wrtLen : (b.length - this.index);        if (maxLen < 4) {            // not enough bytes to encode float/double            return 0;        }        /* write float/double */        if (wrtLen < 8) {            // 4 <= wrtLen < 8  [float]            int len = Payload.encodeDouble(b, this.index, 4, this.bigEndian, val);            this.index += 4;            if (this.size < this.index) { this.size = this.index; }            return 4;        } else {            // 8 <= wrtLen      [double]            int len = Payload.encodeDouble(b, this.index, 8, this.bigEndian, val);            this.index += 8;            if (this.size < this.index) { this.size = this.index; }            return 8;        }            }    // ------------------------------------------------------------------------    /* write a zero-filled buffer to the payload */    public int writeZeroFill(int wrtLen)    {        /* check for nothing to write */        if (wrtLen <= 0) {            // nothing to write            return 0;        }        /* check for available space to write the data */        byte b[] = this._getBytes();        int maxLen = ((this.index + wrtLen) <= b.length)? wrtLen : (b.length - this.index);        if (maxLen <= 0) {            // no room left            return 0;        }        /* fill field bytes with '0's, and adjust pointers */        for (int m = 0; m < maxLen; m++) { b[this.index + m] = 0; }        this.index += maxLen;        if (this.size < this.index) { this.size = this.index; }        /* return number of bytes written */        return maxLen;    }    // ------------------------------------------------------------------------    /* write an array of bytes to the payload */    public int writeBytes(byte n[], int nOfs, int nLen, int wrtLen)    {        /* check for nothing to write */        if (wrtLen <= 0) {            // nothing to write            return 0;        }        /* adjust nOfs/nLen to fit within the byte array */        if ((nOfs < 0) || (nLen <= 0) || (n == null)) {            // invalid offset/length, or byte array            return this.writeZeroFill(wrtLen);        } else        if (nOfs >= n.length) {            // 'nOfs' is outside the array, nothing to write            return this.writeZeroFill(wrtLen);        } else        if ((nOfs + nLen) > n.length) {            // 'nLen' would extend beyond the end of the array            nLen = n.length - nOfs; // nLen will be > 0        }        /* check for available space to write the data */        byte b[] = this._getBytes();        int maxLen = ((this.index + wrtLen) <= b.length)? wrtLen : (b.length - this.index);        if (maxLen <= 0) {            // no room left            return 0;        }        /* write byte field */        // copy 'm' bytes to buffer at current index        int m = (nLen < maxLen)? nLen : maxLen;        System.arraycopy(n, nOfs, b, this.index, m);        /* fill remaining field bytes with '0's, and adjust pointers */        for (;m < maxLen; m++) { b[this.index + m] = 0; }        this.index += maxLen;        if (this.size < this.index) { this.size = this.index; }        /* return number of bytes written */        return maxLen;    }    /* write an array of bytes to the payload */    public int writeBytes(byte n[], int wrtLen)    {        return (n == null)?             this.writeZeroFill(wrtLen) :            this.writeBytes(n, 0, n.length, wrtLen);    }    // ------------------------------------------------------------------------    /* write a string to the payload */    public int writeString(String s, int wrtLen)    {        /* check for nothing to write */        if (wrtLen <= 0) {            // nothing to write            return 0;        }        /* check for available space to write the data */        byte b[] = this._getBytes();        int maxLen = ((this.index + wrtLen) <= b.length)? wrtLen : (b.length - this.index);        if (maxLen <= 0) {            // no room left            return 0;        }                /* empty string ('maxLen' is at least 1) */        if ((s == null) || s.equals("")) {            b[this.index++] = (byte)0;  // string terminator            if (this.size < this.index) { this.size = this.index; }            return 1;        }        /* write string bytes, and adjust pointers */        byte n[] = StringTools.getBytes(s);        int m = (n.length < maxLen)? n.length : maxLen;        System.arraycopy(n, 0, b, this.index, m);        this.index += m;        if (m < maxLen) {             b[this.index++] = (byte)0; // terminate string            m++;        }        if (this.size < this.index) { this.size = this.index; }        /* return number of bytes written */        return m;    }    // ------------------------------------------------------------------------    /* encode a GPS point into the payload */    public int writeGPS(double lat, double lon, int length)    {        return this.writeGPS(new GeoPoint(lat,lon), length);    }        /* encode a GPS point into the payload */    public int writeGPS(GeoPoint gp, int wrtLen)    {        /* check for nothing to write */        if (wrtLen <= 0) {            // nothing to write            return 0;        }        /* check for available space to write the data */        byte b[] = this._getBytes();        int maxLen = ((this.index + wrtLen) <= b.length)? wrtLen : (b.length - this.index);        if (maxLen < 6) {            // not enough bytes to encode GeoPoint            return 0;        }                /* write GPS point */        if (wrtLen < 8) {            // 6 <= wrtLen < 8            int len = 6;            GeoPoint.encodeGeoPoint(gp, b, this.index, len);            this.index += len;            if (this.size < this.index) { this.size = this.index; }            // TODO: zero-fill (wrtLen - len) bytes?            return len;        } else {            // 8 <= wrtLen            int len = 8;            GeoPoint.encodeGeoPoint(gp, b, this.index, len);            this.index += len;            if (this.size < this.index) { this.size = this.index; }            // TODO: zero-fill (wrtLen - len) bytes?            return len;        }            }    // ------------------------------------------------------------------------    /* return hex string representation */    public String toString()    {        return StringTools.toHexString(this.payload, 0, this.size);    }        // ------------------------------------------------------------------------    // ------------------------------------------------------------------------}

⌨️ 快捷键说明

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