Java {R.utils}R Documentation

Static class for Java related methods

Description

Package: R.utils
Class Java

Object
~~|
~~+--Java

Directly known subclasses:

public static class Java
extends Object

Static class that provides methods for reading and writing Java data types. Currently the following data types are supported: byte, short and int. R character strings can be written as UTF-8 formatted strings, which can be read by Java. Currently on Java String's that contain ASCII characters can be imported into R. The reason for this is that other characters are translated into non-eight bits data, e.g. 16- and 24-bits, which the readChar() method currently does not support.

Furthermore, the Java class defines some static constants describing the minimum and maximum value of some of the common Java data types: BYTE.MIN, BYTE.MAX SHORT.MIN, SHORT.MAX INT.MIN, INT.MAX LONG.MIN, and LONG.MAX.

Usage

Java()

Fields and Methods

Methods:

asByte Converts a numeric to a Java byte.
asInt Converts an numeric to a Java integer.
asLong Converts a numeric to a Java long.
asShort Converts a numeric to a Java short.
readByte Reads a Java formatted byte (8 bits) from a connection.
readInt Reads a Java formatted int (32 bits) from a connection.
readShort Reads a Java formatted short (16 bits) from a connection.
readUTF Reads a Java (UTF-8) formatted string from a connection.
writeByte Writes a byte (8 bits) to a connection in Java format.
writeInt Writes a integer (32 bits) to a connection in Java format.
writeShort Writes a short (16 bits) to a connection in Java format.
writeUTF Writes a string to a connection in Java format (UTF-8).

Methods inherited from Object:
$, $<-, [[, [[<-, as.character, attach, attachLocally, clearCache, clearLookupCache, clone, detach, equals, extend, finalize, getEnvironment, getFieldModifier, getFieldModifiers, getFields, getInstantiationTime, getStaticInstance, hasField, hashCode, ll, load, names, objectSize, print, save

Author(s)

Henrik Bengtsson

Examples

pathname <- tempfile()

# Open the temporary file for writing
out <- file(pathname, open="wb")
b <- -128:127
Java$writeByte(out, b)
s <- -32768:32767
Java$writeShort(out, s)
i <- c(-2147483648, -2147483647, -1, 0, +1, 2147483646, 2147483647);
Java$writeInt(out, i)
str <- c("This R string was written (using the UTF-8 format) using",
         "the static methods of the Java class in the R.io package.")
str <- paste(str, collapse="\n")
Java$writeUTF(out, str)
close(out)

# Open the temporary file for reading
inn <- file(pathname, open="rb")

bfr <- Java$readByte(inn, n=length(b))
cat("Read ", length(bfr), " bytes.\n", sep="")
if (!identical(bfr, b))
  throw("Failed to read the same data that was written.")

bfr <- Java$readShort(inn, n=length(s))
cat("Read ", length(bfr), " shorts.\n", sep="")
if (!identical(bfr, s))
  throw("Failed to read the same data that was written.")

bfr <- Java$readInt(inn, n=length(i))
cat("Read ", length(bfr), " ints.\n", sep="")
if (!identical(bfr, i))
  throw("Failed to read the same data that was written.")

bfr <- Java$readUTF(inn)
cat("Read ", nchar(bfr), " UTF characters:\n", "'", bfr, "'\n", sep="")

close(inn)

file.remove(pathname)

[Package R.utils version 2.11.0 Index]