public BAFReader(final PureFactory factory, final InputStream inputStream) { _factory = factory; _reader = new BitStream(inputStream); }
private ATerm readFromBinaryFile(final InputStream stream, final boolean headerRead) throws ParseError, IOException { final BAFReader r = new BAFReader(this, stream); return r.readFromBinaryFile(headerRead); }
public ATerm readFromBinaryFile(final boolean headerAlreadyRead) throws ParseError, IOException { if (!headerAlreadyRead && !isBinaryATerm(_reader)) throw new ParseError("Input is not a BAF file"); final int val = _reader.readInt(); if (val != BAF_VERSION) throw new ParseError("Wrong BAF version (wanted " + BAF_VERSION + ", got " + val + "), giving up"); _nrUniqueSymbols = _reader.readInt(); final int nrUniqueTerms = _reader.readInt(); if (isDebugging()) { debug("" + _nrUniqueSymbols + " unique symbols"); debug("" + nrUniqueTerms + " unique terms"); } _symbols = new SymEntry[_nrUniqueSymbols]; readAllSymbols(); final int i = _reader.readInt(); return readTerm(_symbols[i]); }
private AFun readSymbol() throws IOException { final String s = _reader.readString(); final int arity = _reader.readInt(); final int quoted = _reader.readInt(); if (isDebugging()) debug(s + " / " + arity + " / " + quoted); return _factory.makeAFun(s, arity, quoted != 0); }
private static boolean isBinaryATerm(final BitStream in) throws IOException { try { final int w1 = in.readInt(); return w1 == BAF_MAGIC; } catch (final EOFException e) { _logger.log(Level.FINE, "", e); } return false; }
/** * Deserialializes a real. */ private void touchReal() { final double value = readDouble(); final ATermConstruct ac = _stack[_stackPosition]; final ATerm term = _factory.makeReal(value); _sharedTerms[ac.termIndex] = term; linkTerm(term); }
public int readBits(final int nrBits) throws IOException { int mask = 1; int val = 0; for (int i = 0; i < nrBits; i++) { if (_bitsInBuffer == 0) { final int v = readByte(); if (v == -1) return -1; _bitBuffer = v; _bitsInBuffer = 8; } val |= (_bitBuffer & 0x80) != 0 ? mask : 0; mask <<= 1; _bitBuffer <<= 1; _bitsInBuffer--; } return val; }
/** * Serializes the given placeholder. Placeholders will always be serialized in one piece. * * @see openllet.aterm.ATermFwdVoid#voidVisitPlaceholder(ATermPlaceholder) */ @Override public void voidVisitPlaceholder(final ATermPlaceholder arg) { _currentBuffer.put(getHeader(arg)); // Do nothing, serializing its header is enough. }
/** * Reconstructs a double from the following 8 bytes in the buffer. * * @return The reconstructed double. */ private double readDouble() { final long result = readLong(); return Double.longBitsToDouble(result); }
/** * Splits the given double in separate bytes and writes it to the buffer. Doubles will always * occupy 8 bytes, since the convertion of a floating point number to a long will always cause * the high order bits to be occupied. * * @param value * The integer that needs to be split and written. */ private void writeDouble(final double value) { final long longValue = Double.doubleToLongBits(value); writeLong(longValue); }
public String readString() throws IOException { final int l = readInt(); final byte[] b = new byte[l]; int v = 0; while (v < b.length) v += _stream.read(b, v, b.length - v); return new String(b); }
private ATerm readSAFFromOldStyleStream(final InputStream stream) throws IOException { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final byte[] buffer = new byte[4096]; int nrOfBytesRead; while ((nrOfBytesRead = stream.read(buffer, 0, buffer.length)) != -1) baos.write(buffer, 0, nrOfBytesRead); return BinaryReader.readTermFromSAFString(this, baos.toByteArray()); }
/** * Serializes the given list. List information will always be serialized in one piece. * * @see openllet.aterm.ATermFwdVoid#voidVisitList(ATermList) */ @Override public void voidVisitList(final ATermList arg) { final byte header = getHeader(arg); _currentBuffer.put(header); writeInt(arg.getLength()); }
public int readInt() throws IOException { final int[] buf = new int[5]; buf[0] = readByte(); // Check if 1st character is enough if ((buf[0] & 0x80) == 0) return buf[0]; buf[1] = readByte(); // Check if 2nd character is enough if ((buf[0] & 0x40) == 0) return buf[1] + ((buf[0] & ~0xc0) << 8); buf[2] = readByte(); // Check if 3rd character is enough if ((buf[0] & 0x20) == 0) return buf[2] + (buf[1] << 8) + ((buf[0] & ~0xe0) << 16); buf[3] = readByte(); // Check if 4th character is enough if ((buf[0] & 0x10) == 0) return buf[3] + (buf[2] << 8) + (buf[1] << 16) + ((buf[0] & ~0xf0) << 24); buf[4] = readByte(); return buf[4] + (buf[3] << 8) + (buf[2] << 16) + (buf[1] << 24); }