Java                 package:R.utils                 R Documentation

_S_t_a_t_i_c _c_l_a_s_s _f_o_r _J_a_v_a _r_e_l_a_t_e_d _m_e_t_h_o_d_s

_D_e_s_c_r_i_p_t_i_o_n:

     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'.

_U_s_a_g_e:

     Java()

_F_i_e_l_d_s _a_n_d _M_e_t_h_o_d_s:

     *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, clone, detach, equals, extend, finalize, gc,
     getEnvironment, getFields, getInstanciationTime,
     getStaticInstance, hasField, hashCode, ll, load, objectSize,
     print, save

_A_u_t_h_o_r(_s):

     Henrik Bengtsson (<URL: http://www.braju.com/R/>)

_E_x_a_m_p_l_e_s:

     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 <- "This R string was written (using the UTF-8 format) using\nthe static methods of the Java class in the R.io package."
     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)

