/* (non-Javadoc) * @see com.telenav.framework.data.INode#addValue(long) */ public void addValue(long value) { synchronized (this) { nValues++; checkValueBufferSize(value); valuesBuffOffset += DataUtil.writeValue(valuesBuff, value, valuesBuffOffset); } }
public void setValueAt(long value, int index) { synchronized (this) { int oldSize = getValuesSize(); for (int i = oldSize; i < index + 1; i++) { addValue(0); } int[] offset = new int[1]; for (int i = 0; i < index; i++) { DataUtil.readOffset(valuesBuff, offset); } int startOffset = offset[0]; DataUtil.readOffset(valuesBuff, offset); int endOffset = offset[0]; checkValueBufferSize(value); int newValLength = DataUtil.getValueLength(value); int oldValLength = endOffset - startOffset; if (newValLength != oldValLength) { // need to move old values short delta = (short) (newValLength - oldValLength); int len = valuesBuffOffset - endOffset; System.arraycopy(valuesBuff, endOffset, valuesBuff, endOffset + delta, len); valuesBuffOffset = (short) (valuesBuffOffset + delta); } DataUtil.writeValue(valuesBuff, value, startOffset); } }
/* (non-Javadoc) * @see com.telenav.framework.data.INode#toBinary() */ private byte[] toBinary55(int header, int ver) { int size = 0; if (ver == VERSION_55) { size = 1 + DataUtil.getValueLength(valuesBuffOffset) + valuesBuffOffset; // 1 byte for nValues } else if (ver == VERSION_551) { size = DataUtil.getValueLength(nValues) + DataUtil.getValueLength(valuesBuffOffset) + valuesBuffOffset; } // int nMsgs = 0; /* if (nMsgs > 0) { if (msgsBuff == null) { msgsBuff = new byte[nMsgs][]; } else if (msgsBuff.length < nMsgs) { byte[][] temp = new byte[nMsgs][]; System.arraycopy(msgsBuff, 0, temp, 0, msgsBuff.length); msgsBuff = temp; } } */ int len = 0; for (int i = 0; i < nMsgs; i++) { if (msgsBuff[i] != null) { len += (msgsBuff[i].length + DataUtil.getValueLength(msgsBuff[i].length)); } else if (ver == VERSION_55) { len += DataUtil.getValueLength(0); } else if (ver == VERSION_551) { len += DataUtil.getValueLength(-1); } } size += (DataUtil.getValueLength(nMsgs) + len); len = 0; if (nChildren > 0) { if (childrenBuff == null) { childrenBuff = new byte[nChildren][]; } else if (childrenBuff.length < nChildren) { byte[][] temp = new byte[nChildren][]; System.arraycopy(childrenBuff, 0, temp, 0, childrenBuff.length); childrenBuff = temp; } } for (int i = 0; i < nChildren; i++) { if (children != null && children[i] != null) { childrenBuff[i] = children[i].toBinary55(-1, ver); } if (childrenBuff[i] != null) { len += (childrenBuff[i].length + DataUtil.getValueLength(childrenBuff[i].length)); } else { len += (0 + DataUtil.getValueLength(0)); } } size += (DataUtil.getValueLength(nChildren) + len); int binLen = 0; if (binData != null) { binLen = binData.length; } size += (DataUtil.getValueLength(binLen) + binLen); if (header == HEADER_55 || header == HEADER_551) size += 4; byte[] buff = new byte[size]; int offset = 0; if (header == HEADER_55 || header == HEADER_551) { DataUtil.writeInt(buff, header, offset); offset += 4; } // do values if (ver == VERSION_55) { buff[offset++] = (byte) nValues; } else if (ver == VERSION_551) { offset += DataUtil.writeValue(buff, nValues, offset); } offset += DataUtil.writeValue(buff, valuesBuffOffset, offset); if (nValues > 0) { System.arraycopy(valuesBuff, 0, buff, offset, valuesBuffOffset); offset += valuesBuffOffset; } // do msgs offset += DataUtil.writeValue(buff, nMsgs, offset); for (int i = 0; i < nMsgs; i++) { if (msgsBuff[i] != null) { offset += DataUtil.writeValue(buff, msgsBuff[i].length, offset); System.arraycopy(msgsBuff[i], 0, buff, offset, msgsBuff[i].length); offset += msgsBuff[i].length; } else { if (ver == VERSION_55) { // 0 length indicate an empty string (relaxed protocol) offset += DataUtil.writeValue(buff, 0, offset); } else if (ver == VERSION_551) { // -1 length indicate a null string (to make it different from "" string) offset += DataUtil.writeValue(buff, -1, offset); } } } // do children offset += DataUtil.writeValue(buff, nChildren, offset); for (int i = 0; i < nChildren; i++) { if (childrenBuff[i] != null) { offset += DataUtil.writeValue(buff, childrenBuff[i].length, offset); System.arraycopy(childrenBuff[i], 0, buff, offset, childrenBuff[i].length); offset += childrenBuff[i].length; } else { offset += DataUtil.writeValue(buff, 0, offset); } } // do binData offset += DataUtil.writeValue(buff, binLen, offset); if (binData != null) { System.arraycopy(binData, 0, buff, offset, binData.length); offset += binData.length; } // System.out.println(buff.length+" == "+offset); return buff; }