@JRubyMethod public IRubyObject hash(ThreadContext context) { try { MessageDigest digest = MessageDigest.getInstance("SHA-256"); for (IRubyObject key : table.keySet()) { digest.update((byte) key.hashCode()); digest.update((byte) table.get(key).hashCode()); } return context.runtime.newString(new ByteList(digest.digest())); } catch (NoSuchAlgorithmException ignore) { return context.runtime.newFixnum(System.identityHashCode(table)); } }
@JRubyMethod public IRubyObject hash(ThreadContext context) { try { MessageDigest digest = MessageDigest.getInstance("SHA-256"); for (RubyMap map : maps.values()) { digest.update((byte) map.hashCode()); } for (RubyRepeatedField repeatedField : repeatedFields.values()) { digest.update((byte) repeatedFields.hashCode()); } for (IRubyObject field : fields.values()) { digest.update((byte) field.hashCode()); } return context.runtime.newString(new ByteList(digest.digest())); } catch (NoSuchAlgorithmException ignore) { return context.runtime.newFixnum(System.identityHashCode(this)); } }
@JRubyMethod(meta = true) public static IRubyObject encode(ThreadContext context, IRubyObject recv, IRubyObject value) { RubyMessage message = (RubyMessage) value; return context.runtime.newString(new ByteList(message.build(context).toByteArray())); }
public RubyString(Ruby runtime, RubyClass rubyClass, byte[] value) { super(runtime, rubyClass); assert value != null; this.value = new ByteList(value); }
private static final ByteList intToUnsignedByteList(int i, int shift, byte[] digitmap) { byte[] buf = new byte[32]; int charPos = 32; int radix = 1 << shift; long mask = radix - 1; do { buf[--charPos] = digitmap[(int)(i & mask)]; i >>>= shift; } while (i != 0); return new ByteList(buf, charPos, (32 - charPos), false); }
public static RubyString newUsAsciiStringShared(Ruby runtime, byte[] bytes, int start, int length) { RubyString str = newUsAsciiStringNoCopy(runtime, new ByteList(bytes, start, length, false)); str.shareLevel = SHARE_LEVEL_BUFFER; return str; }
private static ByteList getByteList(final ByteBuffer buffer, final Encoding enc, final boolean shared) { byte[] bytes; int off; if (!shared && buffer.hasArray()) { // HeapByteBuffer bytes = buffer.array(); off = buffer.arrayOffset(); } else { bytes = getBytes(buffer); off = 0; } return new ByteList(bytes, off, off + buffer.limit(), enc, false); }
protected int write(CallSite write, ByteBuffer src) throws IOException { ByteList buffer; if (src.hasArray()) { buffer = new ByteList(src.array(), src.position(), src.remaining(), true); } else { buffer = new ByteList(src.remaining()); buffer.append(src, src.remaining()); } IRubyObject written = write.call(runtime.getCurrentContext(), io, io, RubyString.newStringLight(runtime, buffer)); return (int)written.convertToInteger().getLongValue(); }
public static RubyString newStringShared(Ruby runtime, byte[] bytes, int start, int length, Encoding encoding) { ByteList byteList = new ByteList(bytes, start, length, encoding, false); RubyString str = new RubyString(runtime, runtime.getString(), byteList); str.shareLevel = SHARE_LEVEL_BUFFER; return str; }
private void init(int windowBits) { flater = new com.jcraft.jzlib.Inflater(); flater.init(windowBits); collected = new byte[BASE_SIZE]; collectedIdx = 0; input = new ByteList(); }
private static ByteList bytelist(String value, String encodingName) { Encoding encoding; EncodingDB.Entry entry = EncodingDB.getEncodings().get(encodingName.getBytes()); if (entry == null) entry = EncodingDB.getAliases().get(encodingName.getBytes()); if (entry == null) throw new RuntimeException("could not find encoding: " + encodingName); encoding = entry.getEncoding(); ByteList byteList = new ByteList(value.getBytes(RubyEncoding.ISO), encoding); return byteList; }
@Override public Object execute(ParserSupport support, RubyLexer lexer, Object yyVal, Object[] yyVals, int yyTop) { /* FIXME: bytelist_love: somewhat silly to remake the empty bytelist over and over but this type should change (using null vs "" is a strange distinction).*/ yyVal = new UnnamedRestArgNode(lexer.getPosition(), support.symbolID(new ByteList(new byte[] {})), support.getCurrentScope().addVariable("*")); return yyVal; } };
public int getline(ByteList dst, byte terminator, long limit) throws IOException, BadDescriptorException { if (binmode) { return stream.getline(dst, terminator, limit); } ByteList intermediate = new ByteList(); int result = stream.getline(intermediate, terminator, limit); convertCRLFToLF(intermediate, dst); return result; }
public synchronized ByteList read(int number) throws IOException, BadDescriptorException { checkReadable(); ensureReadNonBuffered(); ByteList byteList = new ByteList(number); // TODO this should entry into error handling somewhere int bytesRead = descriptor.read(number, byteList); if (bytesRead == -1) { eof = true; } return byteList; }
@JRubyMethod(name = "inspect") public IRubyObject inspect(ThreadContext context) { ByteList bytes = new ByteList(); bytes.append("#<Encoding:".getBytes()); bytes.append(name); if (isDummy) bytes.append(" (dummy)".getBytes()); bytes.append('>'); return RubyString.newUsAsciiStringNoCopy(context.runtime, bytes); }
public Encoding getEncodingFromString(String string) { if (string == null) return null; ByteList name = new ByteList(ByteList.plain(string), false); checkAsciiEncodingName(name); SpecialEncoding special = SpecialEncoding.valueOf(name); if (special != null) { return special.toEncoding(runtime); } return findEncodingWithError(name); }
private RubyString to_s(final Ruby runtime) { RubyString str = RubyString.newString(runtime, new ByteList(10), USASCIIEncoding.INSTANCE); str.append(num.to_s()); str.cat((byte)'/'); str.append(den.to_s()); return str; }
@JRubyMethod public IRubyObject replacement(ThreadContext context) { int ret = ec.makeReplacement(); if (ret == -1) { throw context.runtime.newUndefinedConversionError("replacement character setup failed"); } return context.runtime.newString(new ByteList( ec.replacementString, 0, ec.replacementLength, context.runtime.getEncodingService().findEncodingOrAliasEntry(ec.replacementEncoding).getEncoding(), true)); }
public static ByteList addByteLists(ByteList value1, ByteList value2) { ByteList result = new ByteList(value1.getRealSize() + value2.getRealSize()); result.setRealSize(value1.getRealSize() + value2.getRealSize()); System.arraycopy(value1.getUnsafeBytes(), value1.getBegin(), result.getUnsafeBytes(), 0, value1.getRealSize()); System.arraycopy(value2.getUnsafeBytes(), value2.getBegin(), result.getUnsafeBytes(), value1.getRealSize(), value2.getRealSize()); return result; }
private RubyString inspectImpl(Ruby runtime) { RubyString str = RubyString.newString(runtime, new ByteList(12), USASCIIEncoding.INSTANCE); str.cat((byte)'('); str.append((RubyString) num.inspect()); str.cat((byte)'/'); str.append((RubyString) den.inspect()); str.cat((byte)')'); return str; }