summaryrefslogtreecommitdiff
path: root/java/src/org/singinst/uf/view/AppletStore.java
blob: 344bc92ef75ded3a4d08dbfe467d2d5ee5b71b46 (plain)
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
package org.singinst.uf.view;

import java.applet.AppletContext;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.singinst.uf.presenter.Store;

public class AppletStore extends Store {

	private final AppletContext appletContext;

	public AppletStore(AppletContext appletContext) {
		this.appletContext = appletContext;
	}

	@Override
	public Double get(String key) {
		InputStream stream = appletContext.getStream(key);
		if (stream == null) {
			return null;
		}
		try {
			String string = new BufferedReader(new InputStreamReader(stream, "UTF-8")).readLine();
			return Double.valueOf(string);
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException(e);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public void put(String key, double value) {
		try {
			appletContext.setStream(key, new ByteArrayInputStream(("" + value).getBytes("UTF-8")));
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException(e);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
}