示例#1
0
 public void setInputLineSeparator(ThreadContext tc, String sep) {
   try {
     linesep = enc.charset().newEncoder().encode(CharBuffer.wrap(sep)).array();
   } catch (IOException e) {
     throw ExceptionHandling.dieInternal(tc, e);
   }
 }
示例#2
0
 public void close(ThreadContext tc) {
   try {
     is.close();
   } catch (IOException e) {
     throw ExceptionHandling.dieInternal(tc, e);
   }
 }
示例#3
0
  public synchronized String slurp(ThreadContext tc) {
    try {
      // Read in file.
      ArrayList<ByteBuffer> buffers = new ArrayList<ByteBuffer>();
      ByteBuffer curBuffer = ByteBuffer.allocate(32768);
      int total = 0;
      int read;
      if (readBuffer != null) {
        total = readBuffer.limit() - readBuffer.position();
        buffers.add(ByteBuffer.wrap(readBuffer.array(), readBuffer.position(), total));
        readBuffer = null;
      }
      while ((read = chan.read(curBuffer)) != -1) {
        curBuffer.flip();
        buffers.add(curBuffer);
        curBuffer = ByteBuffer.allocate(32768);
        total += read;
      }
      eof = true;

      return decodeBuffers(buffers, total);
    } catch (IOException e) {
      throw ExceptionHandling.dieInternal(tc, e);
    }
  }
示例#4
0
 public void print(ThreadContext tc, String s) {
   try {
     ByteBuffer buffer = enc.encode(CharBuffer.wrap(s));
     write(tc, buffer);
   } catch (IOException e) {
     throw ExceptionHandling.dieInternal(tc, e);
   }
 }
示例#5
0
 protected void write(ThreadContext tc, ByteBuffer buffer) {
   try {
     int toWrite = buffer.limit();
     int written = 0;
     while (written < toWrite) {
       written += chan.write(buffer);
     }
   } catch (IOException e) {
     throw ExceptionHandling.dieInternal(tc, e);
   }
 }
示例#6
0
  public synchronized String readline(ThreadContext tc) {
    try {
      boolean foundLine = false;
      ArrayList<ByteBuffer> lineChunks = new ArrayList<ByteBuffer>();
      int total = 0;

      while (!foundLine) {
        /* Ensure we have a buffer available. */
        if (readBuffer == null) {
          readBuffer = ByteBuffer.allocate(32768);
          if (chan.read(readBuffer) == -1) {
            /* End of file, so what we have is fine. */
            eof = true;
            foundLine = true;
            readBuffer.flip();
            break;
          }
          readBuffer.flip();
        }

        /* Look for a line end. */
        int start = readBuffer.position();
        int end = start;
        while (!foundLine && end < readBuffer.limit()) {
          int index = 0;
          while (index < linesep.length
              && end + index < readBuffer.limit()
              && readBuffer.get(end + index) == linesep[index]) index++;

          if (index == linesep.length) {
            end += index;
            foundLine = true;
          } else {
            end++;
          }
        }

        /* Copy what we found into the results. */
        byte[] lineBytes = new byte[end - start];
        readBuffer.get(lineBytes);
        lineChunks.add(ByteBuffer.wrap(lineBytes));
        total += lineBytes.length;

        /* If we didn't find a line, will cross chunk boundary. */
        if (!foundLine) readBuffer = null;
      }

      if (lineChunks.size() == 1) return dec.decode(lineChunks.get(0)).toString();
      else return decodeBuffers(lineChunks, total);
    } catch (IOException e) {
      throw ExceptionHandling.dieInternal(tc, e);
    }
  }
示例#7
0
 public byte[] read(ThreadContext tc, int bytes) {
   try {
     ByteBuffer buffer = ByteBuffer.allocate(bytes);
     chan.read(buffer);
     buffer.flip();
     byte[] res = new byte[buffer.limit()];
     buffer.get(res);
     return res;
   } catch (IOException e) {
     throw ExceptionHandling.dieInternal(tc, e);
   }
 }
示例#8
0
 public synchronized String readlineInteractive(ThreadContext tc, String prompt) {
   try {
     if (cr == null) cr = new ConsoleReader(is, new OutputStreamWriter(tc.gc.out));
     String line = cr.readLine(prompt);
     if (line == null) {
       eof = true;
       line = "";
     }
     return line;
   } catch (IOException e) {
     throw ExceptionHandling.dieInternal(tc, e);
   }
 }
示例#9
0
 public synchronized String readline(ThreadContext tc) {
   try {
     if (br == null) br = new BufferedReader(new InputStreamReader(is, cs));
     String line = br.readLine();
     if (line == null) {
       eof = true;
       line = "";
     }
     return line;
   } catch (IOException e) {
     throw ExceptionHandling.dieInternal(tc, e);
   }
 }
示例#10
0
 public synchronized String slurp(ThreadContext tc) {
   try {
     if (br == null) br = new BufferedReader(new InputStreamReader(is, cs));
     StringBuffer data = new StringBuffer();
     char[] buf = new char[4096];
     int read = 0;
     while ((read = br.read(buf)) != -1) data.append(String.valueOf(buf, 0, read));
     eof = true;
     return data.toString();
   } catch (IOException e) {
     throw ExceptionHandling.dieInternal(tc, e);
   }
 }
示例#11
0
 public synchronized String getc(ThreadContext tc) {
   try {
     if (br == null) br = new BufferedReader(new InputStreamReader(is, cs));
     int read = br.read();
     if (read == -1) {
       eof = true;
       return "";
     } else {
       return String.valueOf(read);
     }
   } catch (IOException e) {
     throw ExceptionHandling.dieInternal(tc, e);
   }
 }
示例#12
0
 public byte[] read(ThreadContext tc, int bytes) {
   try {
     byte[] array = new byte[bytes];
     int read = 0;
     int offset = 0;
     while ((read = is.read(array, offset, bytes - offset)) != -1) {
       offset += read;
     }
     byte[] compact = new byte[offset];
     System.arraycopy(array, 0, compact, 0, offset);
     return compact;
   } catch (IOException e) {
     throw ExceptionHandling.dieInternal(tc, e);
   }
 }
示例#13
0
  /* TODO - think about unicode in readchars and getc */
  public synchronized String readchars(ThreadContext tc, int count) {
    try {
      if (br == null) br = new BufferedReader(new InputStreamReader(is, cs));
      char[] chars = new char[count];

      int actuallyRead = br.read(chars, 0, count);

      if (actuallyRead == -1) {
        return "";
      } else {
        return new String(chars, 0, actuallyRead);
      }
    } catch (IOException e) {
      throw ExceptionHandling.dieInternal(tc, e);
    }
  }
示例#14
0
  public synchronized String getc(ThreadContext tc) {
    try {
      int maxBytes = (int) enc.maxBytesPerChar();
      ByteBuffer toDecode = ByteBuffer.allocate(maxBytes);
      CharBuffer decoded = CharBuffer.allocate(1);
      for (int i = 0; i < maxBytes; i++) {
        /* Ensure we have a read buffer available. */
        if (readBuffer == null) {
          readBuffer = ByteBuffer.allocate(32768);
          if (chan.read(readBuffer) == -1) {
            /* End of file. */
            eof = true;
            if (i == 0) {
              /* Fine, just no char. */
              return "";
            } else {
              /* Malformed; following will likely throw. */
              toDecode.position(0);
              dec.decode(toDecode, decoded, true).throwException();
              return decoded.toString();
            }
          }
          readBuffer.flip();
        }

        /* Get a character from the read buffer. */
        toDecode.position(i);
        toDecode.put(readBuffer.get());
        if (readBuffer.remaining() == 0) readBuffer = null;

        /* Try to decode; if we fail, try another byte. */
        toDecode.position(0);
        CoderResult cr = dec.decode(toDecode, decoded, false);
        if (!cr.isError()) {
          decoded.rewind();
          return decoded.toString();
        }
      }
      throw new MalformedInputException(maxBytes);
    } catch (IOException e) {
      throw ExceptionHandling.dieInternal(tc, e);
    }
  }
示例#15
0
 public void bind_attribute_native(
     ThreadContext tc, SixModelObject class_handle, String name, long hint) {
   CStructREPRData data = (CStructREPRData) class_handle.st.REPRData;
   AttrInfo info = data.fieldTypes.get(name);
   Object value;
   switch (info.argType) {
     case CHAR:
       tc.native_type = ThreadContext.NATIVE_INT;
       value = new Byte((byte) tc.native_i);
       break;
     case SHORT:
       tc.native_type = ThreadContext.NATIVE_INT;
       value = new Short((short) tc.native_i);
       break;
     case INT:
       tc.native_type = ThreadContext.NATIVE_INT;
       value = new Integer((int) tc.native_i);
       break;
     case LONG:
       tc.native_type = ThreadContext.NATIVE_INT;
       value = new Long((long) tc.native_i);
       break;
     case FLOAT:
       tc.native_type = ThreadContext.NATIVE_NUM;
       value = new Float((float) tc.native_n);
       break;
     case DOUBLE:
       tc.native_type = ThreadContext.NATIVE_NUM;
       value = new Double((double) tc.native_n);
       break;
     default:
       ExceptionHandling.dieInternal(
           tc, String.format("CStruct.bind_attribute_native: Can't handle %s", info.argType));
       value = null;
   }
   storage.writeField(name, value);
 }
示例#16
0
  public void get_attribute_native(
      ThreadContext tc, SixModelObject class_handle, String name, long hint) {
    CStructREPRData data = (CStructREPRData) class_handle.st.REPRData;
    AttrInfo info = data.fieldTypes.get(name);

    Object o = storage.readField(name);
    switch (info.argType) {
      case CHAR:
        tc.native_type = ThreadContext.NATIVE_INT;
        tc.native_i = ((Byte) o).byteValue();
        break;
      case SHORT:
        tc.native_type = ThreadContext.NATIVE_INT;
        tc.native_i = ((Short) o).shortValue();
        break;
      case INT:
        tc.native_type = ThreadContext.NATIVE_INT;
        tc.native_i = ((Integer) o).intValue();
        break;
      case LONG:
        tc.native_type = ThreadContext.NATIVE_INT;
        tc.native_i = ((Long) o).longValue();
        break;
      case FLOAT:
        tc.native_type = ThreadContext.NATIVE_NUM;
        tc.native_n = ((Float) o).floatValue();
        break;
      case DOUBLE:
        tc.native_type = ThreadContext.NATIVE_NUM;
        tc.native_n = ((Double) o).doubleValue();
        break;
      default:
        ExceptionHandling.dieInternal(
            tc, String.format("CStruct.get_attribute_native: Can't handle %s", info.argType));
    }
  }