Package java.nio

Examples of java.nio.CharBuffer


    }
      return sb.toString();
    }

  private static String escapeChar(char chr) {
    CharBuffer cb = CharBuffer.allocate(7);
    cb.append("_u")//$NON-NLS-1$
    CharsetUtils.toHex(cb, (byte)(chr >> 8));
    CharsetUtils.toHex(cb, (byte)chr);
    return cb.append("_").flip().toString()//$NON-NLS-1$
  }
View Full Code Here


        return new FileOutputStream(file);
    }

    @Override
    public CharBuffer getCharContent(boolean ignoreEncodingErrors) throws IOException {
        CharBuffer cb = fileManager.getCachedContent(this);
        if (cb == null) {
            InputStream in = new FileInputStream(file);
            try {
                ByteBuffer bb = fileManager.makeByteBuffer(in);
                JavaFileObject prev = fileManager.log.useSource(this);
View Full Code Here

        compiler = JavaCompiler.instance(context);
        JavaFileObject prev = compiler.log.useSource(null);
        ParserFactory parserFactory = ParserFactory.instance(context);
        Attr attr = Attr.instance(context);
        try {
            CharBuffer buf = CharBuffer.wrap((expr+"\u0000").toCharArray(), 0, expr.length());
            Parser parser = parserFactory.newParser(buf, false, false, false);
            JCTree tree = parser.parseType();
            return attr.attribType(tree, (Symbol.TypeSymbol)scope);
        } finally {
            compiler.log.useSource(prev);
View Full Code Here

  // convert byte array to char array, assuming UTF-8 encoding

  static protected char[] convertByteToCharUTF(byte[] byteArray) {
    Charset c = Charset.forName("UTF-8");
    CharBuffer output = c.decode(ByteBuffer.wrap(byteArray));
    return output.array();
  }
View Full Code Here

          long offset = 0;
          FileChannel chnnl = getChannel();

          // read mbox file to determine the message positions..
          ByteBuffer buffer = chnnl.map(FileChannel.MapMode.READ_ONLY, 0l, size);
          CharBuffer cb = decoder.decode(buffer);

          // check that first message is correct..
          if (Pattern.compile(INITIAL_FROM__PATTERN, Pattern.DOTALL).matcher(cb).matches()) {
            // debugging..
            log.debug("Matched first message...");
View Full Code Here

      log.debug("MboxFile.getMessageAsStream("+String.valueOf(begin)+","+String.valueOf(size)+")");

      // Skip From line
      ByteBuffer byFrom = getChannel().map(FileChannel.MapMode.READ_ONLY, begin, 128);
      CharBuffer chFrom = decoder.decode(byFrom);

      int start = 0;
      // Ignore any white spaces and line feed
      char c = chFrom.charAt(start);
      while (c==' ' || c=='\r' || c=='\n' || c=='\t') c = chFrom.charAt(++start);
      // If first line does not start with message preffx then raise an exception
      if (!chFrom.subSequence(start, start+FROM__PREFIX.length()).toString().equals(FROM__PREFIX))
        throw new IOException ("MboxFile.getMessageAsStream() starting position " + String.valueOf(start) + " \""+chFrom.subSequence(start, start+FROM__PREFIX.length()).toString()+"\" does not match a begin message token \"" + FROM__PREFIX + "\"");
      // Skip the From line
      while (chFrom.charAt(start++)!=(char) 10) ;

      log.debug("  skip = " + String.valueOf(start));
      log.debug("  start = " + String.valueOf(begin+start));

      MappedByteBuffer byBuffer = getChannel().map(FileChannel.MapMode.READ_ONLY, begin+start, size);
View Full Code Here

      throws IOException {
      log.debug("MboxFile.getPartAsStream("+String.valueOf(begin)+","+String.valueOf(offset)+","+String.valueOf(size)+")");

      // Skip From line
      ByteBuffer byFrom = getChannel().map(FileChannel.MapMode.READ_ONLY, begin, 128);
      CharBuffer chFrom = decoder.decode(byFrom);

      log.debug("from line decoded");

      int start = 0;
      // Ignore any white spaces and line feed
      char c = chFrom.charAt(start);
      while (c==' ' || c=='\r' || c=='\n' || c=='\t') c = chFrom.charAt(++start);
      // If first line does not start with message preffx then raise an exception
      log.debug("first line is " + chFrom.subSequence(start, start+FROM__PREFIX.length()).toString());
      if (!chFrom.subSequence(start, start+FROM__PREFIX.length()).toString().equals(FROM__PREFIX))
        throw new IOException ("MboxFile.getPartAsStream() starting position " + String.valueOf(start) + " \""+chFrom.subSequence(start, start+FROM__PREFIX.length()).toString()+"\" does not match a begin message token \"" + FROM__PREFIX + "\"");
      // Skip the From line
      while (chFrom.charAt(start++)!=(char) 10) ;

      start += offset;

      log.debug("  skip = " + String.valueOf(start));
      log.debug("  start = " + String.valueOf(start));
View Full Code Here

          size = (int) (getChannel().size() - position);
      }

      // Skip From line
      ByteBuffer byFrom = getChannel().map(FileChannel.MapMode.READ_ONLY, position, 256);
      CharBuffer chFrom = decoder.decode(byFrom);

      int start = 0;
      // Ignore any white spaces and line feed
      char c = chFrom.charAt(start);
      while (c==' ' || c=='\r' || c=='\n' || c=='\t') c = chFrom.charAt(++start);
      // If first line does not start with message preffx then raise an exception
      if (!chFrom.subSequence(start, start+FROM__PREFIX.length()).toString().equals(FROM__PREFIX))
        throw new IOException ("MboxFile.getMessageAsStream() starting position " + String.valueOf(start) + " \""+chFrom.subSequence(start, start+FROM__PREFIX.length()).toString()+"\" does not match a begin message token \"" + FROM__PREFIX + "\"");
      // Skip the From line
      while (chFrom.charAt(start++)!=(char) 10) ;

      log.debug("  skip = " + String.valueOf(start));
      log.debug("  start = " + String.valueOf(position+start));

      MappedByteBuffer byBuffer = getChannel().map(FileChannel.MapMode.READ_ONLY, position+start, size-start);
View Full Code Here

      throw new EOFException("Attempted to read " + stringLength + " bytes but no more than "
          + encodedString.length + " were available");
    }

    ByteBuffer byteBuf = ByteBuffer.wrap(encodedString);
    CharBuffer charBuf = charsetDecoder.decode(byteBuf);

    return charBuf.toString();
  }
View Full Code Here

        // slightly overestimate the buffer size to avoid reallocation.
        float factor =
            decoder.averageCharsPerByte() * 0.8f +
            decoder.maxCharsPerByte() * 0.2f;
        CharBuffer dest = CharBuffer.
            allocate(10 + (int)(inbuf.remaining()*factor));

        while (true) {
            CoderResult result = decoder.decode(inbuf, dest, true);
            dest.flip();

            if (result.isUnderflow()) { // done reading
                // make sure there is at least one extra character
                if (dest.limit() == dest.capacity()) {
                    dest = CharBuffer.allocate(dest.capacity()+1).put(dest);
                    dest.flip();
                }
                return dest;
            } else if (result.isOverflow()) { // buffer too small; expand
                int newCapacity =
                    10 + dest.capacity() +
                    (int)(inbuf.remaining()*decoder.maxCharsPerByte());
                dest = CharBuffer.allocate(newCapacity).put(dest);
            } else if (result.isMalformed() || result.isUnmappable()) {
                // bad character in input

                // report coding error (warn only pre 1.5)
                if (!getSource().allowEncodingErrors()) {
                    log.error(new SimpleDiagnosticPosition(dest.limit()),
                              "illegal.char.for.encoding",
                              charset == null ? encodingName : charset.name());
                } else {
                    log.warning(new SimpleDiagnosticPosition(dest.limit()),
                                "illegal.char.for.encoding",
                                charset == null ? encodingName : charset.name());
                }

                // skip past the coding error
                inbuf.position(inbuf.position() + result.length());

                // undo the flip() to prepare the output buffer
                // for more translation
                dest.position(dest.limit());
                dest.limit(dest.capacity());
                dest.put((char)0xfffd); // backward compatible
            } else {
                throw new AssertionError(result);
            }
        }
        // unreached
View Full Code Here

TOP

Related Classes of java.nio.CharBuffer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.