@Nullable public static java.lang.String toUtf8String(OtpErlangBitstr bitstr) { if (bitstr.pad_bits() > 0) return null; try { return Charset.availableCharsets().get("UTF-8").newDecoder().decode(ByteBuffer.wrap(bitstr.binaryValue())).toString(); } catch (CharacterCodingException e) { return null; } }
@NotNull private static String toString(@NotNull OtpErlangBitstr quoted) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append('"'); for (byte element : quoted.binaryValue()) { if (element == 0x0A) { stringBuilder.append("\\n"); } else if (isAsciiPrintable((char) element)) { stringBuilder.append((char) element); } else { stringBuilder.append(String.format("\\x%02X", element)); } } stringBuilder.append('"'); return stringBuilder.toString(); }
/** * Create a bitstr with pad bits from a byte array. * * @param bin * the array of bytes from which to create the bitstr. * @param pad_bits * the number of unused bits in the low end of the last byte. */ public OtpErlangBitstr(final byte[] bin, final int pad_bits) { this.bin = new byte[bin.length]; System.arraycopy(bin, 0, this.bin, 0, bin.length); this.pad_bits = pad_bits; check_bitstr(this.bin, this.pad_bits); }
@Override public Object clone() { final OtpErlangBinary that = (OtpErlangBinary) super.clone(); return that; } }
/** * Get the java Object from the bitstr. If the bitstr contains a serialized * Java object, then this method will recreate the object. * * * @return the java Object represented by this bitstr, or null if the bitstr * does not represent a Java Object. */ public Object getObject() { if (pad_bits != 0) { return null; } return fromByteArray(bin); }
/** * Create a bitstr from an arbitrary Java Object. The object must implement * java.io.Serializable or java.io.Externalizable. * * @param o * the object to serialize and create this bitstr from. */ public OtpErlangBitstr(final Object o) { try { bin = toByteArray(o); pad_bits = 0; } catch (final IOException e) { throw new java.lang.IllegalArgumentException( "Object must implement Serializable"); } }
return new OtpErlangBitstr(this);
/** * Create a bitstr from a stream containing a bitstr encoded in Erlang * external format. * * @param buf * the stream containing the encoded bitstr. * * @exception OtpErlangDecodeException * if the buffer does not contain a valid external * representation of an Erlang bitstr. */ public OtpErlangBitstr(final OtpInputStream buf) throws OtpErlangDecodeException { final int pbs[] = { 0 }; // This is ugly just to get a value-result // parameter bin = buf.read_bitstr(pbs); pad_bits = pbs[0]; check_bitstr(bin, pad_bits); }
private static void renderBitstr(OtpErlangBitstr bitstr, XValueTextRenderer renderer) { java.lang.String utf8String = toUtf8String(bitstr); if (utf8String != null) { renderer.renderStringValue(utf8String); } else { renderer.renderSpecialSymbol("<<"); boolean first = true; for (byte b : bitstr.binaryValue()) { if (!first) renderer.renderSpecialSymbol(", "); renderer.renderValue("" + ((int) b & 0xFF)); first = false; } if (bitstr.pad_bits() > 0) { renderer.renderSpecialSymbol("::size(" + (8 - bitstr.pad_bits()) + ")"); } renderer.renderSpecialSymbol(">>"); } }