X-Git-Url: https://review.fuel-infra.org/gitweb?a=blobdiff_plain;f=lib%2Fmcollective%2Fvendor%2Fjson%2Fjava%2Fsrc%2Fjson%2Fext%2FUtils.java;fp=lib%2Fmcollective%2Fvendor%2Fjson%2Fjava%2Fsrc%2Fjson%2Fext%2FUtils.java;h=44d6a55ea296e37c6009bbf8fadd6fe644789235;hb=b87d2f4e68281062df1913440ca5753ae63314a9;hp=0000000000000000000000000000000000000000;hpb=ab0ea530b8ac956091f17b104ab2311336cfc250;p=packages%2Fprecise%2Fmcollective.git diff --git a/lib/mcollective/vendor/json/java/src/json/ext/Utils.java b/lib/mcollective/vendor/json/java/src/json/ext/Utils.java new file mode 100644 index 0000000..44d6a55 --- /dev/null +++ b/lib/mcollective/vendor/json/java/src/json/ext/Utils.java @@ -0,0 +1,89 @@ +/* + * This code is copyrighted work by Daniel Luz . + * + * Distributed under the Ruby and GPLv2 licenses; see COPYING and GPL files + * for details. + */ +package json.ext; + +import org.jruby.Ruby; +import org.jruby.RubyArray; +import org.jruby.RubyClass; +import org.jruby.RubyException; +import org.jruby.RubyHash; +import org.jruby.RubyString; +import org.jruby.exceptions.RaiseException; +import org.jruby.runtime.Block; +import org.jruby.runtime.ThreadContext; +import org.jruby.runtime.builtin.IRubyObject; +import org.jruby.util.ByteList; + +/** + * Library of miscellaneous utility functions + */ +final class Utils { + public static final String M_GENERATOR_ERROR = "GeneratorError"; + public static final String M_NESTING_ERROR = "NestingError"; + public static final String M_PARSER_ERROR = "ParserError"; + + private Utils() { + throw new RuntimeException(); + } + + /** + * Safe {@link RubyArray} type-checking. + * Returns the given object if it is an Array, + * or throws an exception if not. + * @param object The object to test + * @return The given object if it is an Array + * @throws RaiseException TypeError if the object is not + * of the expected type + */ + static RubyArray ensureArray(IRubyObject object) throws RaiseException { + if (object instanceof RubyArray) return (RubyArray)object; + Ruby runtime = object.getRuntime(); + throw runtime.newTypeError(object, runtime.getArray()); + } + + static RubyHash ensureHash(IRubyObject object) throws RaiseException { + if (object instanceof RubyHash) return (RubyHash)object; + Ruby runtime = object.getRuntime(); + throw runtime.newTypeError(object, runtime.getHash()); + } + + static RubyString ensureString(IRubyObject object) throws RaiseException { + if (object instanceof RubyString) return (RubyString)object; + Ruby runtime = object.getRuntime(); + throw runtime.newTypeError(object, runtime.getString()); + } + + static RaiseException newException(ThreadContext context, + String className, String message) { + return newException(context, className, + context.getRuntime().newString(message)); + } + + static RaiseException newException(ThreadContext context, + String className, RubyString message) { + RuntimeInfo info = RuntimeInfo.forRuntime(context.getRuntime()); + RubyClass klazz = info.jsonModule.get().getClass(className); + RubyException excptn = + (RubyException)klazz.newInstance(context, + new IRubyObject[] {message}, Block.NULL_BLOCK); + return new RaiseException(excptn); + } + + static byte[] repeat(ByteList a, int n) { + return repeat(a.unsafeBytes(), a.begin(), a.length(), n); + } + + static byte[] repeat(byte[] a, int begin, int length, int n) { + if (length == 0) return ByteList.NULL_ARRAY; + int resultLen = length * n; + byte[] result = new byte[resultLen]; + for (int pos = 0; pos < resultLen; pos += length) { + System.arraycopy(a, begin, result, pos, length); + } + return result; + } +}