Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Java Programmers FAQ

36 views
Skip to first unread message

Peter van der Linden

unread,
Mar 26, 1999, 3:00:00 AM3/26/99
to
Here are the DIFFS for the latest Java FAQ.

11c11
- Last-modified: 1999/03/16
---
+ Last-modified: 1999/03/26
91,93c92,93
- RMI).
- If your interest is Java, leave the downrev J++ book back on the
- shelf.
---
+ RMI). J++ does not implement Java 2. If your interest is Java,
+ leave the downrev J++ book back on the shelf.
1094c1094,1097
- There are some good Java games applets at
---
+ There are some excellent Java games applets at
+ http://www.javaarcade.com Check out the pinball -- dig that crazy
+ rhythm, man.
+ There are more good Java games applets at
+
1621,1625c1631,1637
- Many people think that contracts prohibiting the distribution of a
- third party's products are somewhat sleazy. Such contracts are also in
- restraint of competition and illegal when used by a monopoly. This is
- why Microsoft is facing mounting legal problems in the United States,
- Italy, Brazil, and the European Union.
---
+ Many people think that contracts between "A" and "B",
+ restricting/discouraging "B" from distributing "C's" products are
+ sleazy. ["A" is Microsoft, "B" is AOL, many ISPs, computer vendors,
+ etc. "C" is Netscape"]. Such contracts are in restraint of competition
+ and illegal when used by a monopoly. This is why Microsoft is facing
+ mounting legal problems in the United States, Italy, Brazil, and the
+ European Union.
1970c1982
- $ js pack/*.java # works
---
+ $ jc pack/*.java # works
2124,2126c2136,2137
- This policy actually gives you more consistent results than on a system
- where floating point output is deliberately rounded to make the output
- "pretty".
---
+ You are actually getting more accuracy than on systems (like C,C++)
+ that suppress the less significant digits.
7230a7242,7250
+ 20. (Sect. 15) How do I "timeout" a socket read, as the "select" function
+ in Unix does?
+
+ [*] There are two timeouts at issue here.
+ Use the ServerSocket.setSoTimeout for timing out your 'accept()' calls.
+
+ For a select() like mechanism, you can poll InputStreams of your
+ connections for available() > 0
+
7264a7285
+
7266,7271c7287,7301
- (application) that could append itself to some other Java program. That
- is just normal file processing, no different to a program written in C,
- C++, Fortran, COBOL, etc. What makes PC viruses dangerous is when they
- have a hidden means of travelling between places, such as being
- attached to code that is automatically executed, like boot sector code,
- or Word macro initialization files.
---
+ (application) that could append itself to some other Java program. They
+ termed it "Strange Brew". That is just normal file processing, no
+ different to a program written in C, C++, Fortran, COBOL, etc. What
+ makes PC viruses dangerous is when they have a hidden means of
+ travelling between places, such as being attached to code that is
+ automatically executed, like boot sector code, or Word macro
+ initialization files. No one has yet produced a virus that does this in
+ Java.
+
+ In March 1999, reports surfaced of a virus called BeanHive. Again this
+ was not Java-related, but relied on the browser user explicitly
+ accepting an unknown certificate as trusted when prompted. If the user
+ allowed that, then any code from that source will be uploaded and run
+ without further input from the user. Don't remove security for
+ untrusted code simply because it asks for it, is the lesson here.
7303a7334
+ See also http://www.hi.is/~logir/logi.crypto/
8095,8098c8126
- Java to get the Julian date, written by Joe Preston: Watch those month
- numbers, this expects month to be in range 1..12. Java Date dumbly uses
- 0..11. (This code is being rewritten for the FAQ. I include the current
- code as a placeholder to remind me to replace it).
---
+ Java to get the Julian date, written by Paul Hill.
8100,8118c8128,8129
+ /**
+ * This Calendar provides Julian Day and "Calendar Day" methods,
+ * The Calendar Day starts at _local_ midnight.
+ *
+ * @author Paul A. Hill
+ * @version 1.1
+ * @since JDK 1.1.7 3/99
+ * @see java.util.Calendar
+ */
+ public class FAQCalendar extends GregorianCalendar implements Cloneable {
+ /**
+ ** this constant and the ones that follow are actually private
+ ** in the GregorianCalendar, so we'll redefine them here.
+ **/
+ public static final long
+ ONE_SECOND = 1000,
+ ONE_MINUTE = ONE_SECOND * 60,
+ ONE_HOUR = ONE_MINUTE * 60;
+ /**
+ * this next constant and the others with it have their uses,
+ * but watch out for DST days and the weeks that contain them.
+ * Note also that 1/1/1970 doesn't start on the first day of the week.
+ */
+ protected static final long
+ ONE_DAY = ONE_HOUR * 24,
+ ONE_WEEK = ONE_DAY * 7;
8121a8158,8161
+ /** The number of days from Jan 1, 4713 BC (Proleptic Julian)
+ ** to 1/1/1970 AD (Gregorian). 1/1/1970 is time 0 for a java.util.Date.
+ **/
+ public static final long JULIAN_DAY_OFFSET = 2440588L;
8123,8127c8163,8174
- The ACM algorithms aren't online (they should be). Look for this one in
- Communications of the ACM, page 444, vol 6, issue 8, Aug 1963. There's
- a follow up in CACM, p661, vol 7, issue 11, Nov 1964 by D.K. Oppenheim.
- The magazine algorithm is in Algol 60 for all you amateur historians.
- The code to convert back from Julian to DD MM YY is:
---
+ /**
+ * Same as GregorianCalendar(). Creates a calendar with the time set to
+ * the moment it was created.
+ *
+ Note: for Brevety I have not provided all of the other
+ * constructors that you can find in GregorianCalendar.
+ *
+ * @see java.util.GregorianCalendar#GregorianCalendar
+ */
+ public FAQCalendar() {
+ super();
+ }
8129,8134c8176,8195
- public void CalendarDate(
- long jdays /* Input julian day #. */
- )
- {
- long temp;
- int wkyear, wkmonth, wkday;
---
+ /**
+ * A calendar day as defined here, is like the Julian Day but it starts
+ * and ends at _local_ 12 midnight, as defined by the timezone which is
+ * attached to this calendar. This value is useful for comparing
+ * any date in the calendar to any other date to determine how many days
+ * between them, i.e. tomorrow - today = 1
+ *
+ * @return the calendar day (see above).
+ * @see #getCalendarDay
+ */
+ public long getCalendarDay() {
+ TimeZone tz = getTimeZone();
+ // Figure the exact offset for the exact time of day.
+ int offset = tz.getOffset( get( ERA ), get( YEAR ),
+ get( MONTH ), get( DAY_OF_MONTH ), get( DAY_OF_WEEK ),
+ (int)((long)get( HOUR_OF_DAY ) * ONE_HOUR +
+ get( MINUTE ) * ONE_MINUTE +
+ get( SECOND ) * ONE_SECOND ) );
+ return round( ONE_DAY, getTime().getTime() + offset ) + JULIAN_DAY_OFFSET;
+ }
8136c8197,8215
- jdays -= 1721119L; /* Remove days before 00-00-0000. */
---
+ /**
+ * Sets the date in the calendar to 00:00 (midnight) on the local calendar day.
+ * See getCalendarDay for the definition of calendar day as used in this class.
+ *
+ * @param calendarDay the day to set the calendar to.
+ * @see #setCalendarDay
+ * @see java.util.TimeZone#getRawOffset
+ */
+ public void setCalendarDay( long calendarDay ) {
+ // Set to the beginning of the Julian day.
+ // Then add in the difference to make it 00:00 local time.
+ setJulianDay( calendarDay );
+ setTimeInMillis( getTime().getTime() - getTimeZone().getRawOffset() );
+ // we may have gone slightly too far, because we used the
+ // raw offset (diff between Standard time to GMT/UT, instead of the
+ // actual value for this day, so during DLS we may be at 1 AM or whatever
+ // the local DLS offset is), so we'll just drop back to midnight.
+ set( HOUR_OF_DAY, 0 );
+ }
8138,8140c8217,8235
- wkyear = (int)((4L * jdays - 1L) / 146097L); /* Get century. */
- jdays = 4L * jdays - 1L - 146097L * wkyear; /* And subtract it out. */
- temp = jdays / 4L;
---
+ /**
+ * Finds the number of days after 12/31/4312 BC 24:00 GMT on a proleptic
+ * Julian Calendar (i.e. extending the Julian Calendar into pre-history)
+ * to the current time.
+ *
+ The Astronomers Julian Day begins at noon. The Julian Day used here
+ * sometimes called the Chronologists or Historians Julian Day
+ * starts at midnight. For more information see
+ * http://www.magnet.ch/serendipity/hermetic/cal_stud/jdn.htm#astronomical
+ *
+ Note: This routine does NOT take into consideration
+ * leap seconds.
+ *
+ * @return the day number of the current time from 1/
+ * @see #getCalendarDay
+ */
+ public long getJulianDay() {
+ return round( ONE_DAY, getTime().getTime() ) + JULIAN_DAY_OFFSET;
+ }
8142,8145c8237,8243
- jdays = (4L * temp + 3L) / 1461L; /* Get century offset. */
- wkyear = (int)(100L * wkyear + jdays); /* Add it into year. */
- temp = (4L * temp + 3L) - 1461L * jdays; /* And subtract it out. */
- temp = (temp + 4L) / 4L;
---
+ /**
+ * Sets the current date contained in this calendar to exactly
+ * 00:00 GMT on the date defined by the Julian Day provided.
+ *
+ * @param julianDay the Julian Day to set the calendar to
+ * @see #setCalendarDay
+ */
8147,8149c8245,8247
- wkmonth = (unsigned)((5L * temp - 3L) / 153L); /* Get month. */
- temp = 5L * temp - 3L - 153L * wkmonth; /* And subtract it out. */
- wkday = (unsigned)((temp + 5L) / 5L);
---
+ public void setJulianDay( long julianDay ) {
+ setTimeInMillis( ( julianDay - JULIAN_DAY_OFFSET ) * ONE_DAY );
+ }
8151,8156c8249,8278
- if (wkmonth < 10) /* Shift Jan & Feb back into first 2 months. */
- wkmonth += 3;
- else
- {
- wkmonth -= 9;
- wkyear++;
---
+ /**
+ * This is a utility routine for rounding (toward negative) to the nearest
+ * multiple of the conversion factor.
+ *
+ BUG? Why is this different than the formula given in
+ * java.util.GregorianCalendar private millisToJulianDay?
+ *
+ * @param conversion typically one of the constants defined
+ * above ONE_MINUTE, ONE_DAY etc.
+ * @param fractions the value to convert expressed in the same units
+ * as the conversion factor (i.e milliseconds).
+ *
+ * @return the value divided by the conversion factor, rounded to the negative.
+ * @see java.util.Calendar
+ */
+ protected static final long round( long conversion, long fractions ) {
+ long wholeUnits;
+
+ // round toward negative:
+ // For secs rounded to minutes -61/60=-1, -60/60=-1, -59/60=0,
+ // but we want -2, -1, -1 not -1,-1,0
+ // or month 0..11 => year 1; -12..-1 => 0; -24..-13 => -1
+
+ if ( fractions >= 0 ) {
+ wholeUnits = fractions / conversion;
+ }
+ else {
+ wholeUnits = ( fractions + 1 )/ conversion - 1;
+ }
+ return wholeUnits;
8158,8159c8280
- set(wkyear, wkmonth - 1, wkday);
- } // CalendarDate
---
+ } // FAQCalendar
8161d8281
- Again, watch those month numbers!
9200c9320,9321
- Nat Pryce, Jeff Luszcz, Brent Callaghan, Neil Allen, Joe Preston, Tim Bell
---
+ Nat Pryce, Jeff Luszcz, Brent Callaghan, Neil Allen, Joe Preston, Tim Bell,
+ Rajesh,
9202c9323
- ------------------------------------------------------------------------
---
+ ------------------------------------------------------------------------

0 new messages