PageRenderTime 105ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/1.2.0/commons/src/main/java/com/orientechnologies/common/serialization/types/OIntegerSerializer.java

http://orient.googlecode.com/
Java | 85 lines | 44 code | 17 blank | 24 comment | 0 complexity | b5765176ff29a8f587a10db8de2a5de1 MD5 | raw file
Possible License(s): Apache-2.0, AGPL-3.0
  1. /*
  2. * Copyright 2010-2012 Luca Garulli (l.garulli--at--orientechnologies.com)
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.orientechnologies.common.serialization.types;
  17. import com.orientechnologies.common.serialization.OBinaryConverter;
  18. import com.orientechnologies.common.serialization.OBinaryConverterFactory;
  19. /**
  20. * Serializer for {@link Integer} type.
  21. *
  22. * @author ibershadskiy <a href="mailto:ibersh20@gmail.com">Ilya Bershadskiy</a>
  23. * @since 17.01.12
  24. */
  25. public class OIntegerSerializer implements OBinarySerializer<Integer> {
  26. private static final OBinaryConverter CONVERTER = OBinaryConverterFactory.getConverter();
  27. public static OIntegerSerializer INSTANCE = new OIntegerSerializer();
  28. public static final byte ID = 8;
  29. /**
  30. * size of int value in bytes
  31. */
  32. public static final int INT_SIZE = 4;
  33. public int getObjectSize(Integer object) {
  34. return INT_SIZE;
  35. }
  36. public void serialize(Integer object, byte[] stream, int startPosition) {
  37. final int value = object;
  38. stream[startPosition] = (byte) ((value >>> 24) & 0xFF);
  39. stream[startPosition + 1] = (byte) ((value >>> 16) & 0xFF);
  40. stream[startPosition + 2] = (byte) ((value >>> 8) & 0xFF);
  41. stream[startPosition + 3] = (byte) ((value >>> 0) & 0xFF);
  42. }
  43. public Integer deserialize(byte[] stream, int startPosition) {
  44. return (stream[startPosition]) << 24 | (0xff & stream[startPosition + 1]) << 16 | (0xff & stream[startPosition + 2]) << 8
  45. | ((0xff & stream[startPosition + 3]));
  46. }
  47. public int getObjectSize(byte[] stream, int startPosition) {
  48. return INT_SIZE;
  49. }
  50. public byte getId() {
  51. return ID;
  52. }
  53. public int getObjectSizeNative(byte[] stream, int startPosition) {
  54. return INT_SIZE;
  55. }
  56. public void serializeNative(Integer object, byte[] stream, int startPosition) {
  57. CONVERTER.putInt(stream, startPosition, object);
  58. }
  59. public Integer deserializeNative(byte[] stream, int startPosition) {
  60. return CONVERTER.getInt(stream, startPosition);
  61. }
  62. public boolean isFixedLength() {
  63. return true;
  64. }
  65. public int getFixedLength() {
  66. return INT_SIZE;
  67. }
  68. }