Back to project page PasswordDroid.
The source code is released under:
GNU General Public License
If you think the Android project PasswordDroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package de.wuthoehle.passworddroid.service.model.entries; /*from ww w . j av a 2 s . c o m*/ /* Copyright (c) 2015 Marco Huenseler <marcoh.huenseler+git@gmail.com> * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ import android.util.Log; import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; import java.util.LinkedList; import de.wuthoehle.passworddroid.crypto.Util; import de.wuthoehle.passworddroid.service.model.FileFormat; public class WriteVisitor implements EntryVisitor { private LinkedList<Byte> to_write = new LinkedList<>(); @Override public void visit(Countable countable) { // Countable: // 1 Byte ID | 4 Bytes counter to_write.add(FileFormat.ID_COUNTABLE); to_write.addAll(Util.byteArrayToList( ByteBuffer.allocate(4).putInt(countable.getCounter()).array() )); } @Override public void visit(Customizable customizable) { // Customizable: // 1 Byte ID | 1 Byte charmask | 4 Bytes size-> | x Bytes UTF-8 additional characters | 4 Bytes Length to_write.add(FileFormat.ID_CUSTOMIZABLE); to_write.add(customizable.getMask()); // Get the additional characters byte[] add_characters = {}; try { add_characters = customizable.getAdditionalCharacters().getBytes("UTF-8"); } catch (UnsupportedEncodingException e) { Log.d("WriteVisitor", "Unable to encode character String to UTF-8!"); e.printStackTrace(); } to_write.addAll(Util.byteArrayToList( ByteBuffer.allocate(4).putInt(add_characters.length).array() )); to_write.addAll(Util.byteArrayToList(add_characters)); to_write.addAll(Util.byteArrayToList( ByteBuffer.allocate(4).putInt(customizable.getLength()).array() )); } @Override public void visit(Commentable commentable) { // Commentable: // 1 Byte ID | 4 Bytes size-> | x Bytes UTF-8 Comment to_write.add(FileFormat.ID_COMMENTABLE); byte[] comment = {}; try { comment = commentable.getComment().getBytes("UTF-8"); } catch (UnsupportedEncodingException e) { Log.d("WriteVisitor", "Unable to encode comment String to UTF-8!"); e.printStackTrace(); } to_write.addAll(Util.byteArrayToList( ByteBuffer.allocate(4).putInt(comment.length).array() )); to_write.addAll(Util.byteArrayToList(comment)); } @Override public void visit(Saltable saltable) { // Saltable: // 1 Byte ID | 4 Byte salts count-> | x*[4 Bytes size-> | y Bytes UTF-8 Salt] to_write.add(FileFormat.ID_SALTABLE); to_write.addAll(Util.byteArrayToList( ByteBuffer.allocate(4).putInt(saltable.size()).array() )); for (String i : saltable) { byte[] salt = {}; try { salt = i.getBytes("UTF-8"); } catch (UnsupportedEncodingException e) { Log.d("WriteVisitor", "Unable to encode salt String to UTF-8!"); e.printStackTrace(); } to_write.addAll(Util.byteArrayToList( ByteBuffer.allocate(4).putInt(salt.length).array() )); to_write.addAll(Util.byteArrayToList(salt)); } } @Override public void visit(Entry entry) { // Entry: // 1 Byte ID | 4 Bytes size-> | x Bytes UTF-8 Service Name | 4 Bytes size-> | x Bytes UTF-8 User Name | 4 Bytes element count to_write.add(FileFormat.ID_ENTRY); byte[] service_name = {}; byte[] user_name = {}; try { service_name = entry.getName().getBytes("UTF-8"); user_name = entry.getUser().getBytes("UTF-8"); } catch (UnsupportedEncodingException e) { Log.d("WriteVisitor", "Unable to encode entry String to UTF-8!"); e.printStackTrace(); } to_write.addAll(Util.byteArrayToList( ByteBuffer.allocate(4).putInt(service_name.length).array() )); to_write.addAll(Util.byteArrayToList(service_name)); to_write.addAll(Util.byteArrayToList( ByteBuffer.allocate(4).putInt(user_name.length).array() )); to_write.addAll(Util.byteArrayToList(user_name)); to_write.addAll(Util.byteArrayToList( ByteBuffer.allocate(4).putInt(entry.getElements().size()).array() )); } public LinkedList<Byte> getWriteList() { return to_write; } }