示例#1
0
 public void flush() throws IOException {
   // Log.i("PackageManager", "flush mPos=" + mPos);
   if (mPos > 0) {
     if (mOutputStream != null) {
       CharBuffer charBuffer = CharBuffer.wrap(mText, 0, mPos);
       CoderResult result = mCharset.encode(charBuffer, mBytes, true);
       while (true) {
         if (result.isError()) {
           throw new IOException(result.toString());
         } else if (result.isOverflow()) {
           flushBytes();
           result = mCharset.encode(charBuffer, mBytes, true);
           continue;
         }
         break;
       }
       flushBytes();
       mOutputStream.flush();
     } else {
       mWriter.write(mText, 0, mPos);
       mWriter.flush();
     }
     mPos = 0;
   }
 }
  /**
   * Write String using specified encoding
   *
   * <p>When this is called multiple times, all but the last value has a trailing null
   *
   * @param encoder
   * @param next
   * @param i
   * @param noOfValues
   * @return
   * @throws CharacterCodingException
   */
  private ByteBuffer writeString(CharsetEncoder encoder, String next, int i, int noOfValues)
      throws CharacterCodingException {

    ByteBuffer bb;
    if ((i + 1) == noOfValues) {
      bb = encoder.encode(CharBuffer.wrap(next));
    } else {
      bb = encoder.encode(CharBuffer.wrap(next + '\0'));
    }
    bb.rewind();
    return bb;
  }
 private void convert(CharBuffer chars) throws IOException {
   CoderResult result = encoder.encode(chars, bytes, true);
   while (true) {
     if (result.isError()) {
       throw new IOException(result.toString());
     } else if (result.isOverflow()) {
       // flush the output buffer
       flush();
       result = encoder.encode(chars, bytes, true);
       continue;
     }
     break;
   }
 }
示例#4
0
  /**
   * Decode byte array as specified charset, then convert to UTF-8 by encoding as UTF8
   *
   * @param buf byte array to decode and convert to UTF8
   * @param charsetName charset to decode byte array into
   * @return
   * @throws CharacterCodingException
   */
  public static synchronized byte[] convertByteArrayToUTF8ByteArray(byte[] buf, String charsetName)
      throws CharacterCodingException {
    Charset cset;
    cset = Charset.forName(charsetName); // detected character set name
    CharsetDecoder csetDecoder = cset.newDecoder();

    Charset utf8 = Charset.forName(Jeeves.ENCODING);
    CharsetEncoder utf8Encoder = utf8.newEncoder();

    ByteBuffer inputBuffer = ByteBuffer.wrap(buf);

    // decode as detected character set
    CharBuffer data = csetDecoder.decode(inputBuffer);

    // encode as UTF-8
    ByteBuffer outputBuffer = utf8Encoder.encode(data);

    // remove any nulls from the end of the encoded data why? - this is a
    // bug in the encoder???? could also be that the file has characters
    // from more than one charset?
    byte[] out = outputBuffer.array();
    int length = out.length;
    while (out[length - 1] == 0) length--;

    byte[] result = new byte[length];
    System.arraycopy(out, 0, result, 0, length);

    // now return the converted bytes
    return result;
  }
示例#5
0
 public static void saveFile(File file, String content, String charsetName) {
   // if (Utils.isEmpty(fileName) || Utils.isEmpty(content)) {
   // return;
   // }
   // logger.info("save file:" + fileName + " charset:" + charsetName);
   file.getParentFile().mkdirs();
   Charset cs;
   if (null == charsetName || "".equals(charsetName)) {
     cs = Charset.defaultCharset();
   } else {
     cs = Charset.forName(charsetName);
   }
   CharsetEncoder encoder = cs.newEncoder();
   FileOutputStream os = null;
   FileChannel out = null;
   try {
     os = new FileOutputStream(file);
     out = os.getChannel();
     out.write(encoder.encode(CharBuffer.wrap(content)));
   } catch (CharacterCodingException e) {
     e.printStackTrace();
   } catch (IOException e) {
     e.printStackTrace();
   } finally {
     close(out);
     close(os);
   }
 }
示例#6
0
文件: Helper.java 项目: jruyi/jruyi
  public static void write(ICharsetCodec cc, CharBuffer cb, IUnitChain unitChain) {
    final CharsetEncoder encoder = cc.getEncoder();
    try {
      IUnit unit = Util.lastUnit(unitChain);
      ByteBuffer bb = unit.getByteBufferForWrite();

      boolean flush = false;
      encoder.reset();
      for (; ; ) {
        CoderResult cr = flush ? encoder.flush(bb) : encoder.encode(cb, bb, true);
        unit.size(bb.position() - unit.start());
        if (cr.isOverflow()) {
          unit = Util.appendNewUnit(unitChain);
          bb = unit.getByteBufferForWrite();
          continue;
        }

        if (!cr.isUnderflow()) cr.throwException();

        if (flush) break;
        else flush = true;
      }
    } catch (CharacterCodingException e) {
      throw new RuntimeException(e);
    } finally {
      cc.releaseEncoder(encoder);
    }
  }
示例#7
0
  private static void writeEntry(
      OutputStream os, Attributes.Name name, String value, CharsetEncoder encoder, ByteBuffer bBuf)
      throws IOException {
    byte[] out = name.getBytes();
    if (out.length > LINE_LENGTH_LIMIT) {
      throw new IOException(
          Messages.getString(
              "archive.33", name, Integer.valueOf(LINE_LENGTH_LIMIT))); // $NON-NLS-1$
    }

    os.write(out);
    os.write(VALUE_SEPARATOR);

    encoder.reset();
    bBuf.clear().limit(LINE_LENGTH_LIMIT - out.length - 2);

    CharBuffer cBuf = CharBuffer.wrap(value);
    CoderResult r;

    while (true) {
      r = encoder.encode(cBuf, bBuf, true);
      if (CoderResult.UNDERFLOW == r) {
        r = encoder.flush(bBuf);
      }
      os.write(bBuf.array(), bBuf.arrayOffset(), bBuf.position());
      os.write(LINE_SEPARATOR);
      if (CoderResult.UNDERFLOW == r) {
        break;
      }
      os.write(' ');
      bBuf.clear().limit(LINE_LENGTH_LIMIT - 1);
    }
  }
示例#8
0
    private void updateWithCharBuf() {
      final int reqSize = (int) charEncoder.maxBytesPerChar() * charBuff.position();
      if (byteBuff.capacity() < reqSize) {
        byteBuff = java.nio.ByteBuffer.allocate(2 * reqSize);
      }

      // Make ready for read
      charBuff.flip();

      final CoderResult cr = charEncoder.encode(charBuff, byteBuff, true);
      try {

        if (cr.isError()) cr.throwException();

        // Make ready for read
        byteBuff.flip();

        final byte[] byts = byteBuff.array();
        final int len = byteBuff.remaining();
        final int strt = byteBuff.arrayOffset();
        digest.update(byts, strt, len);

      } catch (final CharacterCodingException e) {
        throw new OXFException(e);
      } catch (java.nio.BufferOverflowException e) {
        throw new OXFException(e);
      } catch (java.nio.BufferUnderflowException e) {
        throw new OXFException(e);
      } finally {
        // Make ready for write
        charBuff.clear();
        byteBuff.clear();
      }
    }
示例#9
0
 /**
  * Adds an authentication header for basic authentication
  *
  * @param con the connection
  * @throws OsmTransferException thrown if something went wrong. Check for nested exceptions
  */
 protected void addBasicAuthorizationHeader(HttpURLConnection con) throws OsmTransferException {
   CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();
   CredentialsManagerResponse response;
   String token;
   try {
     synchronized (CredentialsManagerFactory.getCredentialManager()) {
       response =
           CredentialsManagerFactory.getCredentialManager()
               .getCredentials(
                   RequestorType.SERVER,
                   false /* don't know yet whether the credentials will succeed */);
     }
   } catch (CredentialsManagerException e) {
     throw new OsmTransferException(e);
   }
   if (response == null) {
     token = ":";
   } else if (response.isCanceled()) {
     cancel = true;
     return;
   } else {
     String username = response.getUsername() == null ? "" : response.getUsername();
     String password =
         response.getPassword() == null ? "" : String.valueOf(response.getPassword());
     token = username + ":" + password;
     try {
       ByteBuffer bytes = encoder.encode(CharBuffer.wrap(token));
       con.addRequestProperty("Authorization", "Basic " + Base64.encode(bytes));
     } catch (CharacterCodingException e) {
       throw new OsmTransferException(e);
     }
   }
 }
示例#10
0
 private void fillByteBuffer(Reader reader) throws IOException {
   CharBuffer cbuf = CharBuffer.allocate(DEFAULT_CHAR_BUFFER_SIZE);
   ByteBuffer bbuf = ByteBuffer.allocate(DEFAULT_BYTE_BUFFER_SIZE);
   List<byte[]> list = new ArrayList<byte[]>();
   while (true) {
     cbuf.clear();
     int size = reader.read(cbuf);
     if (size <= 0) {
       break;
     }
     cbuf.limit(cbuf.position());
     cbuf.rewind();
     boolean eof = false;
     while (!eof) {
       CoderResult cr = encoder.encode(cbuf, bbuf, eof);
       if (cr.isError()) {
         cr.throwException();
       } else if (cr.isUnderflow()) {
         appendBytes(list, bbuf);
         eof = true;
       } else if (cr.isOverflow()) {
         appendBytes(list, bbuf);
         bbuf.clear();
       }
     }
   }
   getByteArray(list);
 }
  @JRubyMethod
  public IRubyObject convert(ThreadContext context, IRubyObject srcBuffer) {
    if (!(srcBuffer instanceof RubyString)) {
      throw context.runtime.newTypeError(srcBuffer, context.runtime.getString());
    }

    RubyString srcString = (RubyString) srcBuffer;

    ByteList srcBL = srcString.getByteList();

    if (srcBL.getRealSize() == 0) return context.runtime.newSymbol("source_buffer_empty");

    ByteBuffer srcBB = ByteBuffer.wrap(srcBL.getUnsafeBytes(), srcBL.begin(), srcBL.getRealSize());
    try {
      CharBuffer srcCB =
          CharBuffer.allocate((int) (srcDecoder.maxCharsPerByte() * srcBL.getRealSize()) + 1);
      CoderResult decodeResult = srcDecoder.decode(srcBB, srcCB, true);
      srcCB.flip();

      ByteBuffer destBB =
          ByteBuffer.allocate((int) (destEncoder.maxBytesPerChar() * srcCB.limit()) + 1);
      CoderResult encodeResult = destEncoder.encode(srcCB, destBB, true);
      destBB.flip();

      byte[] destBytes = new byte[destBB.limit()];
      destBB.get(destBytes);

      srcDecoder.reset();
      destEncoder.reset();

      return context.runtime.newString(new ByteList(destBytes, destEncoding.getEncoding(), false));
    } catch (Exception e) {
      throw context.runtime.newRuntimeError(e.getLocalizedMessage());
    }
  }
示例#12
0
 static byte[] encode(char[] cc, Charset cs, boolean testDirect, Time t) throws Exception {
   ByteBuffer bbf;
   CharBuffer cbf;
   CharsetEncoder enc = cs.newEncoder();
   String csn = cs.name();
   if (testDirect) {
     bbf = ByteBuffer.allocateDirect(cc.length * 4);
     cbf = ByteBuffer.allocateDirect(cc.length * 2).asCharBuffer();
     cbf.put(cc).flip();
   } else {
     bbf = ByteBuffer.allocate(cc.length * 4);
     cbf = CharBuffer.wrap(cc);
   }
   CoderResult cr = null;
   long t1 = System.nanoTime() / 1000;
   for (int i = 0; i < iteration; i++) {
     cbf.rewind();
     bbf.clear();
     enc.reset();
     cr = enc.encode(cbf, bbf, true);
   }
   long t2 = System.nanoTime() / 1000;
   t.t = (t2 - t1) / iteration;
   if (cr != CoderResult.UNDERFLOW) {
     System.out.println("ENC-----------------");
     int pos = cbf.position();
     System.out.printf("  cr=%s, cbf.pos=%d, cc[pos]=%x%n", cr.toString(), pos, cc[pos] & 0xffff);
     throw new RuntimeException("Encoding err: " + csn);
   }
   byte[] bb = new byte[bbf.position()];
   bbf.flip();
   bbf.get(bb);
   return bb;
 }
示例#13
0
  /**
   * Write a string to the network. The length header of the string is written automatically and its
   * encoded to the correct CharSet automatically.
   *
   * @param value the string that shall be send to the server
   */
  @Override
  public void writeString(@Nonnull String value) throws CharacterCodingException {
    int startIndex = buffer.position();
    buffer.putShort((short) 0);

    encodingBuffer.clear();
    encodingBuffer.put(value, 0, Math.min(encodingBuffer.capacity(), value.length()));
    encodingBuffer.flip();

    do {
      CoderResult encodingResult = encoder.encode(encodingBuffer, buffer, true);
      if (!encodingResult.isError()) {
        break;
      }
      if (encodingResult.isUnmappable()) {
        log.warn(
            "Found a character that failed to encode for the transfer to the server: {} - SKIP",
            encodingBuffer.get());
      } else {
        encodingResult.throwException();
      }
    } while (encodingBuffer.hasRemaining());

    int lastIndex = buffer.position();
    buffer.position(startIndex);
    writeUShort(lastIndex - startIndex - 2);
    buffer.position(lastIndex);
  }
示例#14
0
  /**
   * Find potential matches for a query
   *
   * @param query The query string
   * @return Search results object
   */
  public EdictSearchResults search(String query) {

    CharsetEncoder encoder = this.dictionary.getCharacterHandler().getCharsetEncoder();
    ByteBuffer encodedQuery = null;
    try {
      encodedQuery = encoder.encode(CharBuffer.wrap(query));
    } catch (CharacterCodingException e) {
      // If we can't encode it we can't search for it here
      // TODO some sort of exception
      return null;
    }

    try {

      EdictComparator comparator = this.dictionary.comparator();

      int start = 0;
      int end = this.dictionary.getIndexSize() - 1;
      int match = -1;

      do {
        int current = start + ((end - start) / 2);

        int character =
            comparator.compareLeft(encodedQuery, this.dictionary.getIndexEntry(current));
        if (character > 0) {
          start = current + 1;
        } else if (character < 0) {
          end = current - 1;
        } else {
          match = current;
        }
      } while ((start <= end) && (match == -1));

      if (match != -1) {
        end = this.dictionary.getIndexSize() - 1;
        int min = match;
        int max = match;
        while ((min > 0)
            && (comparator.compareLeft(encodedQuery, this.dictionary.getIndexEntry(min - 1))
                == 0)) {
          min--;
        }
        while ((max < end)
            && (comparator.compareLeft(encodedQuery, this.dictionary.getIndexEntry(max + 1))
                == 0)) {
          max++;
        }

        return new EdictSearchResults(this.dictionary, encodedQuery, min, max);
      }

    } catch (CharacterCodingException e) {
      // Shouldn't happen. If any entries of the dictionary were broken, the term index should omit
      // all terms from that entry
      e.printStackTrace();
    }

    return new EdictSearchResults(this.dictionary, encodedQuery, null, null);
  }
示例#15
0
 static byte[] encode(Charset cs, char[] ca, int off, int len) {
   CharsetEncoder ce = cs.newEncoder();
   int en = scale(len, ce.maxBytesPerChar());
   byte[] ba = new byte[en];
   if (len == 0) return ba;
   boolean isTrusted = false;
   if (System.getSecurityManager() != null) {
     if (!(isTrusted = (cs.getClass().getClassLoader0() == null))) {
       ca = Arrays.copyOfRange(ca, off, off + len);
       off = 0;
     }
   }
   ce.onMalformedInput(CodingErrorAction.REPLACE)
       .onUnmappableCharacter(CodingErrorAction.REPLACE)
       .reset();
   if (ce instanceof ArrayEncoder) {
     int blen = ((ArrayEncoder) ce).encode(ca, off, len, ba);
     return safeTrim(ba, blen, cs, isTrusted);
   } else {
     ByteBuffer bb = ByteBuffer.wrap(ba);
     CharBuffer cb = CharBuffer.wrap(ca, off, len);
     try {
       CoderResult cr = ce.encode(cb, bb, true);
       if (!cr.isUnderflow()) cr.throwException();
       cr = ce.flush(bb);
       if (!cr.isUnderflow()) cr.throwException();
     } catch (CharacterCodingException x) {
       throw new Error(x);
     }
     return safeTrim(ba, bb.position(), cs, isTrusted);
   }
 }
示例#16
0
 public static int encode(
     final CharsetEncoder ce,
     final char[] ca,
     final int off,
     final int len,
     final byte[] targetArray) {
   if (len == 0) {
     return 0;
   }
   if (ce instanceof ArrayEncoder) {
     return ((ArrayEncoder) ce).encode(ca, off, len, targetArray);
   } else {
     ce.reset();
     ByteBuffer bb = ByteBuffer.wrap(targetArray);
     CharBuffer cb = CharBuffer.wrap(ca, off, len);
     try {
       CoderResult cr = ce.encode(cb, bb, true);
       if (!cr.isUnderflow()) {
         cr.throwException();
       }
       cr = ce.flush(bb);
       if (!cr.isUnderflow()) {
         cr.throwException();
       }
     } catch (CharacterCodingException x) {
       throw new Error(x);
     }
     return bb.position();
   }
 }
示例#17
0
    byte[] encode(char[] ca, int off, int len) {

      int en = (int) (ce.maxBytesPerChar() * len);

      byte[] ba = new byte[en];

      if (len == 0) return ba;

      ce.reset();

      ByteBuffer bb = ByteBuffer.wrap(ba);

      CharBuffer cb = CharBuffer.wrap(ca, off, len);

      try {

        CoderResult cr = ce.encode(cb, bb, true);

        if (!cr.isUnderflow()) cr.throwException();

        cr = ce.flush(bb);

        if (!cr.isUnderflow()) cr.throwException();

      } catch (CharacterCodingException x) {

        // Substitution is always enabled,

        // so this shouldn't happen

        throw new Error(x);
      }

      return trim(ba, bb.position());
    }
示例#18
0
  public static void main(String args[]) throws Exception {
    String inputFile = "samplein.txt";
    String outputFile = "sampleout.txt";

    RandomAccessFile inf = new RandomAccessFile(inputFile, "r");
    RandomAccessFile outf = new RandomAccessFile(outputFile, "rw");
    long inputLength = new File(inputFile).length();

    FileChannel inc = inf.getChannel();
    FileChannel outc = outf.getChannel();

    MappedByteBuffer inputData = inc.map(FileChannel.MapMode.READ_ONLY, 0, inputLength);

    Charset latin1 = Charset.forName("ISO-8859-1");
    CharsetDecoder decoder = latin1.newDecoder();
    CharsetEncoder encoder = latin1.newEncoder();

    CharBuffer cb = decoder.decode(inputData);

    // Process char data here

    ByteBuffer outputData = encoder.encode(cb);

    outc.write(outputData);

    inf.close();
    outf.close();
  }
示例#19
0
    @Override
    protected void onTextMessage(CharBuffer message) throws IOException {
      String temp = message.toString();
      JSONObject json = (JSONObject) JSONSerializer.toJSON(temp);

      String type = json.getString("type").toLowerCase().replaceAll(" ", "");
      String location = json.getString("location");
      String lat = json.getString("lat");
      String lng = json.getString("lng");
      String radius = json.getString("radius");
      String keywords = json.getString("keywords");

      String result = null;
      try {
        if (type.equals("overview")) result = getOverViewData(location, lat, lng, radius, keywords);
      } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        JSONObject jsonResult = new JSONObject();
        jsonResult.put("error", "true");
        result = jsonResult.toString();
      }
      if (result == null || result.equals("")) {
        JSONObject jsonResult = new JSONObject();
        jsonResult.put("error", "true");
        result = jsonResult.toString();
      }
      Charset charset = Charset.forName("ISO-8859-1");
      CharsetDecoder decoder = charset.newDecoder();
      CharsetEncoder encoder = charset.newEncoder();
      CharBuffer uCharBuffer = CharBuffer.wrap(result);
      ByteBuffer bbuf = encoder.encode(uCharBuffer);
      CharBuffer cbuf = decoder.decode(bbuf);
      getWsOutbound().writeTextMessage(cbuf);
    }
示例#20
0
  // Writes a C string and expands the frame if necessary
  // FIXME: Property strings containing nul ('\0') characters will corrupt the frame when they are
  // written.
  //        Figure out how to throw an exception here if any nul chars are encountered
  private static ByteBuffer writeCString(ByteBuffer frame, String string) {
    Byte b = propertyAbbreviations.get(string);
    if (b != null) {
      if (frame.remaining() < 2)
        frame = ByteBuffer.allocate(frame.capacity() << 1).put((ByteBuffer) frame.rewind());
      frame.put(b);
      frame.put((byte) 0);
    } else {
      CharsetEncoder cStringEncoder = cStringCharset.newEncoder();

      CharBuffer chars = CharBuffer.wrap(string);
      for (int size = frame.capacity(); ; cStringEncoder.flush(frame)) {
        cStringEncoder.reset();
        if (cStringEncoder.encode(chars, frame, true) == CoderResult.OVERFLOW) {
          // debug output
          // System.out.println("overflow, reallocating to size " + (size << 1) + " (printing \"" +
          // string + "\")");
          frame = ByteBuffer.allocate(size = (size << 1)).put((ByteBuffer) frame.rewind());
        } else break;
      }
      cStringEncoder.flush(frame);
      frame.put((byte) 0);
    }
    return frame;
  }
示例#21
0
 public ByteBuffer flush(ByteBuffer dst) throws SVNException {
   if (myCharBuffer != null) {
     CoderResult result;
     while (true) {
       result = myDecoder.flush(myCharBuffer);
       if (result.isError()) {
         throwException(result);
       }
       if (result.isUnderflow()) {
         break;
       }
     }
     myCharBuffer.flip();
     dst = allocate(dst, (int) (myEncoder.maxBytesPerChar() * myCharBuffer.remaining()));
     result = myEncoder.encode(myCharBuffer, dst, true);
     if (result.isError()) {
       throwException(result);
     }
     while (true) {
       result = myEncoder.flush(dst);
       if (result.isError()) {
         throwException(result);
       }
       if (result.isUnderflow()) {
         break;
       }
     }
   }
   reset();
   return dst;
 }
示例#22
0
  public ByteBuffer convertChunk(
      byte[] b, int offset, int length, ByteBuffer dst, boolean endOfInput) throws SVNException {
    myInputByteBuffer = allocate(myInputByteBuffer, length);
    myInputByteBuffer.put(b, offset, length);
    myInputByteBuffer.flip();
    myCharBuffer =
        allocate(myCharBuffer, (int) (myDecoder.maxCharsPerByte() * myInputByteBuffer.remaining()));

    CoderResult result = myDecoder.decode(myInputByteBuffer, myCharBuffer, endOfInput);
    if (result.isError()) {
      throwException(result);
    } else if (result.isUnderflow()) {
      myInputByteBuffer.compact();
    } else {
      myInputByteBuffer.clear();
    }

    myCharBuffer.flip();
    dst = allocate(dst, (int) (myEncoder.maxBytesPerChar() * myCharBuffer.remaining()));

    result = myEncoder.encode(myCharBuffer, dst, false);
    if (result.isError()) {
      throwException(result);
    } else if (result.isUnderflow()) {
      myCharBuffer.compact();
    } else {
      myCharBuffer.clear();
    }

    return dst;
  }
示例#23
0
  /**
   * Creates a string in a specfied character set.
   *
   * @param value String constant, must not be null
   * @param charsetName Name of the character set, may be null
   * @param collation Collation, may be null
   * @throws IllegalCharsetNameException If the given charset name is illegal
   * @throws UnsupportedCharsetException If no support for the named charset is available in this
   *     instance of the Java virtual machine
   * @throws RuntimeException If the given value cannot be represented in the given charset
   */
  public NlsString(String value, String charsetName, SqlCollation collation) {
    assert value != null;
    if (null != charsetName) {
      charsetName = charsetName.toUpperCase();
      this.charsetName = charsetName;
      String javaCharsetName = SqlUtil.translateCharacterSetName(charsetName);
      if (javaCharsetName == null) {
        throw new UnsupportedCharsetException(charsetName);
      }
      this.charset = Charset.forName(javaCharsetName);
      CharsetEncoder encoder = charset.newEncoder();

      // dry run to see if encoding hits any problems
      try {
        encoder.encode(CharBuffer.wrap(value));
      } catch (CharacterCodingException ex) {
        throw RESOURCE.charsetEncoding(value, javaCharsetName).ex();
      }
    } else {
      this.charsetName = null;
      this.charset = null;
    }
    this.collation = collation;
    this.value = value;
  }
示例#24
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);
   }
 }
  /**
   * Write String in UTF-BEBOM format
   *
   * <p>When this is called multiple times, all but the last value has a trailing null
   *
   * @param next
   * @param i
   * @param noOfValues
   * @return
   * @throws CharacterCodingException
   */
  private ByteBuffer writeStringUTF16BEBOM(String next, int i, int noOfValues)
      throws CharacterCodingException {
    CharsetEncoder encoder =
        Charset.forName(TextEncoding.CHARSET_UTF_16_BE_ENCODING_FORMAT).newEncoder();
    encoder.onMalformedInput(CodingErrorAction.IGNORE);
    encoder.onUnmappableCharacter(CodingErrorAction.IGNORE);

    ByteBuffer bb = null;
    // Add BOM
    if ((i + 1) == noOfValues) {
      bb = encoder.encode(CharBuffer.wrap('\ufeff' + next));
    } else {
      bb = encoder.encode(CharBuffer.wrap('\ufeff' + next + '\0'));
    }
    bb.rewind();
    return bb;
  }
示例#26
0
 public static void main(String args[]) throws Exception {
   String s = "abc\uD800\uDC00qrst"; // Valid surrogate
   char[] c = s.toCharArray();
   CharsetEncoder enc =
       Charset.forName("ISO8859_1").newEncoder().onUnmappableCharacter(CodingErrorAction.REPLACE);
   /* Process the first 4 characters, including the high surrogate
   which should be stored */
   ByteBuffer bb = ByteBuffer.allocate(10);
   CharBuffer cb = CharBuffer.wrap(c);
   cb.limit(4);
   enc.encode(cb, bb, false);
   cb.limit(7);
   enc.encode(cb, bb, true);
   byte[] first = bb.array();
   for (int i = 0; i < 7; i++)
     System.err.printf("[%d]=%d was %d\n", i, (int) first[i] & 0xffff, (int) c[i] & 0xffff);
 }
  public static ByteBuffer encode(String charsetName, CharBuffer charBuffer) {
    try {
      CharsetEncoder charsetEncoder = getCharsetEncoder(charsetName);

      return charsetEncoder.encode(charBuffer);
    } catch (CharacterCodingException cce) {
      throw new Error(cce);
    }
  }
示例#28
0
 /**
  * Converts the specified property file text to a Properties object.
  *
  * @param propFileText the property file text in standard property file format
  * @return the resulting Properties object
  * @throws java.nio.charset.CharacterCodingException if invalid encoding
  */
 public static Properties toProperties(String propFileText) throws CharacterCodingException {
   CharsetEncoder encoder =
       Charset.forName("ISO-8859-1").newEncoder().onUnmappableCharacter(CodingErrorAction.REPORT);
   byte[] bytes = encoder.encode(CharBuffer.wrap(propFileText)).array();
   Properties props = new Properties();
   try {
     props.load(new ByteArrayInputStream(bytes));
   } catch (IOException ex) {
     throw new RuntimeException(ex); // shouldn't happen with BAIS
   }
   return props;
 }
示例#29
0
 /** Encode a string into a ByteBuffer : on return position is the end of the encoding */
 public static int toByteBuffer(CharSequence s, ByteBuffer bb, CharsetEncoder enc) {
   int start = bb.position();
   CharBuffer cBuff = CharBuffer.wrap(s);
   enc.reset();
   CoderResult r = enc.encode(cBuff, bb, true);
   if (r.isOverflow()) throw new InternalErrorException("Bytes.toByteBuffer: encode overflow (1)");
   r = enc.flush(bb);
   if (r.isOverflow()) throw new InternalErrorException("Bytes.toByteBuffer: encode overflow (2)");
   //        if ( r.isUnderflow() )
   //            throw new InternalErrorException("Bytes.toByteBuffer: encode underflow") ;
   int finish = bb.position();
   return finish - start;
 }
 // creates the byte array representing the replacement character in the
 // "to" character set; we create a new encoder to do this, so that we
 // don't muck with the state of the "real" encoder (in particular, whether
 // it will handle a BOM)
 private static byte[] encodeReplacement(Charset to, char replacement) {
   try {
     CharsetEncoder tempEncoder = to.newEncoder();
     CharBuffer src = CharBuffer.wrap(new char[] {replacement});
     ByteBuffer dst = tempEncoder.encode(src);
     dst.position(0);
     byte[] result = new byte[dst.remaining()];
     dst.get(result);
     return result;
   } catch (CharacterCodingException e) {
     throw new IllegalArgumentException("illegal replacement character: " + (int) replacement);
   }
 }