Package java.nio

Examples of java.nio.CharBuffer


    final BufferedReader reader = bufferedReader();
    return new CloseOperation<HttpRequest>(reader, ignoreCloseExceptions) {

      @Override
      public HttpRequest run() throws IOException {
        final CharBuffer buffer = CharBuffer.allocate(bufferSize);
        int read;
        while ((read = reader.read(buffer)) != -1) {
          buffer.rewind();
          appendable.append(buffer, 0, read);
          buffer.rewind();
        }
        return HttpRequest.this;
      }
    }.call();
  }
View Full Code Here


    if (len == 0)
      return null;

    ce.reset();
    ByteBuffer bb = ByteBuffer.wrap(ba);
    CharBuffer cb = CharBuffer.wrap(src, off, len);
    try {
      CoderResult cr = ce.encode(cb, bb, true);
      if (!cr.isUnderflow())
        cr.throwException();
      cr = ce.flush(bb);
View Full Code Here

        @Override
        public  int encode(String source) {
            wrapper.clear();
            encoder.reset();

            CharBuffer in = CharBuffer.wrap(source);
            CoderResult result = encoder.encode(in, wrapper, true);
            assert result == CoderResult.UNDERFLOW;
            result = encoder.flush(wrapper);
            assert result == CoderResult.UNDERFLOW;
View Full Code Here

        int idx = 0;

        int length = s.length();
        CharsetEncoder encoder = null;
        ByteBuffer byteBuffer = null;
        CharBuffer buffer = null;

        for (; idx < length; idx++) {
            char c = s.charAt(idx);

            if (!isLegalURIQueryChar(c)) {
                builder.append(s.substring(start, idx));

                if (encoder == null) {
                    encoder = Charset.forName("UTF-8").newEncoder();
                }
                if (buffer == null) {
                    buffer = CharBuffer.allocate(1);
                    byteBuffer = ByteBuffer.allocate(6); // max bytes size in UTF-8
                } else {
                    byteBuffer.limit(6);
                }

                buffer.put(0, c);

                buffer.rewind();
                byteBuffer.rewind();
                encoder.encode(buffer, byteBuffer, true);

                byteBuffer.flip();
View Full Code Here

      CharsetDecoder decoder = Utf8.CHARSET.newDecoder();
      decoder.onMalformedInput(CodingErrorAction.REPORT);
      decoder.onUnmappableCharacter(CodingErrorAction.REPORT);

      ByteBuffer byteBuffer = ByteBuffer.wrap(data);
      CharBuffer charBuffer = decoder.decode(byteBuffer);
      return charBuffer.toString();
    } catch (Exception e) {
      log.debug("Cannot decode as string", e);
      return null;
    }
  }
View Full Code Here

      CharsetDecoder decoder = Charsets.UTF_8.newDecoder();
      decoder.onMalformedInput(CodingErrorAction.REPORT);
      decoder.onUnmappableCharacter(CodingErrorAction.REPORT);

      ByteBuffer byteBuffer = ByteBuffer.wrap(data);
      CharBuffer charBuffer = decoder.decode(byteBuffer);
      return charBuffer.toString();
    } catch (Exception e) {
      log.debug("Cannot decode as string", e);
      return null;
    }
  }
View Full Code Here

        CharsetEncoder enc = this.charset.newEncoder();

        enc.onMalformedInput(CodingErrorAction.REPORT);
        enc.onUnmappableCharacter(CodingErrorAction.REPORT);

        CharBuffer cb = CharBuffer.wrap(name);
        ByteBuffer out = ByteBuffer.allocate(name.length()
                                             + (name.length() + 1) / 2);

        while (cb.remaining() > 0) {
            CoderResult res = enc.encode(cb, out,true);

            if (res.isUnmappable() || res.isMalformed()) {

                // write the unmappable characters in utf-16
                // pseudo-URL encoding style to ByteBuffer.
                if (res.length() * 6 > out.remaining()) {
                    out = ZipEncodingHelper.growBuffer(out, out.position()
                                                       + res.length() * 6);
                }

                for (int i=0; i<res.length(); ++i) {
                    ZipEncodingHelper.appendSurrogate(out,cb.get());
                }

            } else if (res.isOverflow()) {

                out = ZipEncodingHelper.growBuffer(out, 0);
View Full Code Here

     * If OperatingSystem.Unknown is specified this function will generate a filename which fullfils the restrictions of all known OS, currently
     * this is MacOS, Unix and Windows.
     */
  public static String sanitizeFileName(final String fileName, OperatingSystem targetOS, String extraChars) {
    // Filter out any characters which do not exist in the charset.
    final CharBuffer buffer = fileNameCharset.decode(fileNameCharset.encode(fileName)); // Charset are thread-safe

    final StringBuilder sb = new StringBuilder(fileName.length() + 1);

    switch(targetOS) {
      case Unknown: break;
      case MacOS: break;
      case Linux: break;
      case FreeBSD: break;
      case GenericUnix: break;
      case Windows: break;
      default:
        Logger.error(FileUtil.class, "Unsupported operating system: " + targetOS);
        targetOS = OperatingSystem.Unknown;
        break;
    }
   
    char def = ' ';
    if(extraChars.indexOf(' ') != -1) {
      def = '_';
      if(extraChars.indexOf(def) != -1) {
        def = '-';
        if(extraChars.indexOf(def) != -1)
          throw new IllegalArgumentException("What do you want me to use instead of spaces???");
      }
    }

    for(char c : buffer.array()) { // Note that this will add extra whitespace to the end, which we will trim later.
     
      if(extraChars.indexOf(c) != -1) {
        sb.append(def);
        continue;
      }
View Full Code Here

        // TODO: Make sure that the entire world settles on one encoding and
        // that every file in existance is re-encoded.
        final Charset cs = Charset.forName("UTF-8");
        while (fc.read(buf) != -1) {
            buf.rewind();
            final CharBuffer chbuf = cs.decode(buf);
            sb.append(chbuf.array());
            buf.clear();
        }

        in.close();
        return sb.toString();
View Full Code Here

    private static String getBinaryValueString(final OtpErlangBinary b) {
        final StringBuilder sb = new StringBuilder("<<");
        if (b.size() > 0) {
            final byte[] bytes = b.binaryValue();
            CharBuffer cb = null;
            if (looksLikeAscii(bytes)) {
                final Charset[] css = { Charsets.UTF_8, Charsets.ISO_8859_1 };
                final Charset[] tryCharsets = css;
                for (final Charset cset : tryCharsets) {
                    final CharsetDecoder cd = cset.newDecoder();
                    cd.onMalformedInput(CodingErrorAction.REPORT);
                    cd.onUnmappableCharacter(CodingErrorAction.REPORT);
                    try {
                        cb = cd.decode(ByteBuffer.wrap(bytes));
                        break;
                    } catch (final CharacterCodingException e) {
                    }
                }
            }
            if (cb != null && cb.length() > 0) {
                sb.append('"').append(cb).append('"');
            } else {
                for (int i = 0, n = bytes.length; i < n; ++i) {
                    int j = bytes[i];
                    if (j < 0) {
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.