Package java.nio.charset

Examples of java.nio.charset.Charset.encode()


  }

  // convert char array to byte array, assuming UTF-8 encoding
  static protected byte[] convertCharToByteUTF(char[] from) {
    Charset c = Charset.forName("UTF-8");
    ByteBuffer output = c.encode(CharBuffer.wrap(from));
    return output.array();
  }

  //////////////////////////////////////////////////////////////////////////////////////
  // create new file
View Full Code Here


         char[] chars = s.toCharArray();
      Charset cs = Charset.forName ("UTF-8");
        CharBuffer cb = CharBuffer.allocate (chars.length);
        cb.put (chars);
                 cb.flip ();
        ByteBuffer bb = cs.encode (cb);
       
        return bb.array();
      }
      else
         bytes = new byte[0];
View Full Code Here

      for (int i = 0; i < file.contentSize; i++) {
        contents.append(i % 10);
      }
      GcsOutputChannel outputChannel =
          gcsService.createOrReplace(file.filename, GcsFileOptions.getDefaultInstance());
      outputChannel.write(utf8.encode(CharBuffer.wrap(contents.toString())));
      outputChannel.close();
    }
  }

  @After
View Full Code Here

          log.debug("Detected charset: " + match.getName());

          String content = match.getString();
          CharBuffer buffer = CharBuffer.wrap(content.toCharArray());
          Charset utf8Charset = Charset.forName("UTF-8");
          String utf8Content = new String(utf8Charset.encode(buffer).array(), "UTF-8");

          return new OkPage(url, utf8Content);
        }
        return new ErrorPage(url, status);
      } finally {
View Full Code Here

  public static byte[] getBytes (char[] chars) {
    Charset cs = Charset.forName ("UTF-8");
    CharBuffer cb = CharBuffer.allocate (chars.length);
    cb.put (chars);
                cb.flip ();
    ByteBuffer bb = cs.encode (cb);
   
    return bb.array();
        }
  public static char[] getChars (byte[] bytes) {
View Full Code Here

      this.char_decoder_ = charset.newDecoder();
      this.char_encoder_ = charset.newEncoder();
      char_decoder_.onMalformedInput(CodingErrorAction.IGNORE);
      char_decoder_.onUnmappableCharacter(CodingErrorAction.IGNORE);

      ByteBuffer replacement = charset.encode(BAD_INPUT_REPLACEMENT);
      byte[] replace_bytes = new byte[replacement.remaining()];
      replacement.get(replace_bytes);
      char_encoder_.onMalformedInput(CodingErrorAction.REPLACE);
      char_encoder_.onUnmappableCharacter(CodingErrorAction.REPLACE);
      char_encoder_.replaceWith(replace_bytes);
View Full Code Here

  public static byte[] getBytes (char[] chars) {
    Charset cs = Charset.forName ("UTF-8");
    CharBuffer cb = CharBuffer.allocate (chars.length);
    cb.put (chars);
                cb.flip ();
    ByteBuffer bb = cs.encode (cb);
   
    return bb.array();
        }
  public static char[] getChars (byte[] bytes) {
View Full Code Here

      this.char_decoder_ = charset.newDecoder();
      this.char_encoder_ = charset.newEncoder();
      char_decoder_.onMalformedInput(CodingErrorAction.IGNORE);
      char_decoder_.onUnmappableCharacter(CodingErrorAction.IGNORE);

      ByteBuffer replacement = charset.encode(BAD_INPUT_REPLACEMENT);
      byte[] replace_bytes = new byte[replacement.remaining()];
      replacement.get(replace_bytes);
      char_encoder_.onMalformedInput(CodingErrorAction.REPLACE);
      char_encoder_.onUnmappableCharacter(CodingErrorAction.REPLACE);
      char_encoder_.replaceWith(replace_bytes);
View Full Code Here

    Charset cs = findCharset(encoding);

    // can't simply .array() this to get the bytes
    // especially with variable-length charsets the
    // buffer is sometimes larger than the actual encoded data
    ByteBuffer buf = cs.encode(value);
   
    int encodedLen = buf.limit();
    byte[] asBytes = new byte[encodedLen];
    buf.get(asBytes, 0, encodedLen);
   
View Full Code Here

 
  public static byte[] getBytes(String value) {
    try {
      Charset cs = findCharset(platformEncoding);
     
      ByteBuffer buf = cs.encode(value);
     
      int encodedLen = buf.limit();
      byte[] asBytes = new byte[encodedLen];
      buf.get(asBytes, 0, encodedLen);
     
View Full Code Here

TOP
Copyright © 2018 www.massapi.com. 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.