📄 httpdate.java
字号:
package servlet.http;
import java.io.IOException;
import java.io.OutputStream;
import servlet.util.Ascii;
// Referenced classes of package servlet.http:
// HttpDateUpdater
public class HttpDate extends Ascii
{
public HttpDate(long l)
{
setTime(l);
}
public HttpDate()
{
}
public void parse(byte abyte0[], int i, int j)
{
try
{
int k = i + j;
i = parseDay(abyte0, i);
byte byte0;
while(Ascii.isWhite(byte0 = abyte0[i++])) ;
if(Ascii.isDigit(byte0))
{
int l;
for(l = byte0 - 48; Ascii.isDigit(byte0 = abyte0[i++]); l = (l * 10 + byte0) - 48);
mday = l;
if(Ascii.isWhite(byte0))
while(Ascii.isWhite(byte0 = abyte0[i++])) ;
else
if(byte0 == 45)
byte0 = abyte0[i++];
else
throw new IllegalArgumentException();
i = parseMonth(byte0, abyte0, i);
if(Ascii.isWhite(byte0 = abyte0[i++]))
while(Ascii.isWhite(byte0 = abyte0[i++])) ;
else
if(byte0 == 45)
byte0 = abyte0[i++];
else
throw new IllegalArgumentException();
if(!Ascii.isDigit(byte0))
throw new IllegalArgumentException();
for(l = byte0 - 48; Ascii.isDigit(byte0 = abyte0[i++]); l = (l * 10 + byte0) - 48);
if(l < 100)
l += 1900;
if(mday > daysInMonth(mon, l))
throw new IllegalArgumentException();
year = l - 1900;
i = parseTime(abyte0, i);
while(Ascii.isWhite(byte0 = abyte0[i++])) ;
if(Ascii.toLower(byte0) != 103 || Ascii.toLower(abyte0[i++]) != 109 || Ascii.toLower(abyte0[i++]) != 116)
throw new IllegalArgumentException();
}
else
{
i = parseMonth(byte0, abyte0, i);
if(!Ascii.isWhite(byte0 = abyte0[i++]))
throw new IllegalArgumentException();
while(Ascii.isWhite(byte0 = abyte0[i++])) ;
if(!Ascii.isDigit(byte0))
throw new IllegalArgumentException();
int i1;
for(i1 = byte0 - 48; Ascii.isDigit(byte0 = abyte0[i++]); i1 = (i1 * 10 + byte0) - 48);
mday = i1;
i = parseTime(abyte0, i);
while(Ascii.isWhite(byte0 = abyte0[i++])) ;
for(i1 = byte0 - 48; i < k && Ascii.isDigit(byte0 = abyte0[i++]); i1 = (i1 * 10 + byte0) - 48);
if(i1 < 1900 || mday > daysInMonth(mon, i1))
throw new IllegalArgumentException();
year = i1 - 1900;
}
return;
}
catch(ArrayIndexOutOfBoundsException ex)
{
throw new IllegalArgumentException();
}
}
public void parse(String s)
{
byte abyte0[] = toBytes(s);
parse(abyte0, 0, abyte0.length);
}
private int parseDay(byte abyte0[], int i)
{
byte byte0;
while(Ascii.isWhite(byte0 = abyte0[i++])) ;
int j = Ascii.toLower(byte0);
j = j << 8 | Ascii.toLower(abyte0[i++]);
j = j << 8 | Ascii.toLower(abyte0[i++]);
switch(j)
{
case 7566702:
wday = 0;
break;
case 7171950:
wday = 1;
break;
case 7632229:
wday = 2;
break;
case 7824740:
wday = 3;
break;
case 7628917:
wday = 4;
break;
case 6713961:
wday = 5;
break;
case 7561588:
wday = 6;
break;
default:
throw new IllegalArgumentException();
}
if(Ascii.isAlpha(byte0 = abyte0[i++]))
{
byte abyte1[] = days[wday];
int k = abyte1.length;
for(int l = 3; l < k; l++)
{
if(Ascii.toLower(byte0) != abyte1[l])
throw new IllegalArgumentException();
byte0 = abyte0[i++];
}
}
if(!Ascii.isWhite(byte0) && byte0 != 44)
throw new IllegalArgumentException();
else
return i;
}
private int parseMonth(int i, byte abyte0[], int j)
{
int k = Ascii.toLower(i);
k = k << 8 | Ascii.toLower(abyte0[j++]);
k = k << 8 | Ascii.toLower(abyte0[j++]);
switch(k)
{
case 6971758:
mon = 0;
break;
case 6710626:
mon = 1;
break;
case 7168370:
mon = 2;
break;
case 6385778:
mon = 3;
break;
case 7168377:
mon = 4;
break;
case 6976878:
mon = 5;
break;
case 6976876:
mon = 6;
break;
case 6387047:
mon = 7;
break;
case 7562608:
mon = 8;
break;
case 7299956:
mon = 9;
break;
case 7237494:
mon = 10;
break;
case 6579555:
mon = 11;
break;
default:
throw new IllegalArgumentException();
}
return j;
}
private int parseTime(byte abyte0[], int i)
{
byte byte0;
while(Ascii.isWhite(byte0 = abyte0[i++])) ;
if(Ascii.isDigit(byte0))
{
int j;
for(j = byte0 - 48; Ascii.isDigit(byte0 = abyte0[i++]); j = (j * 10 + byte0) - 48);
if(j < 24 && byte0 == 58 && Ascii.isDigit(byte0 = abyte0[i++]))
{
hour = j;
for(j = byte0 - 48; Ascii.isDigit(byte0 = abyte0[i++]); j = (j * 10 + byte0) - 48);
if(j < 60 && byte0 == 58 && Ascii.isDigit(byte0 = abyte0[i++]))
{
min = j;
for(j = byte0 - 48; Ascii.isDigit(byte0 = abyte0[i++]); j = (j * 10 + byte0) - 48);
if(j < 60 && byte0 == 32)
{
sec = j;
return i;
}
}
}
}
throw new IllegalArgumentException();
}
public void write(OutputStream outputstream)
throws IOException
{
byte abyte0[] = new byte[DATELEN];
outputstream.write(abyte0, 0, getBytes(abyte0, 0, abyte0.length));
}
public String toString()
{
byte abyte0[] = new byte[DATELEN];
return new String(abyte0, 0, 0, getBytes(abyte0, 0, abyte0.length));
}
public int getBytes(byte abyte0[], int i, int j)
{
if(j < DATELEN)
{
throw new IllegalArgumentException("array too small");
}
else
{
byte abyte1[] = days[wday];
abyte0[i++] = abyte1[0];
abyte0[i++] = abyte1[1];
abyte0[i++] = abyte1[2];
abyte0[i++] = 44;
abyte0[i++] = 32;
abyte0[i++] = (byte)(48 + mday / 10);
abyte0[i++] = (byte)(48 + mday % 10);
abyte0[i++] = 32;
abyte1 = months[mon];
abyte0[i++] = abyte1[0];
abyte0[i++] = abyte1[1];
abyte0[i++] = abyte1[2];
abyte0[i++] = 32;
int k = year + 1900;
abyte0[i++] = (byte)(48 + k / 1000);
abyte0[i++] = (byte)(48 + (k %= 1000) / 100);
abyte0[i++] = (byte)(48 + (k %= 100) / 10);
abyte0[i++] = (byte)(48 + k % 10);
abyte0[i++] = 32;
abyte0[i++] = (byte)(48 + hour / 10);
abyte0[i++] = (byte)(48 + hour % 10);
abyte0[i++] = 58;
abyte0[i++] = (byte)(48 + min / 10);
abyte0[i++] = (byte)(48 + min % 10);
abyte0[i++] = 58;
abyte0[i++] = (byte)(48 + sec / 10);
abyte0[i++] = (byte)(48 + sec % 10);
abyte0[i++] = 32;
abyte0[i++] = 71;
abyte0[i++] = 77;
abyte0[i] = 84;
return DATELEN;
}
}
public long getTime()
{
int i = year;
int j = (i - 70) * DAYS_PER_YEAR;
j += (i - 69) / 4;
j -= (i - 1) / 100;
j += (i + 299) / 400;
j += (daysBeforeMonth(mon, i) + mday) - 1;
return (sec + 60 * (min + 60 * (hour + 24 * j))) * 1000L;
}
public static long getCurrentTime()
{
return updater.get();
}
protected static boolean isLeapYear(int i)
{
return (i & 0x3) == 0 && (i % 100 != 0 || (i + 300) % 400 != 0);
}
protected static int daysInMonth(int i, int j)
{
if(isLeapYear(j))
return daysInMonthLeap[i];
else
return daysInMonth[i];
}
protected static int daysBeforeMonth(int i, int j)
{
if(isLeapYear(j))
return daysBeforeMonthLeap[i];
else
return daysBeforeMonth[i];
}
public void setTime(long l)
{
int i = (int)(l / 1000L);
int j = i / SECS_PER_DAY;
i -= j * SECS_PER_DAY;
hour = i / SECS_PER_HOUR;
i -= hour * SECS_PER_HOUR;
min = i / SECS_PER_MIN;
sec = i - min * SECS_PER_MIN;
wday = (j + BASE_DAY_OF_WEEK) % 7;
int k = j / DAYS_PER_LEAP;
j -= k * DAYS_PER_LEAP;
boolean flag = false;
int i1 = k * 4 + 70;
if(j >= DAYS_PER_YEAR)
{
i1++;
j -= DAYS_PER_YEAR;
if(j >= DAYS_PER_YEAR)
{
i1++;
j -= DAYS_PER_YEAR;
if(j >= DAYS_PER_YEAR + 1)
{
i1++;
j -= DAYS_PER_YEAR + 1;
}
else
{
flag = true;
}
}
}
year = i1;
short aword0[] = flag ? daysBeforeMonthLeap : daysBeforeMonth;
int j1;
for(j1 = 1; aword0[j1] <= j; j1++);
mon = --j1;
mday = (j - aword0[j1]) + 1;
}
public void setTime()
{
updater.get(this);
}
protected static byte[] toBytes(String s)
{
byte abyte0[] = new byte[s.length()];
s.getBytes(0, abyte0.length, abyte0, 0);
return abyte0;
}
protected int sec;
protected int min;
protected int hour;
protected int mday;
protected int mon;
protected int year;
protected int wday;
protected static final byte days[][] = {
toBytes("Sunday"), toBytes("Monday"), toBytes("Tuesday"), toBytes("Wednesday"), toBytes("Thursday"), toBytes("Friday"), toBytes("Saturday")
};
protected static final byte months[][] = {
toBytes("Jan"), toBytes("Feb"), toBytes("Mar"), toBytes("Apr"), toBytes("May"), toBytes("Jun"), toBytes("Jul"), toBytes("Aug"), toBytes("Sep"), toBytes("Oct"),
toBytes("Nov"), toBytes("Dec")
};
protected static byte daysInMonth[] = {
31, 28, 31, 30, 31, 30, 31, 31, 30, 31,
30, 31
};
protected static byte daysInMonthLeap[] = {
31, 29, 31, 30, 31, 30, 31, 31, 30, 31,
30, 31
};
protected static short daysBeforeMonth[] = {
0, 31, 59, 90, 120, 151, 181, 212, 243, 273,
304, 334, 365
};
protected static short daysBeforeMonthLeap[] = {
0, 31, 60, 91, 121, 152, 182, 213, 244, 274,
305, 335, 366
};
private static final String DATESTR = "Sun, 06 Nov 1994 08:49:37 GMT";
public static final int DATELEN = "Sun, 06 Nov 1994 08:49:37 GMT".length();
protected static HttpDateUpdater updater;
protected static int DAYS_PER_YEAR;
protected static int DAYS_PER_LEAP;
protected static int SECS_PER_MIN;
protected static int SECS_PER_HOUR;
protected static int SECS_PER_DAY;
protected static int BASE_DAY_OF_WEEK = 4;
static
{
updater = new HttpDateUpdater(1000L);
Thread thread = new Thread(updater);
thread.setPriority(10);
thread.start();
DAYS_PER_YEAR = 365;
DAYS_PER_LEAP = DAYS_PER_YEAR * 4 + 1;
SECS_PER_MIN = 60;
SECS_PER_HOUR = SECS_PER_MIN * 60;
SECS_PER_DAY = SECS_PER_HOUR * 24;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -