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.healthmarketscience.jackcess.Column.java

/**
 * @param text Text to encode//w w w .  j ava  2  s.c  o  m
 * @param charset database charset
 * @return A buffer with the text encoded
 * @usage _advanced_method_
 */
public static ByteBuffer encodeUncompressedText(CharSequence text, Charset charset) {
    CharBuffer cb = ((text instanceof CharBuffer) ? (CharBuffer) text : CharBuffer.wrap(text));
    return charset.encode(cb);
}

From source file:com.silentcircle.silenttext.application.SilentTextApplication.java

public SecureFileRepository<User> getUsers() {

    if (users == null) {

        File baseDirectory = getFilesDir("users");

        RepositoryHelper<User> helper = new RepositoryHelper<User>(new Serializer<User>() {

            @Override//from w  w  w.j a  v  a  2s.c o  m
            public User read(DataInputStream in) throws IOException {
                return BasicUser.from(in);
            }

            @Override
            public User read(DataInputStream in, User out) throws IOException {
                if (out instanceof BasicUser) {
                    ((BasicUser) out).load(in);
                    return out;
                }
                return BasicUser.from(in);
            }

            @Override
            public User write(User in, DataOutputStream out) throws IOException {
                if (in instanceof BasicUser) {
                    ((BasicUser) in).save(out);
                }
                return in;
            }

        }) {

            @Override
            public char[] identify(User user) {
                return CryptoUtils.toCharArraySafe(CharBuffer.wrap(user.getID()));
            }

        };
        BufferedBlockCipherFactory cipherFactory = new AESWithCBCAndPKCS7PaddingCipherFactory();
        MacFactory macFactory = new HMacSHA256Factory();
        users = new SecureFileRepository<User>(baseDirectory, helper, cipherFactory, macFactory);
    }
    if (applicationKey != null) {
        users.unlock(applicationKey);
    } else {
        users.lock();
    }
    return users;
}

From source file:com.netscape.cmsutil.crypto.CryptoUtil.java

public static byte[] charsToBytes(char[] chars) {
    if (chars == null)
        return null;

    Charset charset = Charset.forName("UTF-8");
    ByteBuffer byteBuffer = charset.encode(CharBuffer.wrap(chars));
    byte[] result = Arrays.copyOf(byteBuffer.array(), byteBuffer.limit());

    if (byteBuffer.hasArray()) {
        byte[] contentsToBeErased = byteBuffer.array();
        CryptoUtil.obscureBytes(contentsToBeErased, "random");
    }// w  ww. ja v a 2 s .  c om
    return result;
}

From source file:com.viettel.hqmc.DAO.FilesDAO.java

/**
 * lay barcode//from  ww  w  .ja v a  2  s .  co m
 *
 * @return
 */
public void getBarcode(FilesForm form) {

    FilesDAOHE filehe = new FilesDAOHE();
    Files filesqr = filehe.findById(form.getFileId());
    Long announcementReceiptPaperId = 0L;
    Long confirmAnnouncementPaperId = 0L;
    Long confirmImportSatistPaperId = 0L;
    if (filesqr != null) {
        announcementReceiptPaperId = filesqr.getAnnouncementReceiptPaperId();
        confirmAnnouncementPaperId = filesqr.getConfirmAnnouncementPaperId();
        confirmImportSatistPaperId = filesqr.getConfirmImportSatistPaperId();
    }
    String barcodeInfo = "";
    if (announcementReceiptPaperId != null && announcementReceiptPaperId > 0L) {
        AnnouncementReceiptPaperDAOHE announcementReceiptPaperDaohe = new AnnouncementReceiptPaperDAOHE();
        AnnouncementReceiptPaper announcementReceiptPaper = announcementReceiptPaperDaohe
                .findById(announcementReceiptPaperId);
        if (announcementReceiptPaper != null) {
            if (announcementReceiptPaper.getProductName() != null) {
                barcodeInfo += "Tn sn phm: " + announcementReceiptPaper.getProductName();
            } else {
                barcodeInfo += "Tn sn phm: N/A";
            }
            if (announcementReceiptPaper.getBusinessName() != null) {
                barcodeInfo += "\n?n v ng k: " + announcementReceiptPaper.getBusinessName();
            } else {
                barcodeInfo += "\n?n v ng k: N/A";
            }
            if (announcementReceiptPaper.getManufactureName() != null) {
                barcodeInfo += "\nTn nh sn xut: " + announcementReceiptPaper.getManufactureName();
            } else {
                barcodeInfo += "\nTn nh sn xut: N/A";
            }
            if (announcementReceiptPaper.getManufactureAdd() != null) {
                barcodeInfo += "\n?a ch: " + announcementReceiptPaper.getManufactureAdd();
            } else {
                barcodeInfo += "\n?a ch: N/A";
            }
            if (announcementReceiptPaper.getNationName() != null) {
                barcodeInfo += "\nTn nc xut x: " + announcementReceiptPaper.getNationName();
            } else {
                //barcodeInfo += "\nTn nc xut x: N/A";
            }
            //                if (announcementReceiptPaper.getMatchingTarget() != null) {
            //                    barcodeInfo += "\nS qui chun KT/Q?ATTP: " + announcementReceiptPaper.getMatchingTarget();
            //                } else {
            //                    barcodeInfo += "\nS qui chun KT/Q?ATTP: N/A";
            //                }
            if (announcementReceiptPaper.getReceiptDeptName() != null) {
                barcodeInfo += "\n?n vi cp chng nhn: "
                        + announcementReceiptPaper.getReceiptDeptName();
            } else {
                barcodeInfo += "\n?n vi cp chng nhn: N/A";
            }
            if (announcementReceiptPaper.getReceiptNo() != null) {
                barcodeInfo += "\nS cng b: " + announcementReceiptPaper.getReceiptNo();
            } else {
                barcodeInfo += "\nS cng b: N/A";
            }
            SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
            if (announcementReceiptPaper.getEffectiveDate() != null) {
                barcodeInfo += "\nNgy cp: " + formatter.format(announcementReceiptPaper.getReceiptDate());
            } else {
                barcodeInfo += "\nNgy cp: N/A";
            }
        } else {
            barcodeInfo = "Sn phm hin ti cha c thng tin";
        }
    } else if (confirmAnnouncementPaperId != null && confirmAnnouncementPaperId > 0L) {
        //            ConfirmAnnouncementPaperDAOHE confirmAnnouncementPaperDaohe = new ConfirmAnnouncementPaperDAOHE();
        //            ConfirmAnnouncementPaper confirmAnnouncementPaper = confirmAnnouncementPaperDaohe.findById(confirmAnnouncementPaperId);
        barcodeInfo = "Sn phm hin ti cha c thng tin";
    } else if (confirmImportSatistPaperId != null && confirmImportSatistPaperId > 0L) {
        //            ConfirmImportSatistPaperDAOHE confirmImportSatistPaperDaohe = new ConfirmImportSatistPaperDAOHE();
        //            ConfirmImportSatistPaper confirmImportSatistPaper = confirmImportSatistPaperDaohe.findById(announcementReceiptPaperId);
        barcodeInfo = "Sn phm hin ti cha c thng tin";
    } else {
        barcodeInfo = "Sn phm hin ti cha c thng tin";
    }
    //        byte[] bAvatar = filebo1.getQrCode();
    //
    //        try {
    //            FileOutputStream fos = new FileOutputStream("C:\\QR_Code.PNG");
    //            fos.write(bAvatar);
    //            fos.close();
    //        } catch (IOException e) {
    //            e.printStackTrace();
    //        }
    ResourceBundle rb = ResourceBundle.getBundle("config");
    String dir = rb.getString("directoryQR");
    File folderExisting = new File(dir);
    if (!folderExisting.isDirectory()) {
        folderExisting.mkdir();
    }
    if (folderExisting.isDirectory()) {//tao folder theo ngay thang
        File temp = new File(dir);
        if (!temp.isDirectory()) {
            temp.mkdirs();
        }
    }
    Charset charset = Charset.forName("UTF-8");
    CharsetEncoder encoder = charset.newEncoder();
    byte[] b = null;
    try {// Convert a string to UTF-8 bytes in a ByteBuffer
        ByteBuffer bbuf = encoder.encode(CharBuffer.wrap(barcodeInfo));
        b = bbuf.array();
    } catch (CharacterCodingException ex) {
        LogUtil.addLog(ex);//binhnt sonar a160901
    }

    String data;
    String fName = dir + File.separatorChar + filesqr.getFileCode() + "_QRCode.PNG";
    try {
        data = new String(b, "UTF-8");
        // get a byte matrix for the data
        BitMatrix matrix = null;
        int h = 200;
        int w = 200;
        Writer writer = new MultiFormatWriter();
        try {
            Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(2);
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            matrix = writer.encode(data, BarcodeFormat.QR_CODE, w, h, hints);
        } catch (com.google.zxing.WriterException ex) {
            LogUtil.addLog(ex);//binhnt sonar a160901
        }
        // change this path to match yours (this is my mac home folder, you can use: c:\\qr_png.png if you are on windows)
        File file = new File(fName);
        try {
            MatrixToImageWriter.writeToFile(matrix, "PNG", file);//System.out.println("printing to " + file.getAbsolutePath());
        } catch (IOException ex) {
            LogUtil.addLog(ex);//binhnt sonar a160901
        }
    } catch (UnsupportedEncodingException ex) {
        LogUtil.addLog(ex);//binhnt sonar a160901
    }
    //save image into database
    File file = new File(fName);
    byte[] bFile = new byte[(int) file.length()];
    try {
        FileInputStream fileInputStream = new FileInputStream(file);
        //convert file into array of bytes
        fileInputStream.read(bFile);
        fileInputStream.close();
    } catch (IOException ex) {
        LogUtil.addLog(ex);//binhnt sonar a160901
        Logger.getLogger(FilesDAO.class.getName()).log(Level.SEVERE, null, ex);
    }
    Files filebo = filehe.findById(form.getFileId());
    filebo.setQrCode(bFile);
    getSession().update(filebo);
    file.deleteOnExit();
}