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 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
|
package org.gnu.pilotlink;
import java.util.Date;
import java.util.GregorianCalendar;
/**
* Description of the Class
*
*@author stephan
*@created 5. Dezember 2004
*/
public abstract class Record {
/**
* Description of the Field
*/
public final static int DELETED = 0x80;
/**
* Description of the Field
*/
public final static int DIRTY = 0x40;
/**
* Description of the Field
*/
public final static int BUSY = 0x20;
/**
* Description of the Field
*/
public final static int SECRET = 0x10;
/**
* Description of the Field
*/
public final static int ARCHIVED = 0x8;
/**
* Description of the Field
*/
protected long id;
/**
* Description of the Field
*/
protected int attribs;
/**
* Description of the Field
*/
protected int category;
/**
* Description of the Field
*/
protected int size;
/**
* Constructor for the Record object
*/
public Record() {
id = 0;
attribs = 0;
category = 0;
size = 0;
}
/**
* Gets the deleted attribute of the Record object
*
*@return The deleted value
*/
public boolean isDeleted() {
return (attribs & DELETED) != 0;
}
/**
* Gets the secret attribute of the Record object
*
*@return The secret value
*/
public boolean isSecret() {
return (attribs & SECRET) != 0;
}
/**
* Gets the archived attribute of the Record object
*
*@return The archived value
*/
public boolean isArchived() {
return (attribs & ARCHIVED) != 0;
}
/**
* Gets the dirty attribute of the Record object
*
*@return The dirty value
*/
public boolean isDirty() {
return (attribs & DIRTY) != 0;
}
/**
* Gets the busy attribute of the Record object
*
*@return The busy value
*/
public boolean isBusy() {
return (attribs & BUSY) != 0;
}
/**
* Sets the dirty attribute of the Record object
*
*@param d The new dirty value
*/
public void setDirty(boolean d) {
if (d) {
attribs = attribs | DIRTY;
} else {
attribs = attribs & (~DIRTY);
}
}
public void setSecret(boolean b) {
if (b) {
attribs |=SECRET;
} else {
attribs &=(~SECRET);
}
}
/**
* Sets the archived attribute of the Record object
*
*@param a The new archived value
*/
public void setArchived(boolean a) {
if (a) {
attribs = attribs | ARCHIVED;
} else {
attribs = attribs & (~ARCHIVED);
}
}
/**
* Constructor for the Record object
*
*@param i Description of the Parameter
*@param att Description of the Parameter
*@param cat Description of the Parameter
*@param sz Description of the Parameter
*/
public Record(int i, int att, int cat, int sz) {
size = sz;
id = i;
attribs = att;
category = cat;
}
/**
* Constructor for the Record object
*
*@param r Description of the Parameter
*/
public Record(Record r) {
size = r.getSize();
id = r.getId();
attribs = r.getAttribs();
category = r.getCategory();
setBuffer(r.getBuffer());
}
/**
* Sets the id attribute of the Record object
*
*@param i The new id value
*/
public void setId(long i) {
id = i;
}
/**
* Gets the category attribute of the Record object
*
*@return The category value
*/
public int getCategory() {
return category;
}
/**
* Sets the category attribute of the Record object
*
*@param c The new category value
*/
public void setCategory(int c) {
category = c;
}
/**
* Sets the attribs attribute of the Record object
*
*@param a The new attribs value
*/
public void setAttribs(int a) {
attribs = a;
}
/**
* Gets the attribs attribute of the Record object
*
*@return The attribs value
*/
public int getAttribs() {
return attribs;
}
/**
* Gets the id attribute of the Record object
*
*@return The id value
*/
public long getId() {
return id;
}
/**
* Gets the size attribute of the Record object
*
*@return The size value
*/
public int getSize() {
return size;
}
/**
* Sets the size attribute of the Record object
*
*@param s The new size value
*/
public void setSize(int s) {
size = s;
}
/**
* Gets the buffer attribute of the Record object
*
*@return The buffer value
*/
public abstract byte[] getBuffer();
/**
* Sets the buffer attribute of the Record object
*
*@param buf The new buffer value
*/
public abstract void setBuffer(byte buf[]);
/**
* Gets the dateAt attribute of the Record object
*
*@param idx Description of the Parameter
*@return The dateAt value
*/
public Date getDateAt(int idx) {
return getDateAt(getBuffer(), idx);
}
/**
* Gets the dateAt attribute of the Record class
*
*@param buffer Description of the Parameter
*@param idx Description of the Parameter
*@return The dateAt value
*/
public static Date getDateAt(byte buffer[], int idx) {
int binary = (buffer[idx] & 0xff) * 256 + (buffer[idx + 1] & 0xff);
int year = binary & 0xfe00;
year = year >> 9;
year += 1904;
int month = binary & 0x01e0;
month = month >> 5;
month--;
int day = binary & 0x001f;
//System.out.println("Y: "+year+" M:"+month+" D: "+day);
GregorianCalendar cal = new GregorianCalendar(year, month, day);
return cal.getTime();
}
/**
* Sets the dateAt attribute of the Record class
*
*@param buffer The new dateAt value
*@param d The new dateAt value
*@param idx The new dateAt value
*/
public static void setDateAt(byte buffer[], Date d, int idx) {
GregorianCalendar cal = new GregorianCalendar();
//System.out.println("Setting date "+d+" at: "+idx);
cal.setTime(d);
int binary = 0;
int year = cal.get(GregorianCalendar.YEAR);
int month = cal.get(GregorianCalendar.MONTH);
int day = cal.get(GregorianCalendar.DAY_OF_MONTH);
year -= 1904;
year = year << 9;
month++;
month = month << 5;
binary = year | month | day;
buffer[idx + 1] = (byte) (binary & 0x00ff);
buffer[idx] = (byte) ((binary & 0xff00) >> 8);
}
/**
* Sets the dateTimeAt attribute of the Record class
*
*@param buffer The new dateTimeAt value
*@param d The new dateTimeAt value
*@param idx The new dateTimeAt value
*@param tidx The new dateTimeAt value
*/
public static void setDateTimeAt(byte buffer[], Date d, int idx, int tidx) {
setDateAt(buffer, d, idx);
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(d);
int h = cal.get(GregorianCalendar.HOUR_OF_DAY);
int m = cal.get(GregorianCalendar.MINUTE);
buffer[tidx] = (byte) h;
buffer[tidx + 1] = (byte) m;
}
/**
* Gets the dateTimeAt attribute of the Record object
*
*@param idx Description of the Parameter
*@param tidx Description of the Parameter
*@return The dateTimeAt value
*/
public Date getDateTimeAt(int idx, int tidx) {
return getDateTimeAt(getBuffer(), idx, tidx);
}
/**
* Gets the dateTimeAt attribute of the Record class
*
*@param buffer Description of the Parameter
*@param idx Description of the Parameter
*@param tidx Description of the Parameter
*@return The dateTimeAt value
*/
public static Date getDateTimeAt(byte buffer[], int idx, int tidx) {
int binary = (buffer[idx] & 0xff) * 256 + (buffer[idx + 1] & 0xff);
int year = binary & 0xfe00;
year = year >> 9;
year += 1904;
int month = binary & 0x01e0;
month = month >> 5;
month--;
int day = binary & 0x001f;
//System.out.println("Y: "+year+" M:"+month+" D: "+day);
int hour = buffer[tidx] & 0xff;
int min = buffer[tidx + 1] & 0xff;
GregorianCalendar cal = new GregorianCalendar(year, month, day, hour, min, 0);
return cal.getTime();
}
/**
* Sets the stringAt attribute of the Record class
*
*@param buffer The new stringAt value
*@param s The new stringAt value
*@param idx The new stringAt value
*@return Description of the Return Value
*/
public static int setStringAt(byte buffer[], String s, int idx) {
if (s == null) {
return idx;
}
byte str[];
str = s.getBytes();
for (int i = 0; i < str.length; i++) {
if (s.charAt(i) == '\ufffd') {
buffer[idx + i] = (byte) 252;
} else if (s.charAt(i) == '\ufffd') {
buffer[idx + i] = (byte) 246;
} else if (s.charAt(i) == '\ufffd') {
buffer[idx + i] = (byte) 228;
} else if (s.charAt(i) == '\ufffd') {
buffer[idx + i] = (byte) 223;
} else if (s.charAt(i) == '\ufffd') {
buffer[idx + i] = (byte) 196;
} else if (s.charAt(i) == '\ufffd') {
buffer[idx + i] = (byte) 220;
} else if (s.charAt(i) == '\ufffd') {
buffer[idx + i] = (byte) 214;
} else {
buffer[idx + i] = str[i];
}
}
//System.out.println();
buffer[idx + str.length] = 0;
return (idx + str.length + 1);
}
/**
* Gets the stringAt attribute of the Record object
*
*@param idx Description of the Parameter
*@return The stringAt value
*/
public String getStringAt(int idx) {
return getStringAt(getBuffer(), idx);
}
/**
* Gets the stringAt attribute of the Record class
*
*@param buffer Description of the Parameter
*@param idx Description of the Parameter
*@return The stringAt value
*/
public static String getStringAt(byte buffer[], int idx) {
String str = "";
while (idx < buffer.length && buffer[idx] != 0) {
if (buffer[idx] + 256 == 252) {
str += "\ufffd";
} else if (buffer[idx] + 256 == 246) {
str += "\ufffd";
} else if (buffer[idx] + 256 == 228) {
str += "\ufffd";
} else if (buffer[idx] + 256 == 223) {
str += "\ufffd";
} else if (buffer[idx] + 256 == 196) {
str += "\ufffd";
} else if (buffer[idx] + 256 == 220) {
str += "\ufffd";
} else if (buffer[idx] + 256 == 214) {
str += "\ufffd";
} else {
str += (char) buffer[idx];
}
idx++;
}
return str;
}
/**
* Sets the intAt attribute of the Record class
*
*@param buffer The new intAt value
*@param i The new intAt value
*@param idx The new intAt value
*/
public static void setIntAt(byte buffer[], int i, int idx) {
buffer[idx + 1] = (byte) (i & 0x00ff);
buffer[idx] = (byte) ((i & 0xff00) >> 8);
}
/**
* Gets the intAt attribute of the Record object
*
*@param idx Description of the Parameter
*@return The intAt value
*/
public int getIntAt(int idx) {
return getIntAt(getBuffer(), idx);
}
/**
* Gets the intAt attribute of the Record class
*
*@param buffer Description of the Parameter
*@param idx Description of the Parameter
*@return The intAt value
*/
public static int getIntAt(byte buffer[], int idx) {
return ((buffer[idx] & 0xff) << 8) + (buffer[idx + 1] & 0xff);
}
/**
* Gets the doubleAt attribute of the Record class
*
*@param buffer Description of the Parameter
*@param idx Description of the Parameter
*@return The doubleAt value
*/
public static double getDoubleAt(byte buffer[], int idx) {
long l = getDoubleLongAt(buffer, idx);
return Double.longBitsToDouble(l);
}
/**
* Sets the doubleAt attribute of the Record class
*
*@param buffer The new doubleAt value
*@param v The new doubleAt value
*@param idx The new doubleAt value
*/
public static void setDoubleAt(byte buffer[], double v, int idx) {
long l = Double.doubleToLongBits(v);
setDoubleLongAt(buffer, l, idx);
}
/**
* Gets the doubleLongAt attribute of the Record class
*
*@param buffer Description of the Parameter
*@param idx Description of the Parameter
*@return The doubleLongAt value
*/
public static long getDoubleLongAt(byte buffer[], int idx) {
long value = 0;
//A clumsy hack, but quick
//could not think of a better version
//java makes is impossible to convert large bytes to chars wihtout
//losing information
for (int octet = 0; octet < 8; octet++) {
for (int bit = 0; bit < 8; bit++) {
if ((buffer[octet + idx] & (1 << bit)) != 0) {
value |= (1L << (56 - octet * 8 + bit));
}
}
}
return value;
}
/**
* Sets the doubleLongAt attribute of the Record class
*
*@param buffer The new doubleLongAt value
*@param v The new doubleLongAt value
*@param idx The new doubleLongAt value
*/
public static void setDoubleLongAt(byte buffer[], long v, int idx) {
buffer[idx + 0] = (byte) ((v & 0xff00000000000000L) >> 56);
buffer[idx + 1] = (byte) ((v & 0x00ff000000000000L) >> 48);
buffer[idx + 2] = (byte) ((v & 0x0000ff0000000000L) >> 40);
buffer[idx + 3] = (byte) ((v & 0x000000ff00000000L) >> 32);
buffer[idx + 4] = (byte) ((v & 0x00000000ff000000L) >> 24);
buffer[idx + 5] = (byte) ((v & 0x0000000000ff0000L) >> 16);
buffer[idx + 6] = (byte) ((v & 0x000000000000ff00L) >> 8);
buffer[idx + 7] = (byte) ((v & 0x00000000000000ffL));
}
/**
* Sets the longAt attribute of the Record class
*
*@param buffer The new longAt value
*@param v The new longAt value
*@param idx The new longAt value
*/
public static void setLongAt(byte buffer[], long v, int idx) {
buffer[idx + 0] = (byte) ((v & 0x00000000ff000000L) >> 24);
buffer[idx + 1] = (byte) ((v & 0x0000000000ff0000L) >> 16);
buffer[idx + 2] = (byte) ((v & 0x000000000000ff00L) >> 8);
buffer[idx + 3] = (byte) ((v & 0x00000000000000ffL));
}
/**
* Gets the longAt attribute of the Record class
*
*@param buffer Description of the Parameter
*@param idx Description of the Parameter
*@return The longAt value
*/
public static long getLongAt(byte buffer[], int idx) {
long value = 0;
//A clumsy hack, but quick
//could not think of a better version
//java makes is impossible to convert large bytes to chars wihtout
//losing information
for (int octet = 0; octet < 4; octet++) {
for (int bit = 0; bit < 8; bit++) {
if ((buffer[octet + idx] & (1 << bit)) != 0) {
value |= (1L << (24 - octet * 8 + bit));
}
}
}
return value;
}
}
|