1 30 package org.jruby.runtime.marshal; 31 32 import java.io.IOException ; 33 import java.util.ArrayList ; 34 import java.util.List ; 35 36 import org.jruby.Ruby; 37 import org.jruby.RubySymbol; 38 import org.jruby.runtime.builtin.IRubyObject; 39 40 public class UnmarshalCache { 41 private final Ruby runtime; 42 private List links = new ArrayList (); 43 private List symbols = new ArrayList (); 44 45 public UnmarshalCache(Ruby runtime) { 46 this.runtime = runtime; 47 } 48 49 public void register(IRubyObject value) { 50 selectCache(value).add(value); 51 } 52 53 private List selectCache(IRubyObject value) { 54 return (value instanceof RubySymbol) ? symbols : links; 55 } 56 57 public boolean isLinkType(int c) { 58 return c == ';' || c == '@'; 59 } 60 61 public IRubyObject readLink(UnmarshalStream input, int type) throws IOException { 62 if (type == '@') { 63 return linkedByIndex(input.unmarshalInt()); 64 } 65 assert type == ';'; 66 int i = input.unmarshalInt(); 67 68 return symbolByIndex(i); 69 } 70 71 private IRubyObject linkedByIndex(int index) { 72 try { 73 return (IRubyObject) links.get(index); 74 } catch (IndexOutOfBoundsException e) { 75 throw runtime.newArgumentError("dump format error (unlinked, index: " + index + ")"); 76 } 77 } 78 79 private RubySymbol symbolByIndex(int index) { 80 try { 81 return (RubySymbol) symbols.get(index); 82 } catch (IndexOutOfBoundsException e) { 83 throw runtime.newTypeError("bad symbol"); 84 } 85 } 86 } 87 | Popular Tags |