Android Charset Encode encode(CharBuffer in, CharsetEncoder encoder)

Here you can find the source of encode(CharBuffer in, CharsetEncoder encoder)

Description

encode

License

Apache License

Declaration

private static ByteBuffer encode(CharBuffer in, CharsetEncoder encoder) 

Method Source Code

//package com.java2s;
/*//from   w w  w. j av a 2  s  .co  m
 *    Copyright 2013 TOYAMA Sumio <jun.nama@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * ----
 * getBytes(), encode() and allocateMore() are modifications based on
 * work copyrighted and licensed as follows:
 * 
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.nio.ByteBuffer;
import java.nio.CharBuffer;

import java.nio.charset.CharsetEncoder;
import java.nio.charset.CoderResult;

public class Main {
    private static ByteBuffer encode(CharBuffer in, CharsetEncoder encoder) {
        int length = (int) (in.remaining() * (double) encoder
                .averageBytesPerChar());
        ByteBuffer out = ByteBuffer.allocate(length);

        encoder.reset();
        CoderResult flushResult = null;

        while (flushResult != CoderResult.UNDERFLOW) {
            CoderResult encodeResult = encoder.encode(in, out, true);
            if (encodeResult == CoderResult.OVERFLOW) {
                out = allocateMore(out);
                continue;
            }

            flushResult = encoder.flush(out);
            if (flushResult == CoderResult.OVERFLOW) {
                out = allocateMore(out);
            }
        }

        out.flip();
        return out;
    }

    private static ByteBuffer allocateMore(ByteBuffer output) {
        if (output.capacity() == 0) {
            return ByteBuffer.allocate(1);
        }
        ByteBuffer result = ByteBuffer.allocate(output.capacity() * 2);
        output.flip();
        result.put(output);
        return result;
    }
}