AreWe

Untitled

Mar 12th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. @Override
  2. public byte[] encodeDataFields(List<FieldDefinition> fieldDefinitions, Map<String, Object> fieldValues) {
  3.  
  4. // TODO Exercise 3 add code here
  5.  
  6. ArrayList<byte[]> turboGazariOOD = new ArrayList<byte[]>();
  7.  
  8. for(int i=0; i<fieldDefinitions.size(); i++) {
  9. if(fieldDefinitions.get(i).getType().getByteLength()!=-1) {
  10.  
  11. ByteBuffer byteBuffer = ByteBuffer.allocate(fieldDefinitions.get(i).getType().getByteLength());
  12. Object currentFieldValue = fieldValues.get(fieldDefinitions.get(i).getName());
  13. switch(fieldDefinitions.get(i).getType()) {
  14. case Byte:
  15. turboGazariOOD.add(byteBuffer.put((byte) currentFieldValue).array());
  16. break;
  17. case Short:
  18. turboGazariOOD.add(byteBuffer.putShort((short) currentFieldValue).array());
  19. break;
  20. case Int:
  21. turboGazariOOD.add(byteBuffer.putInt((int) currentFieldValue).array());
  22. break;
  23. case Long:
  24. turboGazariOOD.add(byteBuffer.putLong((long) currentFieldValue).array());
  25. break;
  26. case Float:
  27. turboGazariOOD.add(byteBuffer.putFloat((float) currentFieldValue).array());
  28. break;
  29. case Double:
  30. turboGazariOOD.add(byteBuffer.putDouble((double) currentFieldValue).array());
  31. break;
  32. }
  33.  
  34. }
  35. }
  36.  
  37. int length = 0;
  38. for(byte[] b : turboGazariOOD) length+=b.length;
  39.  
  40. if(length!=0)
  41. {
  42. byte[] masivche = new byte[length];
  43. int counter = 0;
  44. for(byte[] b : turboGazariOOD) {
  45. for(int i=0; i<b.length; i++) {
  46. masivche[counter++] = b[i];
  47. }
  48. }
  49. return masivche;
  50. }
  51. else
  52. {
  53. return null;
  54. }
  55. }
Add Comment
Please, Sign In to add comment