package com.thoughtworks.xstream.io.binary; import com.thoughtworks.xstream.converters.ErrorWriter; import com.thoughtworks.xstream.io.ExtendedHierarchicalStreamReader; import com.thoughtworks.xstream.io.HierarchicalStreamReader; import com.thoughtworks.xstream.io.StreamException; import com.thoughtworks.xstream.io.binary.Token; import java.io.DataInputStream; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.Iterator; import java.util.Map; /* loaded from: classes.dex */ public class BinaryStreamReader implements ExtendedHierarchicalStreamReader { private final DataInputStream in; private Token pushback; private final ReaderDepthState depthState = new ReaderDepthState(); private final IdRegistry idRegistry = new IdRegistry(); private final Token.Formatter tokenFormatter = new Token.Formatter(); private static class IdRegistry { private Map map; private IdRegistry() { this.map = new HashMap(); } public String get(long j) { String str = (String) this.map.get(new Long(j)); if (str != null) { return str; } StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append("Unknown ID : "); stringBuffer.append(j); throw new StreamException(stringBuffer.toString()); } public void put(long j, String str) { this.map.put(new Long(j), str); } } public BinaryStreamReader(InputStream inputStream) { this.in = new DataInputStream(inputStream); moveDown(); } private Token readToken() { Token token = this.pushback; if (token != null) { this.pushback = null; return token; } try { Token read = this.tokenFormatter.read(this.in); if (read.getType() != 2) { return read; } this.idRegistry.put(read.getId(), read.getValue()); return readToken(); } catch (IOException e) { throw new StreamException(e); } } @Override // com.thoughtworks.xstream.io.HierarchicalStreamReader, com.thoughtworks.xstream.converters.ErrorReporter public void appendErrors(ErrorWriter errorWriter) { } @Override // com.thoughtworks.xstream.io.HierarchicalStreamReader public void close() { try { this.in.close(); } catch (IOException e) { throw new StreamException(e); } } @Override // com.thoughtworks.xstream.io.HierarchicalStreamReader public String getAttribute(String str) { return this.depthState.getAttribute(str); } @Override // com.thoughtworks.xstream.io.HierarchicalStreamReader public int getAttributeCount() { return this.depthState.getAttributeCount(); } @Override // com.thoughtworks.xstream.io.HierarchicalStreamReader public String getAttributeName(int i) { return this.depthState.getAttributeName(i); } @Override // com.thoughtworks.xstream.io.HierarchicalStreamReader public Iterator getAttributeNames() { return this.depthState.getAttributeNames(); } @Override // com.thoughtworks.xstream.io.HierarchicalStreamReader public String getNodeName() { return this.depthState.getName(); } @Override // com.thoughtworks.xstream.io.HierarchicalStreamReader public String getValue() { return this.depthState.getValue(); } @Override // com.thoughtworks.xstream.io.HierarchicalStreamReader public boolean hasMoreChildren() { return this.depthState.hasMoreChildren(); } @Override // com.thoughtworks.xstream.io.HierarchicalStreamReader public void moveDown() { this.depthState.push(); Token readToken = readToken(); if (readToken.getType() != 3) { throw new StreamException("Expected StartNode"); } this.depthState.setName(this.idRegistry.get(readToken.getId())); while (true) { Token readToken2 = readToken(); byte type = readToken2.getType(); if (type == 3) { this.depthState.setHasMoreChildren(true); pushBack(readToken2); return; } if (type == 4) { this.depthState.setHasMoreChildren(false); pushBack(readToken2); return; } else if (type == 5) { this.depthState.addAttribute(this.idRegistry.get(readToken2.getId()), readToken2.getValue()); } else { if (type != 6) { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append("Unexpected token "); stringBuffer.append(readToken2); throw new StreamException(stringBuffer.toString()); } this.depthState.setValue(readToken2.getValue()); } } } @Override // com.thoughtworks.xstream.io.HierarchicalStreamReader public void moveUp() { this.depthState.pop(); int i = 0; while (true) { byte type = readToken().getType(); if (type == 3) { i++; } else if (type != 4) { continue; } else if (i == 0) { break; } else { i--; } } Token readToken = readToken(); byte type2 = readToken.getType(); if (type2 == 3) { this.depthState.setHasMoreChildren(true); } else { if (type2 != 4) { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append("Unexpected token "); stringBuffer.append(readToken); throw new StreamException(stringBuffer.toString()); } this.depthState.setHasMoreChildren(false); } pushBack(readToken); } @Override // com.thoughtworks.xstream.io.ExtendedHierarchicalStreamReader public String peekNextChild() { if (this.depthState.hasMoreChildren()) { return this.idRegistry.get(this.pushback.getId()); } return null; } public void pushBack(Token token) { if (this.pushback != null) { throw new Error("Cannot push more than one token back"); } this.pushback = token; } @Override // com.thoughtworks.xstream.io.HierarchicalStreamReader public HierarchicalStreamReader underlyingReader() { return this; } @Override // com.thoughtworks.xstream.io.HierarchicalStreamReader public String getAttribute(int i) { return this.depthState.getAttribute(i); } }