Ejemplo n.º 1
0
 public char[] encode(byte[] data, String encoding) throws IOException {
   if (encoding == null) {
     encoding = DEFAULT_ENCODING;
   }
   if (!Charset.isSupported(encoding)) throw new IOException("Unsupported encoding " + encoding);
   Charset charset = Charset.forName(encoding);
   CharsetDecoder cd =
       charset
           .newDecoder()
           .onMalformedInput(CodingErrorAction.REPLACE)
           .onUnmappableCharacter(CodingErrorAction.REPLACE);
   int en = (int) (cd.maxCharsPerByte() * data.length);
   char[] ca = new char[en];
   ByteBuffer bb = ByteBuffer.wrap(data);
   CharBuffer cb = CharBuffer.wrap(ca);
   CoderResult cr = cd.decode(bb, cb, true);
   if (!cr.isUnderflow()) {
     cr.throwException();
   }
   cr = cd.flush(cb);
   if (!cr.isUnderflow()) {
     cr.throwException();
   }
   return trim(ca, cb.position());
 }
Ejemplo n.º 2
0
  @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());
    }
  }
Ejemplo n.º 3
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;
  }
Ejemplo n.º 4
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();
  }
Ejemplo n.º 5
0
 @SuppressFBWarnings("SUA_SUSPICIOUS_UNINITIALIZED_ARRAY")
 public static String decode(
     final CharsetDecoder cd, final byte[] ba, final int off, final int len) {
   if (len == 0) {
     return "";
   }
   int en = (int) (len * (double) cd.maxCharsPerByte());
   char[] ca = Arrays.getCharsTmp(en);
   if (cd instanceof ArrayDecoder) {
     int clen = ((ArrayDecoder) cd).decode(ba, off, len, ca);
     return new String(ca, 0, clen);
   }
   cd.reset();
   ByteBuffer bb = ByteBuffer.wrap(ba, off, len);
   CharBuffer cb = CharBuffer.wrap(ca);
   try {
     CoderResult cr = cd.decode(bb, cb, true);
     if (!cr.isUnderflow()) {
       cr.throwException();
     }
     cr = cd.flush(cb);
     if (!cr.isUnderflow()) {
       cr.throwException();
     }
   } catch (CharacterCodingException x) {
     throw new Error(x);
   }
   return new String(ca, 0, cb.position());
 }
Ejemplo n.º 6
0
 public String getPayloadTracingString() {
   if (null == payload || 0 == payload.length) return "no payload";
   boolean text = true;
   for (byte b : payload) {
     if (' ' > b) {
       switch (b) {
         case '\t':
         case '\n':
         case '\r':
           continue;
       }
       text = false;
       break;
     }
   }
   if (text) {
     CharsetDecoder decoder = CoAP.UTF8_CHARSET.newDecoder();
     decoder.onMalformedInput(CodingErrorAction.REPORT);
     decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
     ByteBuffer in = ByteBuffer.wrap(payload);
     CharBuffer out = CharBuffer.allocate(24);
     CoderResult result = decoder.decode(in, out, true);
     decoder.flush(out);
     out.flip();
     if (CoderResult.OVERFLOW == result) {
       return "\"" + out + "\".. " + payload.length + " bytes";
     } else if (!result.isError()) {
       return "\"" + out + "\"";
     }
   }
   return Utils.toHexText(payload, 256);
 }
Ejemplo n.º 7
0
 private static String decode(final ByteBuffer b, final Charset charset)
     throws CharacterCodingException {
   final CharsetDecoder d = charset.newDecoder();
   d.onMalformedInput(CodingErrorAction.REPORT);
   d.onUnmappableCharacter(CodingErrorAction.REPORT);
   return d.decode(b).toString();
 }
  /** Load synonyms with the given {@link SynonymMap.Parser} class. */
  private SynonymMap loadSynonyms(
      ResourceLoader loader, String cname, boolean dedup, Analyzer analyzer)
      throws IOException, ParseException {
    CharsetDecoder decoder =
        Charset.forName("UTF-8")
            .newDecoder()
            .onMalformedInput(CodingErrorAction.REPORT)
            .onUnmappableCharacter(CodingErrorAction.REPORT);

    SynonymMap.Parser parser;
    Class<? extends SynonymMap.Parser> clazz = loader.findClass(cname, SynonymMap.Parser.class);
    try {
      parser =
          clazz
              .getConstructor(boolean.class, boolean.class, Analyzer.class)
              .newInstance(dedup, expand, analyzer);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }

    File synonymFile = new File(synonyms);
    if (synonymFile.exists()) {
      decoder.reset();
      parser.parse(new InputStreamReader(loader.openResource(synonyms), decoder));
    } else {
      List<String> files = splitFileNames(synonyms);
      for (String file : files) {
        decoder.reset();
        parser.parse(new InputStreamReader(loader.openResource(file), decoder));
      }
    }
    return parser.build();
  }
Ejemplo n.º 9
0
    char[] decode(byte[] ba, int off, int len) {

      int en = (int) (cd.maxCharsPerByte() * len);

      char[] ca = new char[en];

      if (len == 0) return ca;

      cd.reset();

      ByteBuffer bb = ByteBuffer.wrap(ba, off, len);

      CharBuffer cb = CharBuffer.wrap(ca);

      try {

        CoderResult cr = cd.decode(bb, cb, true);

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

        cr = cd.flush(cb);

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

      } catch (CharacterCodingException x) {

        // Substitution is always enabled,

        // so this shouldn't happen

        throw new Error(x);
      }

      return trim(ca, cb.position());
    }
Ejemplo n.º 10
0
  private void assertValidReplaceKind(File repositoryRoot, long revision, SVNNodeKind kind)
      throws SVNException {
    final CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
    decoder.onMalformedInput(CodingErrorAction.IGNORE);
    decoder.onUnmappableCharacter(CodingErrorAction.IGNORE);

    final File revisionNodeFile = new File(repositoryRoot, "db/revs/0/" + revision);
    final InputStream in = SVNFileUtil.openFileForReading(revisionNodeFile);
    final StringBuffer buffer = new StringBuffer();
    final String replaceString = "replace-" + kind;
    boolean replaceLineFound = false;
    try {
      while (true) {
        final String line = SVNFileUtil.readLineFromStream(in, buffer, decoder);
        if (line == null) {
          break;
        }
        buffer.setLength(0);
        if (line.indexOf(replaceString) >= 0) {
          replaceLineFound = true;
          break;
        }
      }
    } catch (IOException e) {
      SVNErrorManager.error(SVNErrorMessage.create(SVNErrorCode.UNKNOWN, e), e, SVNLogType.CLIENT);
    } finally {
      SVNFileUtil.closeFile(in);
    }
    Assert.assertTrue(
        "Could not find 'replace-" + kind + "' string in revision node file", replaceLineFound);
  }
Ejemplo n.º 11
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;
  }
Ejemplo n.º 12
0
 public static void main(String[] arguments) {
   try {
     // read byte data into a byte buffer
     String data = "friends.dat";
     FileInputStream inData = new FileInputStream(data);
     FileChannel inChannel = inData.getChannel();
     long inSize = inChannel.size();
     ByteBuffer source = ByteBuffer.allocate((int) inSize);
     inChannel.read(source, 0);
     source.position(0);
     System.out.println("Original byte data:");
     for (int i = 0; source.remaining() > 0; i++) {
       System.out.print(source.get() + " ");
     }
     // convert byte data into character data
     source.position(0);
     Charset ascii = Charset.forName("US-ASCII");
     CharsetDecoder toAscii = ascii.newDecoder();
     CharBuffer destination = toAscii.decode(source);
     destination.position(0);
     System.out.println("\n\nNew character data:");
     for (int i = 0; destination.remaining() > 0; i++) {
       System.out.print(destination.get());
     }
     System.out.println();
   } catch (FileNotFoundException fne) {
     System.out.println(fne.getMessage());
   } catch (IOException ioe) {
     System.out.println(ioe.getMessage());
   }
 }
Ejemplo n.º 13
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);
    }
  /**
   * Read a 'n' bytes from buffer into a String where n is the framesize - offset so therefore
   * cannot use this if there are other objects after it because it has no delimiter.
   *
   * <p>Must take into account the text encoding defined in the Encoding Object ID3 Text Frames
   * often allow multiple strings seperated by the null char appropriate for the encoding.
   *
   * @param arr this is the buffer for the frame
   * @param offset this is where to start reading in the buffer for this field
   * @throws NullPointerException
   * @throws IndexOutOfBoundsException
   */
  public void readByteArray(byte[] arr, int offset) throws InvalidDataTypeException {
    logger.finest("Reading from array from offset:" + offset);

    // Get the Specified Decoder
    String charSetName = getTextEncodingCharSet();
    CharsetDecoder decoder = Charset.forName(charSetName).newDecoder();
    decoder.reset();

    // Decode sliced inBuffer
    ByteBuffer inBuffer;
    // #302 [dallen] truncating array manually since the decoder.decode() does not honor the offset
    // in the in buffer
    byte[] truncArr = new byte[arr.length - offset];
    System.arraycopy(arr, offset, truncArr, 0, truncArr.length);
    inBuffer = ByteBuffer.wrap(truncArr);

    CharBuffer outBuffer = CharBuffer.allocate(arr.length - offset);
    CoderResult coderResult = decoder.decode(inBuffer, outBuffer, true);
    if (coderResult.isError()) {
      logger.warning("Decoding error:" + coderResult.toString());
    }
    decoder.flush(outBuffer);
    outBuffer.flip();

    // If using UTF16 with BOM we then search through the text removing any BOMs that could exist
    // for multiple values, BOM could be Big Endian or Little Endian
    if (charSetName.equals(TextEncoding.CHARSET_UTF_16)) {
      value = outBuffer.toString().replace("\ufeff", "").replace("\ufffe", "");
    } else {
      value = outBuffer.toString();
    }
    // SetSize, important this is correct for finding the next datatype
    setSize(arr.length - offset);
    logger.config("Read SizeTerminatedString:" + value + " size:" + size);
  }
Ejemplo n.º 15
0
 static boolean check(CharsetDecoder dec, byte[] bytes, boolean direct, int[] flow) {
   int inPos = flow[0];
   int inLen = flow[1];
   int outPos = flow[2];
   int outLen = flow[3];
   int expedInPos = flow[4];
   int expedOutPos = flow[5];
   CoderResult expedCR = (flow[6] == 0) ? CoderResult.UNDERFLOW : CoderResult.OVERFLOW;
   ByteBuffer bbf;
   CharBuffer cbf;
   if (direct) {
     bbf = ByteBuffer.allocateDirect(inPos + bytes.length);
     cbf = ByteBuffer.allocateDirect((outPos + outLen) * 2).asCharBuffer();
   } else {
     bbf = ByteBuffer.allocate(inPos + bytes.length);
     cbf = CharBuffer.allocate(outPos + outLen);
   }
   bbf.position(inPos);
   bbf.put(bytes).flip().position(inPos).limit(inPos + inLen);
   cbf.position(outPos);
   dec.reset();
   CoderResult cr = dec.decode(bbf, cbf, false);
   if (cr != expedCR || bbf.position() != expedInPos || cbf.position() != expedOutPos) {
     System.out.printf("Expected(direct=%5b): [", direct);
     for (int i : flow) System.out.print(" " + i);
     System.out.println(
         "]  CR=" + cr + ", inPos=" + bbf.position() + ", outPos=" + cbf.position());
     return false;
   }
   return true;
 }
Ejemplo n.º 16
0
  public static void main(String args[]) {
    if (args.length != 1) {
      System.err.println("Provide a filename");
      return;
    }

    try {
      // Map File from filename to byte buffer
      FileInputStream input = new FileInputStream(args[0]);
      FileChannel channel = input.getChannel();
      int fileLength = (int) channel.size();
      MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, fileLength);

      // Convert to character buffer
      Charset charset = Charset.forName("ISO-8859-1");
      CharsetDecoder decoder = charset.newDecoder();
      CharBuffer charBuffer = decoder.decode(buffer);

      // Create line pattern
      // Pattern linePattern = Pattern.compile(".*$", Pattern.MULTILINE);
      Pattern linePattern = Pattern.compile(".*$");
      // Create word pattern
      Pattern wordBreakPattern = Pattern.compile("[\\p{Punct}\\s}]");
      System.out.println(wordBreakPattern);

      // Match line pattern to buffer
      Matcher lineMatcher = linePattern.matcher(charBuffer);

      // Holder for longest word
      String longest = "";
      int lineCount = 0;
      // For each line
      while (lineMatcher.find()) {
        lineCount++;
        // Get line
        String line = lineMatcher.group();
        System.out.println(line);
        // Get array of words on line
        String words[] = wordBreakPattern.split(line);
        for (String temp : words) {

          System.out.println(temp);
        }
        // Look for longest word
        for (int i = 0, n = words.length; i < n; i++) {
          if (words[i].length() > longest.length()) {
            longest = words[i];
          }
        }
      }
      // Report
      System.out.println("Longest word: " + longest);
      System.out.println("Total: " + lineCount + " lines.");
      // Close
      input.close();
    } catch (IOException e) {
      System.err.println("Error processing");
    }
  }
Ejemplo n.º 17
0
 public String getString(byte[] bytes) {
   CharsetDecoder utf8Decoder = UTF_8.newDecoder();
   try {
     return utf8Decoder.decode(ByteBuffer.wrap(bytes)).toString();
   } catch (CharacterCodingException e) {
     throw new MarshalException("invalid UTF8 bytes " + Arrays.toString(bytes));
   }
 }
  public void listen() {
    try {
      int length;
      int b;
      String inputLine;
      Charset utf = Charset.forName("UTF-8");
      CharsetDecoder decoder = utf.newDecoder();
      byte[] buffer = new byte[MAX_BUFFER_LENGTH];
      ByteBuffer bytes;
      while (isOpen) {
        decoder.reset();
        b = in.read();
        if ((b & 0x80) == 0x80) {
          length = 0;
          while (b != -1 && (b & 0x80) == 0x80) {
            b = in.read();
            int bv = b & 0x7F;
            length *= 128;
            length += bv;
          }
          in.read(buffer, 0, length);
        } else if (b == 0x00) {
          b = in.read();
          inputLine = "";
          length = 0;
          while (b != -1 && (b & 0xFF) != 0xFF) {
            buffer[length] = (byte) b;
            b = in.read();
            length++;
            if (length > MAX_BUFFER_LENGTH) {
              bytes = ByteBuffer.wrap(buffer);
              inputLine += decoder.decode(bytes).toString();
              length = 0;
            }
          }
        } else {
          break;
        }
        bytes = ByteBuffer.wrap(buffer, 0, length);
        inputLine = decoder.decode(bytes).toString();
        if (b == -1) {
          break;
        }
        messageReceived(inputLine);
      }

      connectionClosed();

      out.close();
      in.close();
      socket.close();

    } catch (IOException e) {
      e.printStackTrace();
    }
  }
  protected String sendAndReceive(byte[] payload) throws IOException {
    ByteBuffer sbuf = ByteBuffer.wrap(payload);
    ByteBuffer rbuf = ByteBuffer.allocateDirect(256);
    CharsetDecoder rbufDecoder = Charset.forName("UTF-8").newDecoder();
    StringBuilder response = new StringBuilder();

    int ops = SelectionKey.OP_WRITE | SelectionKey.OP_READ;
    if (this.channel.isConnectionPending()) {
      ops = ops | SelectionKey.OP_CONNECT;
    }

    Selector selector = Selector.open();
    try {
      this.channel.register(selector, ops);
      while (true) {
        if (0 < selector.select(Client.POLLS_INTERVAL * 1000)) {
          Iterator keys = selector.selectedKeys().iterator();
          while (keys.hasNext()) {
            SelectionKey key = (SelectionKey) keys.next();
            SocketChannel ch = (SocketChannel) key.channel();
            if (key.isConnectable()) {
              // Just connected
              ch.finishConnect();
            }
            if (key.isReadable() && !sbuf.hasRemaining()) {
              // Receiving the response
              while (0 < ch.read(rbuf)) {
                rbuf.flip();
                response.append(rbufDecoder.decode(rbuf).toString());
              }
              if (2 <= response.length()
                  && response
                      .substring(response.length() - 2, response.length())
                      .equals(SocketClient.TERMINATOR)) {
                response.setLength(response.length() - 2);
                return response.toString();
              } else if (0 == response.length()) {
                throw new IOException("Connection lost");
              }
            }
            if (key.isWritable() && sbuf.hasRemaining()) {
              // Sending the request
              while (0 < ch.write(sbuf) && sbuf.hasRemaining()) {
                //
              }
            }
            keys.remove();
          }
        }
      }
    } catch (java.lang.Exception e) {
      throw new IOException("API communication failed: " + e.toString());
    } finally {
      selector.close();
    }
  }
 /** Create a new WriterOutputStream which writes to the given writer. */
 public WriterOutputStream(Writer writer) {
   this.writer = writer;
   decoder.reset();
   decoder.onMalformedInput(CodingErrorAction.REPLACE);
   decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
   inBuffer = ByteBuffer.allocate(4096);
   inBuffer.clear();
   outBuffer = CharBuffer.allocate(4096);
   outBuffer.clear();
 }
Ejemplo n.º 21
0
  protected static String guessEncoding(Blob blob) throws IOException {
    // encoding already known?
    if (blob.getEncoding() != null) {
      return null;
    }

    // bad mime type?
    String mimeType = blob.getMimeType();
    if (mimeType == null) {
      return null;
    }
    if (!mimeType.startsWith("text/") && !mimeType.startsWith("application/xhtml")) {
      // not a text file, we shouldn't be in the Note importer
      return null;
    }

    byte[] bytes = blob.getByteArray();

    List<String> charsets = Arrays.asList("utf-8", "iso-8859-1");

    // charset specified in MIME type?
    String CSEQ = "charset=";
    int i = mimeType.indexOf(CSEQ);
    if (i > 0) {
      String onlyMimeType = mimeType.substring(0, i).replace(";", "").trim();
      blob.setMimeType(onlyMimeType);
      String charset = mimeType.substring(i + CSEQ.length());
      i = charset.indexOf(";");
      if (i > 0) {
        charset = charset.substring(0, i);
      }
      charset = charset.trim().replace("\"", "");
      charsets = new ArrayList<String>(charsets);
      charsets.add(0, charset);
    }

    // resort to auto-detection
    for (String charset : charsets) {
      try {
        Charset cs = Charset.forName(charset);
        CharsetDecoder d =
            cs.newDecoder()
                .onMalformedInput(CodingErrorAction.REPORT)
                .onUnmappableCharacter(CodingErrorAction.REPORT);
        CharBuffer cb = d.decode(ByteBuffer.wrap(bytes));
        return cb.toString();
      } catch (IllegalArgumentException e) {
        // illegal charset
      } catch (CharacterCodingException e) {
        // could not decode
      }
    }
    // nothing worked, use platform
    return null;
  }
Ejemplo n.º 22
0
 /**
  * Attempts to convert text in a given character set to a Unicode string. Returns null on failure.
  *
  * @param text ByteBuffer containing the character array to convert.
  * @param charsetName Character set it's in encoded in.
  * @return: Unicode string on success, null on failure.
  */
 @CalledByNative
 private static String convertToUnicode(ByteBuffer text, String charsetName) {
   try {
     Charset charset = Charset.forName(charsetName);
     CharsetDecoder decoder = charset.newDecoder();
     // On invalid characters, this will throw an exception.
     return decoder.decode(text).toString();
   } catch (Exception e) {
     return null;
   }
 }
Ejemplo n.º 23
0
  /**
   * This method should be called if the InputBuffer is being used in conjunction with a {@link
   * java.io.Reader} implementation. If this method is not called, any character-based methods
   * called on this <code>InputBuffer</code> will throw a {@link IllegalStateException}.
   */
  public void processingChars() {

    if (!processingChars) {
      processingChars = true;
      final String enc = httpHeader.getCharacterEncoding();
      if (enc != null) {
        encoding = enc;
        final CharsetDecoder localDecoder = getDecoder();
        averageCharsPerByte = localDecoder.averageCharsPerByte();
      }
    }
  }
Ejemplo n.º 24
0
  /** Decode a string into a ByteBuffer */
  public static String fromByteBuffer(ByteBuffer bb, CharsetDecoder dec) {
    if (bb.remaining() == 0) return "";

    dec.reset();
    CharBuffer cBuff = CharBuffer.allocate(bb.remaining());
    CoderResult r = dec.decode(bb, cBuff, true);
    if (r.isOverflow()) throw new InternalErrorException("fromByteBuffer: decode overflow (1)");
    r = dec.flush(cBuff);
    if (r.isOverflow()) throw new InternalErrorException("fromByteBuffer: decode overflow (2)");
    cBuff.flip();
    return cBuff.toString();
  }
Ejemplo n.º 25
0
 public static String byteBufferToString(ByteBuffer buffer) {
   CharBuffer charBuffer = null;
   try {
     Charset charset = Charset.forName("UTF-8");
     CharsetDecoder decoder = charset.newDecoder();
     charBuffer = decoder.decode(buffer);
     buffer.flip();
     return charBuffer.toString();
   } catch (Exception ex) {
     ex.printStackTrace();
     return null;
   }
 }
 String toString(byte[] ba, int length) {
   CharsetDecoder cd = decoder().reset();
   int len = (int) (length * cd.maxCharsPerByte());
   char[] ca = new char[len];
   if (len == 0) return new String(ca);
   ByteBuffer bb = ByteBuffer.wrap(ba, 0, length);
   CharBuffer cb = CharBuffer.wrap(ca);
   CoderResult cr = cd.decode(bb, cb, true);
   if (!cr.isUnderflow()) throw new IllegalArgumentException(cr.toString());
   cr = cd.flush(cb);
   if (!cr.isUnderflow()) throw new IllegalArgumentException(cr.toString());
   return new String(ca, 0, cb.position());
 }
  /**
   * Flush the input buffer (byte buffer), as much as possible. This may leave a few undecoded bytes
   * in the buffer.
   *
   * @param endOfInput true if there is no more input available
   * @throws IOException
   */
  private void flushInBuffer(boolean endOfInput) throws IOException {
    // Prepare to read from the input buffer
    inBuffer.flip();

    CoderResult result = decoder.decode(inBuffer, outBuffer, endOfInput);
    while (result.isOverflow()) {
      flushOutBuffer();
      result = decoder.decode(inBuffer, outBuffer, endOfInput);
    }

    // Remove processed input from the input buffer, and position for writing
    inBuffer.compact();
  }
  public static String stringDecode(ByteBuffer oByteBuffer) {
    CharBuffer oDecodedChat;
    Charset charset = Charset.forName("utf8");
    CharsetDecoder decoder = charset.newDecoder();

    try {
      oDecodedChat = decoder.decode(oByteBuffer);
      return oDecodedChat.toString();

    } catch (CharacterCodingException ex) {
      ex.printStackTrace();
    }
    return null;
  }
Ejemplo n.º 29
0
 public String decode(ByteBuffer buffer) {
   Charset charset = null;
   CharsetDecoder decoder = null;
   CharBuffer charBuffer = null;
   try {
     charset = Charset.forName("UTF-8");
     decoder = charset.newDecoder();
     charBuffer = decoder.decode(buffer);
     return charBuffer.toString();
   } catch (Exception ex) {
     ex.printStackTrace();
     return "";
   }
 }
Ejemplo n.º 30
0
 static char[] decode(Charset cs, byte[] ba, int off, int len) {
   // (1)We never cache the "external" cs, the only benefit of creating
   // an additional StringDe/Encoder object to wrap it is to share the
   // de/encode() method. These SD/E objects are short-lifed, the young-gen
   // gc should be able to take care of them well. But the best approash
   // is still not to generate them if not really necessary.
   // (2)The defensive copy of the input byte/char[] has a big performance
   // impact, as well as the outgoing result byte/char[]. Need to do the
   // optimization check of (sm==null && classLoader0==null) for both.
   // (3)getClass().getClassLoader0() is expensive
   // (4)There might be a timing gap in isTrusted setting. getClassLoader0()
   // is only chcked (and then isTrusted gets set) when (SM==null). It is
   // possible that the SM==null for now but then SM is NOT null later
   // when safeTrim() is invoked...the "safe" way to do is to redundant
   // check (... && (isTrusted || SM == null || getClassLoader0())) in trim
   // but it then can be argued that the SM is null when the opertaion
   // is started...
   CharsetDecoder cd = cs.newDecoder();
   int en = scale(len, cd.maxCharsPerByte());
   char[] ca = new char[en];
   if (len == 0) return ca;
   boolean isTrusted = false;
   if (System.getSecurityManager() != null) {
     if (!(isTrusted = (cs.getClass().getClassLoader0() == null))) {
       ba = Arrays.copyOfRange(ba, off, off + len);
       off = 0;
     }
   }
   cd.onMalformedInput(CodingErrorAction.REPLACE)
       .onUnmappableCharacter(CodingErrorAction.REPLACE)
       .reset();
   if (cd instanceof ArrayDecoder) {
     int clen = ((ArrayDecoder) cd).decode(ba, off, len, ca);
     return safeTrim(ca, clen, cs, isTrusted);
   } else {
     ByteBuffer bb = ByteBuffer.wrap(ba, off, len);
     CharBuffer cb = CharBuffer.wrap(ca);
     try {
       CoderResult cr = cd.decode(bb, cb, true);
       if (!cr.isUnderflow()) cr.throwException();
       cr = cd.flush(cb);
       if (!cr.isUnderflow()) cr.throwException();
     } catch (CharacterCodingException x) {
       // Substitution is always enabled,
       // so this shouldn't happen
       throw new Error(x);
     }
     return safeTrim(ca, cb.position(), cs, isTrusted);
   }
 }