Example usage for java.nio CharBuffer wrap

List of usage examples for java.nio CharBuffer wrap

Introduction

In this page you can find the example usage for java.nio CharBuffer wrap.

Prototype

public static CharBuffer wrap(CharSequence chseq) 

Source Link

Document

Creates a new char buffer by wrapping the given char sequence.

Usage

From source file:com.github.kubakaszycki.jkhttpd.api.FileURLHandler.java

@Override
public void handle(String res, HttpRequest req, HttpResponse resp)
        throws IOException, IllegalArgumentException {
    File relativeFile = new File(baseDir, res);
    if (!relativeFile.exists()) {
        resp.setCode(404);//from   w ww  .  j a va 2  s  .c  om
        resp.setCodeString();
        return;
    }
    if (relativeFile.isDirectory()) {
        resp.setContents(CharBuffer.wrap(DirectoryListing.doDirectoryListing(relativeFile, res)));
    } else {
        resp.setContents(
                Charset.defaultCharset().decode(ByteBuffer.wrap(FileUtils.readFileToByteArray(relativeFile))));
    }
    resp.setCode(200);
    resp.setCodeString();
}

From source file:com.silverpeas.ical.StringUtils.java

static byte[] encodeArray(char[] chars, Charset encoding) throws CharacterCodingException {
    if (CharEncoding.US_ASCII.equals(encoding.name())) {
        byte[] array = new byte[chars.length];
        for (int i = 0; i < array.length; i++) {
            array[i] = (byte) chars[i];
        }//from w  w w. j av  a  2  s . co m
        return array;
    }
    ByteBuffer buffer = encoding.newEncoder().encode(CharBuffer.wrap(chars));
    byte[] array = new byte[buffer.limit()];
    System.arraycopy(buffer.array(), 0, array, 0, array.length);
    return array;
}

From source file:Com.Operaciones.java

public static InputStream generateBarras(String text, int h, int w) throws Exception {

    Charset charset = Charset.forName("ISO-8859-1");
    CharsetEncoder encoder = charset.newEncoder();
    byte[] b = null;
    ByteBuffer bbuf = encoder.encode(CharBuffer.wrap(text));
    b = bbuf.array();//from ww w  . j a v  a  2  s.  c om
    String data = new String(b, "ISO-8859-1");
    // get a byte matrix for the data

    BitMatrix matrix = null;

    matrix = new Code128Writer().encode(data, BarcodeFormat.CODE_128, w, h, null);

    int height = matrix.getHeight();
    int width = matrix.getWidth();
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
        }
    }

    byte[] imgByte = ChartUtilities.encodeAsPNG(image);

    InputStream myInputStream = new ByteArrayInputStream(imgByte);

    return myInputStream;

}

From source file:com.googlecode.jcimd.TextMessageUserDataFactory.java

private static byte[] encodeAs(Charset charset, String textMessage) {
    CharsetEncoder encoder = charset.newEncoder();
    ByteBuffer byteBuffer = ByteBuffer
            .allocate(textMessage.length() * (int) Math.ceil(encoder.maxBytesPerChar()));
    encoder.encode(CharBuffer.wrap(textMessage), byteBuffer, true);
    byte[] bytes = new byte[byteBuffer.position()];
    byteBuffer.flip();// ww  w.  j a va  2 s. c o  m
    byteBuffer.get(bytes);
    return bytes;
}

From source file:io.alicorn.server.http.LoginEndpoint.java

public static String hash(char[] chars) {
    //Parse chars into bytes for hashing.
    CharBuffer charBuffer = CharBuffer.wrap(chars);
    ByteBuffer byteBuffer = charset.encode(charBuffer);
    byte[] bytes = Arrays.copyOfRange(byteBuffer.array(), byteBuffer.position(), byteBuffer.limit());

    //Clear temporary arrays of any data.
    Arrays.fill(charBuffer.array(), '\u0000');
    Arrays.fill(byteBuffer.array(), (byte) 0);

    //Generate the SHA-256 hash.
    String hash = hash(bytes);//  www  . j a v a  2 s  .  c  o  m

    //Clear remaining arrays of any data.
    Arrays.fill(bytes, (byte) 0);

    return hash;
}

From source file:org.opencb.hpg.bigdata.core.utils.VariantContextBlockIteratorTest.java

/**
 * @throws java.lang.Exception//from   ww w.  ja  va 2s.c  o m
 */
@Before
public void setUp() throws Exception {
    rows = new LinkedList<CharBuffer>();
    String inFile = "/" + this.getClass().getName().replaceAll("\\.", "/") + ".vcf";
    FullVcfCodec codec = new FullVcfCodec();
    try (InputStream in = getStream(inFile)) {
        LineIterator iter = codec.makeSourceFromStream(in);
        this.header = (VCFHeader) codec.readActualHeader(iter);
        this.version = codec.getVCFHeaderVersion();

    }
    try (InputStream in = getStream(inFile)) {
        List<String> lines = IOUtils.readLines(in);
        for (String l : lines) {
            if (!l.startsWith("#")) {
                rows.add(CharBuffer.wrap(l));
            }
        }
    }
    //      rows.add(CharBuffer.wrap("20   60479   rs149529999   C   T    100   PASS   ERATE=0.0005;RSQ=0.8951;LDAF=0.0021;   GT:DS:GL   0|0:0.000:-0.19,-0.46,-2.68   0|0:0.000:-0.01,-1.85,-5.00"));
}

From source file:org.itstechupnorth.walrus.text.wiki.micro.MicroWikiPullParser.java

public MicroWikiPullParser(String string) {
    this(CharBuffer.wrap(string).asReadOnlyBuffer());
}

From source file:pl.psnc.synat.wrdz.zu.authentication.AuthenticationHelper.java

/**
 * Hashes the user's password with the salt.
 * //w  w  w.  j  a  va  2s .c  o m
 * @param password
 *            user's password.
 * @param salt
 *            salt for a user
 * @return hashed user's password.
 * @throws CharacterCodingException
 *             when a character encoding error occurs
 */
public char[] hashPassword(char[] password, byte[] salt) throws CharacterCodingException {
    byte[] bytes = ArrayUtils.addAll(
            Charset.forName(configuration.getPasswordCharset()).encode(CharBuffer.wrap(password)).array(),
            salt);
    try {
        MessageDigest messageDigest = MessageDigest.getInstance(configuration.getPasswordDigestAlgorithm());
        messageDigest.reset();
        bytes = messageDigest.digest(bytes);
    } catch (NoSuchAlgorithmException e) {
        throw new WrdzRuntimeException(
                "Unsupported digest algorithm " + configuration.getPasswordDigestAlgorithm());
    }
    if (HEX.equalsIgnoreCase(configuration.getPasswordEncoding())) {
        return hexEncode(bytes);
    } else if (BASE64.equalsIgnoreCase(configuration.getPasswordEncoding())) {
        return base64Encode(bytes);
    } else { // no encoding specified
        return Charset.forName(configuration.getPasswordCharset()).decode(ByteBuffer.wrap(bytes)).array();
    }
}

From source file:org.sonar.api.batch.fs.internal.charhandler.FileHashComputer.java

private void processBuffer() {
    try {/*ww w.  j  a  v  a2 s .  c o m*/
        if (sb.length() > 0) {
            ByteBuffer encoded = encoder.encode(CharBuffer.wrap(sb));
            globalMd5Digest.update(encoded.array(), 0, encoded.limit());
        }
    } catch (CharacterCodingException e) {
        throw new IllegalStateException("Error encoding line hash in file: " + filePath, e);
    }
}

From source file:com.acciente.oacc.sql.internal.StrongCleanablePasswordEncryptor.java

private byte[] getCleanedBytes(char[] password) {
    final ByteBuffer byteBuffer = StandardCharsets.UTF_8
            .encode(CharBuffer.wrap(Normalizer.normalizeToNfc(password)));
    final byte[] byteArray = new byte[byteBuffer.remaining()];
    byteBuffer.get(byteArray);//from   www  . j  av a2 s .c  o  m
    Arrays.fill(byteBuffer.array(), (byte) 0);
    return byteArray;
}