Debian Codegraph @

rhino/src/org/mozilla/javascript/serialize/ScriptableInputStream.java

raw symbols SCIP
1 /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 // API class
8
9 package org.mozilla.javascript.serialize;
10
11 import java.io.*;
12
13 import org.mozilla.javascript.*;
14
15 /**
16 * Class ScriptableInputStream is used to read in a JavaScript
17 * object or function previously serialized with a ScriptableOutputStream.
18 * References to names in the exclusion list
19 * replaced with references to the top-level scope specified during
20 * creation of the ScriptableInputStream.
21 *
22 * @author Norris Boyd
23 */
24
25 public class ScriptableInputStream extends ObjectInputStream {
26
27 /**
28 * Create a ScriptableInputStream.
29 * @param in the InputStream to read from.
30 * @param scope the top-level scope to create the object in.
31 */
32 public ScriptableInputStream(InputStream in, Scriptable scope)
33 throws IOException
34 {
35 super(in);
36 this.scope = scope;
37 enableResolveObject(true);
38 Context cx = Context.getCurrentContext();
39 if (cx != null) {
40 this.classLoader = cx.getApplicationClassLoader();
41 }
42 }
43
44 @Override
45 protected Class<?> resolveClass(ObjectStreamClass desc)
46 throws IOException, ClassNotFoundException
47 {
48 String name = desc.getName();
49 if (classLoader != null) {
50 try {
51 return classLoader.loadClass(name);
52 } catch (ClassNotFoundException ex) {
53 // fall through to default loading
54 }
55 }
56 return super.resolveClass(desc);
57 }
58
59 @Override
60 protected Object resolveObject(Object obj)
61 throws IOException
62 {
63 if (obj instanceof ScriptableOutputStream.PendingLookup) {
64 String name = ((ScriptableOutputStream.PendingLookup)obj).getName();
65 obj = ScriptableOutputStream.lookupQualifiedName(scope, name);
66 if (obj == Scriptable.NOT_FOUND) {
67 throw new IOException("Object " + name + " not found upon " +
68 "deserialization.");
69 }
70 }else if (obj instanceof UniqueTag) {
71 obj = ((UniqueTag)obj).readResolve();
72 }else if (obj instanceof Undefined) {
73 obj = ((Undefined)obj).readResolve();
74 }
75 return obj;
76 }
77
78 private Scriptable scope;
79 private ClassLoader classLoader;
80 }