DataStreamCache.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package org.ts.ddcs.dataStream;
  2. import lombok.NoArgsConstructor;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import java.util.Set;
  6. /**
  7. * 数据缓存
  8. *
  9. * @author swp
  10. */
  11. @NoArgsConstructor
  12. public abstract class DataStreamCache {
  13. private TestState state = TestState.LOOK;
  14. private byte[] dataBuff;
  15. private int readSize;
  16. private Map<String, Object> keyStore = new HashMap<>();
  17. public byte[] getDataBuff() {
  18. return this.dataBuff;
  19. }
  20. public void setDataBuff(byte[] dataBuff) {
  21. this.dataBuff = dataBuff;
  22. }
  23. public void resetStore() {
  24. this.keyStore.clear();
  25. }
  26. public TestState getState() {
  27. return this.state;
  28. }
  29. public void setState(TestState testState) {
  30. this.state = testState;
  31. }
  32. public int remainDataSize() {
  33. if (null != this.dataBuff) {
  34. if (this.readSize == this.dataBuff.length) {
  35. return 0;
  36. }
  37. return this.dataBuff.length - this.readSize;
  38. }
  39. return 0;
  40. }
  41. public void readIndex(int readSize) {
  42. this.readSize = readSize;
  43. }
  44. public void readSize(int readSize) {
  45. this.readSize = this.readSize + readSize;
  46. }
  47. public byte getByte() throws NullPointerException, IndexOutOfBoundsException {
  48. return this.dataBuff[this.readSize];
  49. }
  50. public byte[] getBytes(int length) throws NullPointerException, IndexOutOfBoundsException {
  51. byte[] buf = new byte[length];
  52. System.arraycopy(this.dataBuff, this.readSize, buf, 0, length);
  53. return buf;
  54. }
  55. public byte getByte(int pos) throws NullPointerException, IndexOutOfBoundsException {
  56. return this.dataBuff[pos];
  57. }
  58. public byte[] getBytes(int pos, int length) throws NullPointerException, IndexOutOfBoundsException {
  59. byte[] buf = new byte[length];
  60. System.arraycopy(this.dataBuff, pos, buf, 0, length);
  61. return buf;
  62. }
  63. public void addValue(String key, Object value) {
  64. this.keyStore.put(key, value);
  65. }
  66. public Set<String> keys() {
  67. return this.keyStore.keySet();
  68. }
  69. public Object getValue(String key) {
  70. return this.keyStore.get(key);
  71. }
  72. public Object getItemValue(String key) {
  73. // Object itemTemplateCacheObj = this.keyStore.get(BusinessConstant.SW_DATA_STREAM_CACHE_VALUE);
  74. // if (null != itemTemplateCacheObj) {
  75. // if (itemTemplateCacheObj instanceof DataItemTemplateCache) {
  76. // DataItemTemplateCache itemTemplateCache = (DataItemTemplateCache) itemTemplateCacheObj;
  77. // return itemTemplateCache.getValue(key);
  78. // }
  79. // }
  80. return null;
  81. }
  82. public abstract void setPacketBuff(byte[] buff);
  83. public abstract byte[] getPacketBuff();
  84. public abstract void setPacketLen(int len);
  85. public abstract int getPacketLen();
  86. public abstract void setBodyBuff(byte[] buff);
  87. public abstract byte[] getBodyBuff();
  88. public abstract int getBodyLen();
  89. public abstract byte[] getDeviceCodeBuff();
  90. }