Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private Object
- decodeInputStream(
- InputStream bais,
- int nesting)
- throws IOException {
- System.out.println(nesting);
- if (nesting == 0 && !bais.markSupported()) {
- throw new IOException("InputStream must support the mark() method");
- }
- //set a mark
- bais.mark(Integer.MAX_VALUE);
- //read a byte
- int tempByte = bais.read();
- //decide what to do
- switch (tempByte) {
- case 'd':
- //create a new dictionary object
- Map tempMap = new HashMap();
- try {
- //get the key
- byte[] tempByteArray = null;
- while ((tempByteArray = (byte[]) decodeInputStream(bais,
- nesting + 1)) != null) {
- //decode some more
- Object value = decodeInputStream(bais, nesting + 1);
- //add the value to the map
- CharBuffer cb = Constants.BYTE_CHARSET.decode(ByteBuffer.
- wrap(tempByteArray));
- String key = new String(cb.array(), 0, cb.limit());
- tempMap.put(key, value);
- }
- bais.mark(Integer.MAX_VALUE);
- tempByte = bais.read();
- bais.reset();
- if (nesting > 0 && tempByte == -1) {
- throw (new IOException(
- "BDecoder: invalid input data, 'e' missing from end of dictionary"));
- }
- } catch (Throwable e) {
- if (!recovery_mode) {
- if (e instanceof IOException) {
- throw ((IOException) e);
- }
- throw (new IOException(e.toString()));
- }
- }
- //return the map
- return tempMap;
- case 'l':
- //create the list
- List tempList = new ArrayList();
- try {
- //create the key
- Object tempElement = null;
- System.out.println("here");
- while ((tempElement = decodeInputStream(bais, nesting + 1)) != null) {
- //add the element
- // System.out.println(tempElement.getClass());
- tempList.add(tempElement);
- }
- bais.mark(Integer.MAX_VALUE);
- tempByte = bais.read();
- bais.reset();
- if (nesting > 0 && tempByte == -1) {
- throw (new IOException(
- "BDecoder: invalid input data, 'e' missing from end of list"));
- }
- } catch (Throwable e) {
- if (!recovery_mode) {
- if (e instanceof IOException) {
- throw ((IOException) e);
- }
- throw (new IOException(e.toString()));
- }
- }
- //return the list
- return tempList;
- case 'e':
- case -1:
- return null;
- case 'i':
- return new Long(getNumberFromStream(bais, 'e'));
- case '0':
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9':
- //move back one
- bais.reset();
- //get the string
- return getByteArrayFromStream(bais);
- default: {
- int rem_len = bais.available();
- if (rem_len > 256) {
- rem_len = 256;
- }
- byte[] rem_data = new byte[rem_len];
- bais.read(rem_data);
- throw (new IOException(
- "BDecoder: unknown command '" + tempByte + ", remainder = " +
- new String(rem_data)));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement