Advertisement
Guest User

Untitled

a guest
Jul 21st, 2011
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. private Object
  2. decodeInputStream(
  3. InputStream bais,
  4. int nesting)
  5.  
  6. throws IOException {
  7. System.out.println(nesting);
  8. if (nesting == 0 && !bais.markSupported()) {
  9.  
  10. throw new IOException("InputStream must support the mark() method");
  11. }
  12.  
  13. //set a mark
  14. bais.mark(Integer.MAX_VALUE);
  15.  
  16. //read a byte
  17. int tempByte = bais.read();
  18.  
  19. //decide what to do
  20. switch (tempByte) {
  21. case 'd':
  22.  
  23. //create a new dictionary object
  24. Map tempMap = new HashMap();
  25.  
  26. try {
  27. //get the key
  28. byte[] tempByteArray = null;
  29.  
  30. while ((tempByteArray = (byte[]) decodeInputStream(bais,
  31. nesting + 1)) != null) {
  32.  
  33. //decode some more
  34.  
  35. Object value = decodeInputStream(bais, nesting + 1);
  36.  
  37. //add the value to the map
  38.  
  39. CharBuffer cb = Constants.BYTE_CHARSET.decode(ByteBuffer.
  40. wrap(tempByteArray));
  41.  
  42. String key = new String(cb.array(), 0, cb.limit());
  43.  
  44. tempMap.put(key, value);
  45. }
  46.  
  47. bais.mark(Integer.MAX_VALUE);
  48. tempByte = bais.read();
  49. bais.reset();
  50. if (nesting > 0 && tempByte == -1) {
  51.  
  52. throw (new IOException(
  53. "BDecoder: invalid input data, 'e' missing from end of dictionary"));
  54. }
  55. } catch (Throwable e) {
  56.  
  57. if (!recovery_mode) {
  58.  
  59. if (e instanceof IOException) {
  60.  
  61. throw ((IOException) e);
  62. }
  63.  
  64. throw (new IOException(e.toString()));
  65. }
  66. }
  67.  
  68. //return the map
  69. return tempMap;
  70.  
  71. case 'l':
  72.  
  73. //create the list
  74. List tempList = new ArrayList();
  75.  
  76. try {
  77. //create the key
  78. Object tempElement = null;
  79. System.out.println("here");
  80. while ((tempElement = decodeInputStream(bais, nesting + 1)) != null) {
  81. //add the element
  82. // System.out.println(tempElement.getClass());
  83. tempList.add(tempElement);
  84. }
  85.  
  86. bais.mark(Integer.MAX_VALUE);
  87. tempByte = bais.read();
  88.  
  89. bais.reset();
  90. if (nesting > 0 && tempByte == -1) {
  91.  
  92. throw (new IOException(
  93. "BDecoder: invalid input data, 'e' missing from end of list"));
  94. }
  95. } catch (Throwable e) {
  96.  
  97. if (!recovery_mode) {
  98.  
  99. if (e instanceof IOException) {
  100.  
  101. throw ((IOException) e);
  102. }
  103.  
  104. throw (new IOException(e.toString()));
  105. }
  106. }
  107.  
  108. //return the list
  109. return tempList;
  110.  
  111. case 'e':
  112. case -1:
  113. return null;
  114.  
  115. case 'i':
  116. return new Long(getNumberFromStream(bais, 'e'));
  117.  
  118. case '0':
  119. case '1':
  120. case '2':
  121. case '3':
  122. case '4':
  123. case '5':
  124. case '6':
  125. case '7':
  126. case '8':
  127. case '9':
  128.  
  129. //move back one
  130. bais.reset();
  131.  
  132. //get the string
  133. return getByteArrayFromStream(bais);
  134.  
  135. default: {
  136.  
  137. int rem_len = bais.available();
  138.  
  139. if (rem_len > 256) {
  140.  
  141. rem_len = 256;
  142. }
  143.  
  144. byte[] rem_data = new byte[rem_len];
  145.  
  146. bais.read(rem_data);
  147.  
  148. throw (new IOException(
  149. "BDecoder: unknown command '" + tempByte + ", remainder = " +
  150. new String(rem_data)));
  151. }
  152. }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement