Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (524)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (593)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Help with converting BigDecimal to double.  (Read 1110 times)
0 Members and 1 Guest are viewing this topic.
Offline i30817

Junior Devvie





« Posted 2009-09-30 21:52:57 »

This is a inputstream that has some statistics built in like bytes/ps.

I think its a performance drain because of bigdecimal churn. The algoritm to estimate the velocity is also the most simple i could find. I did find more smooth algorithms, but i couldn't get them to work, and i only could get the algorithm not to jump around by using bigdecimals.

I'm asking for help on this, since it combines both areas that i'm a absolute noob on, floating point errors, timers and concurrency.

Main class (bigdecimal churn)
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
package downloads;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;

public class StatisticsInputStream extends InputStream {

    private long totalBytesRead;
    private long startTimeMillis;
    private InputStream delegate;
    private volatile long expectedSize;
    private BigDecimal bytesPerMillisecond = BigDecimal.ZERO;
    private BigDecimal thousand = BigDecimal.valueOf(1000);
    private BigDecimal timeRemaing = BigDecimal.valueOf(-1);


    public StatisticsInputStream(InputStream delegate, long expectedSize) {
        super();
        this.expectedSize = expectedSize;
        this.delegate = delegate;
    }

    @Override
    public long skip(long n) throws IOException {
        return delegate.skip(n);
    }

    @Override
    public synchronized void reset() throws IOException {
        delegate.reset();
    }

    @Override
    public int read(byte[] b, int off, int len) throws IOException {
        if (startTimeMillis == 0) {
            startTimeMillis = System.currentTimeMillis();
        }
        int read = delegate.read(b, off, len);
        if (read != -1) {
            updateMeasure(read);
        }
        return read;
    }

    @Override
    public int read(byte[] b) throws IOException {
        if (startTimeMillis == 0) {
            startTimeMillis = System.currentTimeMillis();
        }
        int read = delegate.read(b);
        if (read != -1) {
            updateMeasure(read);
        }
        return read;
    }

    public int read() throws IOException {
        if (startTimeMillis == 0) {
            startTimeMillis = System.currentTimeMillis();
        }
        int byteRead = delegate.read();
        if (byteRead != -1) {
            updateMeasure(1);
        }
        return byteRead;
    }

    @Override
    public boolean markSupported() {
        return delegate.markSupported();
    }

    @Override
    public synchronized void mark(int readlimit) {
        delegate.mark(readlimit);
    }

    @Override
    public void close() throws IOException {
        delegate.close();
    }

    @Override
    public int available() throws IOException {
        return delegate.available();
    }

    protected void updateMeasure(int bytesRead) {
        totalBytesRead += bytesRead;
        long now = System.currentTimeMillis();
        long delta_T = now - startTimeMillis;
        //You'd think negative intervals couldn't happen. You'd be wrong.
        if (delta_T <= 0) {
            return;
        }
        //(time elapsed/dl'ed size)*size left
        BigDecimal remainingBytes = BigDecimal.valueOf(getExpectedSize() - getBytesRead().longValue());
        BigDecimal timeElapsed = BigDecimal.valueOf(delta_T);
        BigDecimal bytes = BigDecimal.valueOf(totalBytesRead);
        timeRemaing = timeElapsed.divide(bytes, 5, RoundingMode.HALF_UP).multiply(remainingBytes);
    }

    public BigDecimal getBytesPerSecond() {
        return bytesPerMillisecond.multiply(thousand);
    }

    public BigInteger getBytesRead() {
        return BigInteger.valueOf(totalBytesRead);
    }

    public BigDecimal getTimeRemaining() {
        return timeRemaing;
    }

    /**
     * @return the expectedSize
     */

    public long getExpectedSize() {
        return expectedSize;
    }

    /**
     * @param expectedSize the expectedSize to set
     */

    protected void setExpectedSize(long expectedSize) {
        this.expectedSize = expectedSize;
    }
}

Offline i30817

Junior Devvie





« Reply #1 - Posted 2009-09-30 21:53:10 »


And the class that uses it - the main problem with this class is the painfully long time until
the observer gets notified the first time (and i suspect getBytesRead() and getBytesPerSecond() churn).
A way to remove the synchronization there would be great also. I just slapdashed synchronize allover the place without
thinking it through.  Tongue

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
225  
226  
227  
228  
229  
230  
231  
232  
233  
234  
235  
236  
237  
238  
239  
240  
241  
242  
243  
244  
245  
246  
247  
248  
249  
250  
251  
252  
253  
254  
255  
256  
257  
258  
259  
260  
261  
262  
263  
264  
265  
266  
267  
268  
269  
270  
271  
272  
273  
274  
275  
276  
277  
278  
279  
280  
281  
282  
283  
284  
285  
286  
287  
288  
289  
290  
291  
292  
293  
294  
295  
296  
297  
298  
299  
300  
301  
302  
303  
304  
305  
306  
307  
package downloads;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;
import java.util.Observable;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.SwingUtilities;

public final class Download extends Observable {

    private BigInteger size;
    private File localFile;
    private volatile StatisticsInputStream stream;
    private final URL url;
    private boolean cancelled = false, started = false;
    private final String mimeType;

    public Download(URL downloadURL, File localFile, long expectedSizeInBytes, String mimeType) throws DownloadException {
        super();
        this.size = BigInteger.valueOf(expectedSizeInBytes);
        this.localFile = localFile;
        this.url = downloadURL;
        this.mimeType = mimeType;
    }

    /**
     * reate a download with a unknown mimeType
     * @param downloadURL
     * @param localFile
     * @param expectedSizeInBytes
     * @throws DownloadException
     */

    public Download(URL downloadURL, File localFile, long expectedSizeInBytes) throws DownloadException {
        this(downloadURL, localFile, -1, null);
    }

    /**
     * Create a download with a unknown size and a unknown mimeType
     * @param downloadURL
     * @param localFile
     * @throws DownloadException
     */

    public Download(URL downloadURL, File localFile) throws DownloadException {
        this(downloadURL, localFile, -1, null);
    }

    /**
     * The local file where the download is being put
     * @return
     */

    public File getDownloadedFile() {
        return localFile;
    }

    /**
     * @return the mimeType null if unknown
     */

    public String getMimeType() {
        return mimeType;
    }

    /**
     * @return the expected download size or -1
     * if not known.
     */

    public synchronized BigInteger getExpectedSize() {
        return size;
    }

    /**
     * The origin of the download
     * @return
     */

    public URL getURL() {
        return url;
    }

    /**
     * Cancel the download, only cancels if it already started
     * @return if it was cancelled
     */

    public synchronized boolean cancel() {
        if (!isDone()) {
            if (started) {
                cancelled = true;
                try {
                    stream.close();
                } catch (Exception ex) {
                    //if failed closing, bah
                }
            }
            setChanged();
            notifyObservers();
            return !isDone();
        }
        setChanged();
        notifyObservers();
        return false;
    }

    /**
     * Retry the download (only if canceled)
     * @return
     */

    public synchronized boolean retry() {
        if (!cancelled) {
            return false;
        }
        try {
            started = false;
            start();
            cancelled = false;
        } catch (IOException ex) {
            setChanged();
            notifyObservers();
            Logger.getLogger(Download.class.getName()).log(Level.SEVERE, "Couldn't start download", ex);
            return false;
        }
        setChanged();
        notifyObservers();
        return true;
    }

    /**
     * Returns if the download is done.
     * @return
     */

    public synchronized boolean isDone() {
        //if size is unknown it will be set at the end of the download
        return getBytesRead().longValue() == size.longValue();
    }

    /**
     * Returns if the download is cancelled
     * @return
     */

    public synchronized boolean isCancelled() {
        return cancelled;
    }

    /**
     * Percentage of the download from 0 to 100
     * Only valid if getExpectedSize != -1
     * @return
     */

    public synchronized BigInteger getProgress() {
        BigInteger hundred = BigInteger.valueOf(100);
        return getBytesRead().multiply(hundred).divide(getExpectedSize());
    }

    /**
     * Name of the downloaded url
     * @return
     */

    public String getName() {
        String realpath;
        try {
            realpath = URLDecoder.decode(url.getPath(), "UTF-8");
        } catch (UnsupportedEncodingException ex) {
            throw new AssertionError("UTF-8 is always a supported encoding.");
        }
        //avoid directories path seperator indexes.
        int index = realpath.lastIndexOf('/');

        if (index == (realpath.length() - 1)) {//directory
            int index2 = realpath.lastIndexOf('/', realpath.length() - 2);
            return realpath.substring(index2 + 1, index);
        } else {//file
            return realpath.substring(index + 1);
        }
    }

    /**
     * The number of bytes read so far from the download
     * @return
     */

    public synchronized BigInteger getBytesRead() {
        return (stream == null) ? BigInteger.ZERO : stream.getBytesRead();
    }

    /**
     * The number of bytes per second
     * @return
     */

    public synchronized BigDecimal getBytesPerSecond() {
        return (stream == null) ? BigDecimal.ZERO : stream.getBytesPerSecond();
    }

    /**
     * The time remaining
     * @return
     */

    public synchronized BigDecimal getTimeRemaining() {

        return (stream == null) ? BigDecimal.ZERO : stream.getTimeRemaining();
    }
    private static Timer observersTimedNotify = new Timer(true);

    /**
     * Start the download
     * @throws IOException
     */

    public synchronized void start() throws IOException {
        if (started) {
            throw new IllegalStateException("Can't start a download twice, try retry");
        }
        started = true;

        final URLConnection conn = url.openConnection();
        //conn.setConnectTimeout(1500);
        if (size.longValue() == -1) {
            //may or may not work
            size = BigInteger.valueOf(conn.getContentLength());
        }

        //start the stream in the same thread that starts the download(for visibility)
        try {
            stream = new ObservableStatisticsInputStream(conn.getInputStream(), Download.this.getExpectedSize().longValue());
        } catch (IOException ex) {
            cancel();
            return;
        } finally {
            setChanged();
            notifyObservers();
        }

        Runnable task = new Runnable() {

            public void run() {
                try {
                    writeInto(stream, true, new FileOutputStream(localFile), 1024, true);
                    //not finished prematurely
                    if (!isCancelled()) {
                        size = stream.getBytesRead();
                    }
                } catch (IOException ex) {
                    Logger.getLogger(Download.class.getName()).log(Level.SEVERE, "Couldn't start download", ex);
                    cancel();
                } finally {
                    setChanged();
                    notifyObservers();
                }
            }
        };

        if (SwingUtilities.isEventDispatchThread()) {
            new Thread(task).start();
        } else {
            task.run();
        }

        class RecursiveNotify extends TimerTask {

            @Override
            public void run() {
                notifyObservers();
                if (!isDone() && !isCancelled()) {
                    observersTimedNotify.schedule(new RecursiveNotify(), 100);
                }
            }
        }

        observersTimedNotify.schedule(new RecursiveNotify(), 100);
    }

    private static void writeInto(final InputStream input, boolean closeInput, final OutputStream output, final int bufferSize, boolean closeOutput) throws IOException {
        final byte[] buffer = new byte[bufferSize];
        int n = 0;
        try {
            while (-1 != (n = input.read(buffer))) {
                output.write(buffer, 0, n);
            }
        } finally {
            if (closeInput && input != null) {
                input.close();
            }
            if (closeOutput && output != null) {
                output.close();
            }
        }
    }

    private final class ObservableStatisticsInputStream extends StatisticsInputStream {

        public ObservableStatisticsInputStream(InputStream delegate, long expectedSize) {
            super(delegate, expectedSize);
        }

        @Override
        protected void updateMeasure(int bytesRead) {
            super.updateMeasure(bytesRead);
            setChanged();
        }
    }
}
Offline pjt33
« Reply #2 - Posted 2009-10-01 13:26:16 »

I'm asking for help on this, since it combines both areas that i'm a absolute noob on, floating point errors, timers and concurrency.
Floating point errors: just don't do any subtraction in floats. The number of bytes read is an int or a long; the time elapsed is an int or a long; cast to double for a division when you query.
Concurrency: why are there concurrency issues? I assume you're not calling read() from multiple threads.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

toopeicgaming1999 (51 views)
2014-11-26 15:22:04

toopeicgaming1999 (43 views)
2014-11-26 15:20:36

toopeicgaming1999 (8 views)
2014-11-26 15:20:08

SHC (24 views)
2014-11-25 12:00:59

SHC (24 views)
2014-11-25 11:53:45

Norakomi (25 views)
2014-11-25 11:26:43

Gibbo3771 (23 views)
2014-11-24 19:59:16

trollwarrior1 (36 views)
2014-11-22 12:13:56

xFryIx (75 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!