Christophe Weblog Wiki Code Publications Music
fix multibyte decoding protocol bug
[swankr.git] / swank.R
diff --git a/swank.R b/swank.R
index 57ead0ac9f803a1d2729f737d739c0d4a10fcb69..901f1c16a5bc7f1af0015a73b7bd352c4adf36b3 100644 (file)
--- a/swank.R
+++ b/swank.R
@@ -141,19 +141,27 @@ readPacket <- function(io) {
   header <- readChunk(io, 6)
   len <- strtoi(header, base=16)
   payload <- readChunk(io, len)
-  readSexpFromString(payload)
+  sexp <- readSexpFromString(payload)
+  sexp
 }
 
 readChunk <- function(io, len) {
-  buffer <- readChar(io, len)
+  buffer <- readChar(io, len, useBytes=TRUE)
   if(length(buffer) == 0) {
     condition <- simpleCondition("End of file on io")
     class(condition) <- c("endOfFile", class(condition))
     signalCondition(condition)
   }
-  if(nchar(buffer) != len) {
-    stop("short read in readChunk")
-  }
+  ## FIXME: with the useBytes argument to readChar, it is normal for
+  ## the buffer returned to be fewer character than bytes were read,
+  ## given the possibility of multibyte characters.  However, that
+  ## means we can’t detect at all the case where there is actually a
+  ## short read (though empirically the readChar call blocks rather
+  ## than returning early).
+  ##
+  ## if(nchar(buffer) != len) {
+  ##   stop("short read in readChunk")
+  ## }
   buffer
 }