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
|
//******************************************************************************
//
// File: DataOutputStream.java
// Package: edu.rit.io
// Unit: Class edu.rit.io.DataOutputStream
//
// This Java source file is copyright (C) 2009 by Alan Kaminsky. All rights
// reserved. For further information, contact the author, Alan Kaminsky, at
// ark@cs.rit.edu.
//
// This Java source file is part of the Parallel Java Library ("PJ"). PJ is free
// software; you can redistribute it and/or modify it under the terms of the GNU
// General Public License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// PJ is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
// A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//
// Linking this library statically or dynamically with other modules is making a
// combined work based on this library. Thus, the terms and conditions of the
// GNU General Public License cover the whole combination.
//
// As a special exception, the copyright holders of this library give you
// permission to link this library with independent modules to produce an
// executable, regardless of the license terms of these independent modules, and
// to copy and distribute the resulting executable under terms of your choice,
// provided that you also meet, for each linked independent module, the terms
// and conditions of the license of that module. An independent module is a
// module which is not derived from or based on this library. If you modify this
// library, you may extend this exception to your version of the library, but
// you are not obligated to do so. If you do not wish to do so, delete this
// exception statement from your version.
//
// A copy of the GNU General Public License is provided in the file gpl.txt. You
// may also obtain a copy of the GNU General Public License on the World Wide
// Web at http://www.gnu.org/licenses/gpl.html.
//
//******************************************************************************
package edu.rit.io;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
* Class DataOutputStream provides an output stream that writes primitive data
* types and strings in binary form. It behaves similarly to class
* java.io.DataOutputStream, except the methods for writing types byte, short,
* char, int, long, and String are implemented differently. These methods write
* an integer value using a variable number of bytes, as described below. This
* can save space in the file if small integer values are written more
* frequently than large integer values. The resulting byte stream can be read
* using class {@linkplain DataInputStream}.
* <P>
* Note that class DataOutputStream does <I>not</I> implement interface
* java.io.DataOutput, because the methods do not obey the contract specified in
* that interface.
*
* @author Alan Kaminsky
* @version 18-Dec-2009
*/
public class DataOutputStream
extends FilterOutputStream
{
// Exported constructors.
/**
* Construct a new data output stream.
*
* @param out Underlying output stream.
*/
public DataOutputStream
(OutputStream out)
{
super (out);
}
// Exported operations.
/**
* Write the given Boolean value to this data output stream. One byte is
* written, either 0 (if <TT>v</TT> is false) or 1 (if <TT>v</TT> is true).
*
* @param v Boolean value.
*
* @exception IOException
* Thrown if an I/O error occurred.
*/
public void writeBoolean
(boolean v)
throws IOException
{
out.write (v ? 1 : 0);
}
/**
* Write the given integer value to this data output stream. This method can
* be used to write values of type byte, short, char, or int. From one to
* five bytes are written, in big-endian order, as follows:
* <UL>
* <P><LI>
* If −64 ≤ <TT>v</TT> ≤ 63, then one byte is written,
* containing 0 (1 bit) followed by <TT>v</TT> (7 bits).
* <P><LI>
* Else if −8192 ≤ <TT>v</TT> ≤ 8191, then two bytes are
* written, containing 10 (2 bits) followed by <TT>v</TT> (14 bits).
* <P><LI>
* Else if −1048576 ≤ <TT>v</TT> ≤ 1048575, then three bytes are
* written, containing 110 (3 bits) followed by <TT>v</TT> (21 bits).
* <P><LI>
* Else if −134217728 ≤ <TT>v</TT> ≤ 134217727, then four bytes
* are written, containing 1110 (4 bits) followed by <TT>v</TT> (28 bits).
* <P><LI>
* Else five bytes are written, containing 1111 (4 bits) followed by
* <TT>v</TT> (sign-extended to 36 bits).
* </UL>
*
* @param v Integer value.
*
* @exception IOException
* Thrown if an I/O error occurred.
*/
public void writeInt
(int v)
throws IOException
{
if (-64 <= v && v <= 63)
{
out.write (v & 0x7F);
}
else if (-8192 <= v && v <= 8191)
{
out.write (((v >> 8) & 0x3F) | 0x80);
out.write (v);
}
else if (-1048576 <= v && v <= 1048575)
{
out.write (((v >> 16) & 0x1F) | 0xC0);
out.write (v >> 8);
out.write (v);
}
else if (-134217728 <= v && v <= 134217727)
{
out.write (((v >> 24) & 0x0F) | 0xE0);
out.write (v >> 16);
out.write (v >> 8);
out.write (v);
}
else
{
out.write (((v >> 32) & 0x0F) | 0xF0);
out.write (v >> 24);
out.write (v >> 16);
out.write (v >> 8);
out.write (v);
}
}
/**
* Write the given unsigned integer value to this data output stream. This
* method can be used to write values of type byte, short, char, or int.
* From one to five bytes are written, in big-endian order, as follows:
* <UL>
* <P><LI>
* If 0 ≤ <TT>v</TT> ≤ 127, then one byte is written, containing 0 (1
* bit) followed by <TT>v</TT> (7 bits).
* <P><LI>
* Else if 128 ≤ <TT>v</TT> ≤ 16383, then two bytes are written,
* containing 10 (2 bits) followed by <TT>v</TT> (14 bits).
* <P><LI>
* Else if 16384 ≤ <TT>v</TT> ≤ 2097151, then three bytes are written,
* containing 110 (3 bits) followed by <TT>v</TT> (21 bits).
* <P><LI>
* Else if 2097152 ≤ <TT>v</TT> ≤ 268435455, then four bytes are
* written, containing 1110 (4 bits) followed by <TT>v</TT> (28 bits).
* <P><LI>
* Else five bytes are written, containing 1111 (4 bits) followed by
* <TT>v</TT> (zero-extended to 36 bits).
* </UL>
*
* @param v Integer value.
*
* @exception IOException
* Thrown if an I/O error occurred.
*/
public void writeUnsignedInt
(int v)
throws IOException
{
if (0 <= v && v <= 127)
{
out.write (v);
}
else if (128 <= v && v <= 16383)
{
out.write ((v >> 8) | 0x80);
out.write (v);
}
else if (16384 <= v && v <= 2097151)
{
out.write ((v >> 16) | 0xC0);
out.write (v >> 8);
out.write (v);
}
else if (2097152 <= v && v <= 268435455)
{
out.write ((v >> 24) | 0xE0);
out.write (v >> 16);
out.write (v >> 8);
out.write (v);
}
else
{
out.write (0xF0);
out.write (v >> 24);
out.write (v >> 16);
out.write (v >> 8);
out.write (v);
}
}
/**
* Write the given long value to this data output stream. From one to nine
* bytes are written, in big-endian order, as follows:
* <UL>
* <P><LI>
* If −64 ≤ <TT>v</TT> ≤ 63, then one byte is written,
* containing 0 (1 bit) followed by <TT>v</TT> (7 bits).
* <P><LI>
* Else if −8192 ≤ <TT>v</TT> ≤ 8191, then two bytes are
* written, containing 10 (2 bits) followed by <TT>v</TT> (14 bits).
* <P><LI>
* Else if −1048576 ≤ <TT>v</TT> ≤ 1048575, then three bytes are
* written, containing 110 (3 bits) followed by <TT>v</TT> (21 bits).
* <P><LI>
* Else if −134217728 ≤ <TT>v</TT> ≤ 134217727, then four bytes
* are written, containing 1110 (4 bits) followed by <TT>v</TT> (28 bits).
* <P><LI>
* Else if −17179869184 ≤ <TT>v</TT> ≤ 17179869183, then five
* bytes are written, containing 11110 (5 bits) followed by <TT>v</TT> (35
* bits).
* <P><LI>
* Else if −2199023255552 ≤ <TT>v</TT> ≤ 2199023255551, then six
* bytes are written, containing 111110 (6 bits) followed by <TT>v</TT> (42
* bits).
* <P><LI>
* Else if −281474976710656 ≤ <TT>v</TT> ≤ 281474976710655, then
* seven bytes are written, containing 1111110 (7 bits) followed by
* <TT>v</TT> (49 bits).
* <P><LI>
* Else if −36028797018963968 ≤ <TT>v</TT> ≤ 36028797018963967,
* then eight bytes are written, containing 11111110 (8 bits) followed by
* <TT>v</TT> (56 bits).
* <P><LI>
* Else nine bytes are written, containing 11111111 (8 bits) followed by
* <TT>v</TT> (64 bits).
* </UL>
*
* @param v Integer value.
*
* @exception IOException
* Thrown if an I/O error occurred.
*/
public void writeLong
(long v)
throws IOException
{
if (-64L <= v && v <= 63L)
{
out.write ((int)(v) & 0x7F);
}
else if (-8192L <= v && v <= 8191L)
{
out.write (((int)(v >> 8L) & 0x3F) | 0x80);
out.write ((int)(v));
}
else if (-1048576L <= v && v <= 1048575L)
{
out.write (((int)(v >> 16L) & 0x1F) | 0xC0);
out.write ((int)(v >> 8L));
out.write ((int)(v));
}
else if (-134217728L <= v && v <= 134217727L)
{
out.write (((int)(v >> 24L) & 0x0F) | 0xE0);
out.write ((int)(v >> 16L));
out.write ((int)(v >> 8L));
out.write ((int)(v));
}
else if (-17179869184L <= v && v <= 17179869183L)
{
out.write (((int)(v >> 32L) & 0x07) | 0xF0);
out.write ((int)(v >> 24L));
out.write ((int)(v >> 16L));
out.write ((int)(v >> 8L));
out.write ((int)(v));
}
else if (-2199023255552L <= v && v <= 2199023255551L)
{
out.write (((int)(v >> 40L) & 0x03) | 0xF8);
out.write ((int)(v >> 32L));
out.write ((int)(v >> 24L));
out.write ((int)(v >> 16L));
out.write ((int)(v >> 8L));
out.write ((int)(v));
}
else if (-281474976710656L <= v && v <= 281474976710656L)
{
out.write (((int)(v >> 48L) & 0x01) | 0xFC);
out.write ((int)(v >> 40L));
out.write ((int)(v >> 32L));
out.write ((int)(v >> 24L));
out.write ((int)(v >> 16L));
out.write ((int)(v >> 8L));
out.write ((int)(v));
}
else if (-36028797018963968L <= v && v <= 36028797018963968L)
{
out.write (0xFE);
out.write ((int)(v >> 48L));
out.write ((int)(v >> 40L));
out.write ((int)(v >> 32L));
out.write ((int)(v >> 24L));
out.write ((int)(v >> 16L));
out.write ((int)(v >> 8L));
out.write ((int)(v));
}
else
{
out.write (0xFF);
out.write ((int)(v >> 56L));
out.write ((int)(v >> 48L));
out.write ((int)(v >> 40L));
out.write ((int)(v >> 32L));
out.write ((int)(v >> 24L));
out.write ((int)(v >> 16L));
out.write ((int)(v >> 8L));
out.write ((int)(v));
}
}
/**
* Write the given unsigned long value to this data output stream. From one
* to nine bytes are written, in big-endian order, as follows:
* <UL>
* <P><LI>
* If 0 ≤ <TT>v</TT> ≤ 127, then one byte is written, containing 0 (1
* bit) followed by <TT>v</TT> (7 bits).
* <P><LI>
* Else if 128 ≤ <TT>v</TT> ≤ 16383, then two bytes are written,
* containing 10 (2 bits) followed by <TT>v</TT> (14 bits).
* <P><LI>
* Else if 16384 ≤ <TT>v</TT> ≤ 2097151, then three bytes are written,
* containing 110 (3 bits) followed by <TT>v</TT> (21 bits).
* <P><LI>
* Else if 2097152 ≤ <TT>v</TT> ≤ 268435455, then four bytes are
* written, containing 1110 (4 bits) followed by <TT>v</TT> (28 bits).
* <P><LI>
* Else if 268435456 ≤ <TT>v</TT> ≤ 34359738367, then five bytes are
* written, containing 11110 (5 bits) followed by <TT>v</TT> (35 bits).
* <P><LI>
* Else if 34359738368 ≤ <TT>v</TT> ≤ 4398046511103, then six bytes
* are written, containing 111110 (6 bits) followed by <TT>v</TT> (42 bits).
* <P><LI>
* Else if 4398046511104 ≤ <TT>v</TT> ≤ 562949953421311, then seven
* bytes are written, containing 1111110 (7 bits) followed by <TT>v</TT> (49
* bits).
* <P><LI>
* Else if 562949953421312 ≤ <TT>v</TT> ≤ 72057594037927935, then
* eight bytes are written, containing 11111110 (8 bits) followed by
* <TT>v</TT> (56 bits).
* <P><LI>
* Else nine bytes are written, containing 11111111 (8 bits) followed by
* <TT>v</TT> (64 bits).
* </UL>
*
* @param v Integer value.
*
* @exception IOException
* Thrown if an I/O error occurred.
*/
public void writeUnsignedLong
(long v)
throws IOException
{
if (0L <= v && v <= 127L)
{
out.write ((int)(v));
}
else if (128L <= v && v <= 16383L)
{
out.write ((int)(v >> 8L) | 0x80);
out.write ((int)(v));
}
else if (16384L <= v && v <= 2097151L)
{
out.write ((int)(v >> 16L) | 0xC0);
out.write ((int)(v >> 8L));
out.write ((int)(v));
}
else if (2097152L <= v && v <= 268435455L)
{
out.write ((int)(v >> 24L) | 0xE0);
out.write ((int)(v >> 16L));
out.write ((int)(v >> 8L));
out.write ((int)(v));
}
else if (268435456L <= v && v <= 34359738367L)
{
out.write ((int)(v >> 32L) | 0xF0);
out.write ((int)(v >> 24L));
out.write ((int)(v >> 16L));
out.write ((int)(v >> 8L));
out.write ((int)(v));
}
else if (34359738368L <= v && v <= 4398046511103L)
{
out.write ((int)(v >> 40L) | 0xF8);
out.write ((int)(v >> 32L));
out.write ((int)(v >> 24L));
out.write ((int)(v >> 16L));
out.write ((int)(v >> 8L));
out.write ((int)(v));
}
else if (4398046511104L <= v && v <= 562949953421311L)
{
out.write ((int)(v >> 48L) | 0xFC);
out.write ((int)(v >> 40L));
out.write ((int)(v >> 32L));
out.write ((int)(v >> 24L));
out.write ((int)(v >> 16L));
out.write ((int)(v >> 8L));
out.write ((int)(v));
}
else if (562949953421312L <= v && v <= 72057594037927935L)
{
out.write (0xFE);
out.write ((int)(v >> 48L));
out.write ((int)(v >> 40L));
out.write ((int)(v >> 32L));
out.write ((int)(v >> 24L));
out.write ((int)(v >> 16L));
out.write ((int)(v >> 8L));
out.write ((int)(v));
}
else
{
out.write (0xFF);
out.write ((int)(v >> 56L));
out.write ((int)(v >> 48L));
out.write ((int)(v >> 40L));
out.write ((int)(v >> 32L));
out.write ((int)(v >> 24L));
out.write ((int)(v >> 16L));
out.write ((int)(v >> 8L));
out.write ((int)(v));
}
}
/**
* Write the given float value to this data output stream. Four bytes are
* written in big-endian order containing
* <TT>Float.floatToRawIntBits(v)</TT>.
*
* @param v Float value.
*
* @exception IOException
* Thrown if an I/O error occurred.
*/
public void writeFloat
(float v)
throws IOException
{
int x = Float.floatToRawIntBits (v);
out.write (x >> 24);
out.write (x >> 16);
out.write (x >> 8);
out.write (x);
}
/**
* Write the given double value to this data output stream. Eight bytes are
* written in big-endian order containing
* <TT>Double.doubleToRawLongBits(v)</TT>.
*
* @param v Double value.
*
* @exception IOException
* Thrown if an I/O error occurred.
*/
public void writeDouble
(double v)
throws IOException
{
long x = Double.doubleToRawLongBits (v);
out.write ((int)(x >> 56L));
out.write ((int)(x >> 48L));
out.write ((int)(x >> 40L));
out.write ((int)(x >> 32L));
out.write ((int)(x >> 24L));
out.write ((int)(x >> 16L));
out.write ((int)(x >> 8L));
out.write ((int)(x));
}
/**
* Write the given string value to this data output stream. The length of
* the string is written using <TT>writeUnsignedInt()</TT>, then each
* character of the string is written using <TT>writeUnsignedInt()</TT>.
*
* @param v String value.
*
* @exception IOException
* Thrown if an I/O error occurred.
*/
public void writeString
(String v)
throws IOException
{
int n = v.length();
writeUnsignedInt (n);
for (int i = 0; i < n; ++ i)
{
writeUnsignedInt (v.charAt (i));
}
}
}
|