From ee53ad2ccc88b91a1857e75714ae56068bd40952 Mon Sep 17 00:00:00 2001 From: Xavier Del Campo Romero Date: Sat, 18 May 2024 00:16:22 +0200 Subject: WIP --- src/org/slcl/core/Cookie.java | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) (limited to 'src/org/slcl/core/Cookie.java') diff --git a/src/org/slcl/core/Cookie.java b/src/org/slcl/core/Cookie.java index 86f64b5..3bdd320 100644 --- a/src/org/slcl/core/Cookie.java +++ b/src/org/slcl/core/Cookie.java @@ -24,8 +24,9 @@ import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.IOException; -import java.text.ParsePosition; -import java.text.SimpleDateFormat; +import org.joda.time.DateTime; +import org.joda.time.format.DateTimeFormat; +import org.joda.time.format.DateTimeFormatter; import java.util.Date; import java.util.List; @@ -50,7 +51,14 @@ public final class Cookie { final FileInputStream fis = new FileInputStream(f); final InputStreamReader ir = new InputStreamReader(fis); final BufferedReader r = new BufferedReader(ir); - final Results res = from_data(null, r.readLine()); + final String data = r.readLine(); + + if (data == null) { + fis.close(); + throw new IOException("empty file"); + } + + final Results res = from_data(null, data); username = res.username; cookie = res.cookie; @@ -59,8 +67,8 @@ public final class Cookie { } private final class CookieDate { - final private String FMT = "EEE, dd MMM yyyy hh:mm:ss z"; - Date date; + final private String FMT = "EEE, dd MMM yyyy HH:mm:ss z"; + final DateTime date; public CookieDate(final String[] tokens) throws IOException { @@ -77,14 +85,17 @@ public final class Cookie { continue; } - ParsePosition pos = new ParsePosition(0); - final SimpleDateFormat fmt = new SimpleDateFormat(FMT); - final Date d = fmt.parse(date_tokens[1], pos); + final DateTimeFormatter fmt = DateTimeFormat.forPattern(FMT); - if (d != null) { - // TODO: check pos. - date = d; + try { + final DateTime dt = fmt.parseDateTime(date_tokens[1]); + + date = dt; return; + } catch (final IllegalArgumentException e) { + throw new IOException(e.getMessage()); + } catch (final UnsupportedOperationException e) { + throw new IOException(e.getMessage()); } } @@ -93,12 +104,10 @@ public final class Cookie { public String toString() { - final SimpleDateFormat fmt = new SimpleDateFormat(FMT); - - return fmt.format(date, null, null).toString(); + return date.toString(FMT); } - public Date getDate() { + public DateTime getDate() { return date; } } -- cgit v1.2.3