Offload int[] to byte[].
[SquirrelJME.git] / modules / meep-swm / src / main / java / javax / microedition / swm / __SuiteTracker__.java
blobe17241afdbf65be6a6184dd07667127d108ec4b2
1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // SquirrelJME
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // ---------------------------------------------------------------------------
10 package javax.microedition.swm;
12 import cc.squirreljme.runtime.cldc.debug.Debugging;
13 import cc.squirreljme.runtime.swm.JarStreamSupplier;
14 import java.util.Set;
16 /**
17 * This performs the installation and tracks progress.
19 * @since 2017/12/28
21 final class __SuiteTracker__
22 extends SuiteManagementTracker
24 /** Bytes per percent threshold. */
25 private static final int _PERCENT_THRESHOLD =
26 4096;
28 /** The suite when it is installed. */
29 volatile Suite _suite;
31 /**
32 * Initializes the tracker.
34 * @param __i The owning installer.
35 * @throws NullPointerException On null arguments.
36 * @since 2017/12/28
38 __SuiteTracker__(SuiteInstaller __i)
39 throws NullPointerException
41 if (__i == null)
42 throw new NullPointerException("NARG");
44 // Setup thread which performs the actual installation
45 Thread thread = new Thread(new __Runner__(this, __i),
46 "SquirrelJME-Suite-Installer");
47 thread.start();
50 /**
51 * {@inheritDoc}
52 * @since 2016/06/24
54 @Override
55 public Suite getSuite()
57 return this._suite;
60 /**
61 * This thread performs the installation.
63 * @since 2017/12/28
65 private static final class __Runner__
66 implements Runnable
68 /** The tracker which is given the suite when finished. */
69 protected final __SuiteTracker__ tracker;
71 /** The supplier for the JAR data. */
72 protected final JarStreamSupplier supplier;
74 /** Listeners for suites. */
75 private final Set<SuiteInstallListener> _listeners;
77 /**
78 * Initializes the runner.
80 * @param __tracker The tracker where the resulting suite is placed.
81 * @param __i The installer used.
82 * @throws NullPointerException On null arguments.
83 * @since 2017/12/28
85 private __Runner__(__SuiteTracker__ __tracker, SuiteInstaller __i)
86 throws NullPointerException
88 if (__tracker == null || __i == null)
89 throw new NullPointerException("NARG");
91 this.tracker = __tracker;
92 this.supplier = __i._supplier;
93 this._listeners = __i._listeners;
96 /**
97 * {@inheritDoc}
98 * @since 2017/12/28
100 @Override
101 public void run()
105 throw Debugging.todo();
107 // Read the JAR data stream
108 byte[] data;
109 __update(SuiteInstallStage.DOWNLOAD_DATA, 0);
110 try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
111 InputStream is = this.supplier.get())
113 // Read in source data
114 byte[] buf = new byte[512];
115 for (int count = 0, last = 0;;)
117 int rc = is.read(buf);
119 if (rc < 0)
120 break;
122 baos.write(buf, 0, rc);
124 // Update progress for the first initial set of blocks
125 if (last < 99)
127 count += rc;
128 int now = count / _PERCENT_THRESHOLD;
129 if (last != now && now <= 99)
131 __update(SuiteInstallStage.DOWNLOAD_DATA, now);
132 last = now;
137 // Get the entire data buffer
138 baos.flush();
139 data = baos.toByteArray();
142 // Could not read the JAR
143 catch (IOException e)
145 __done(InstallErrorCodes.IO_FILE_ERROR);
146 return;
148 __update(SuiteInstallStage.DOWNLOAD_DATA, 100);
150 // Need the library manager
151 __SystemSuiteManager__ ssm =
152 (__SystemSuiteManager__)ManagerFactory.getSuiteManager();
153 LibrariesClient manager = ssm._manager;
155 // Send it to the kernel
156 __update(SuiteInstallStage.VERIFYING, 0);
157 LibraryInstallationReport report = manager.install(
158 data, 0, data.length);
160 // Failed
161 int error = report.error();
162 if (error != 0)
164 // Determine the error code
165 error--;
166 InstallErrorCodes[] codes = InstallErrorCodes.values();
167 InstallErrorCodes code = (error >= 0 &&
168 error < codes.length ? codes[error] :
169 InstallErrorCodes.OTHER_ERROR);
171 // This will be the only chance to print the installation
172 // report
173 /* {@squirreljme.error DG0t Failed to install the program
174 due to the specified error. (The error code; The more
175 detailed message associated with the error)} * /
176 System.err.printf("DG0t %s %s%n", code, report.message());
178 // Mark as done
179 __done(code);
180 return;
183 // Set the suite used
184 this.tracker._suite = new Suite(report.library());
186 // Did not fail, but report it anyway
187 __update(SuiteInstallStage.VERIFYING, 100);
189 // Finished
190 __update(SuiteInstallStage.DONE, 100);
191 __done(InstallErrorCodes.NO_ERROR);
195 // Oops
196 catch (Throwable t)
198 // Just print the trace
199 t.printStackTrace();
201 // And use some other error code to indicate failure
202 this.__done(InstallErrorCodes.OTHER_ERROR);
207 * Called when installation has finished, potentially with an error.
209 * @param __code The error code.
210 * @since 2017/12/28
212 private void __done(InstallErrorCodes __code)
214 __SuiteTracker__ tracker = this.tracker;
215 for (SuiteInstallListener l : this._listeners)
218 l.installationDone(__code, tracker);
220 catch (Exception e)
226 * Updates the current install stage.
228 * @param __stage The current installation stage.
229 * @param __pct The percentage complete.
230 * @since 2017/12/28
232 private void __update(SuiteInstallStage __stage, int __pct)
234 __SuiteTracker__ tracker = this.tracker;
235 for (SuiteInstallListener l : this._listeners)
238 l.updateStatus(tracker, __stage, __pct);
240 catch (Exception e)