2

I am trying find an alternative for a byteBuffer.putShort method. I want same functionality of putshort. So we have any? My jdk (1.4.2) does not support Byte buffer.

Regards Sailaja

sailaja kasanur
  • 87
  • 1
  • 2
  • 11
  • 4
    Is there a reason you're using a version of the JDK that is so out of date? It reached EOL in [October 2008](http://www.oracle.com/technetwork/java/javase/index-jsp-138567.html) and requiring users to have an equivalent runtime would be a massive security issue. – Phylogenesis Mar 26 '15 at 13:21
  • who is "we"? Why are you using primeval versions of the JDK? – specializt Mar 26 '15 at 13:27
  • `ByteBuffer.putShort()` doesn't do anything sophisticated. Given a buffer to write into, it ought to be simple to implement from scratch. Could you explain the problem that you're having in more detail? – Kenster Mar 26 '15 at 13:33
  • This works for me :http://stackoverflow.com/questions/2188660/convert-short-to-byte-in-java – sailaja kasanur Mar 27 '15 at 07:37
  • @Phylogenesis. Its our project restriction we are yet to update to Jdk 1.7 – sailaja kasanur Mar 27 '15 at 07:38
  • Thanks all for your comments – sailaja kasanur Mar 27 '15 at 07:38
  • And yet, Java 7 [reaches EOL next month](https://www.java.com/en/download/faq/java_7.xml) (April 2015). You shouldn't be looking to update to 1.7, you should be looking at 1.8 – Phylogenesis Mar 27 '15 at 08:43

1 Answers1

1

putShort() does nothing special. The ByteBuffer has information on whether its BigEndian or LittleEndian, - unsure whether your problem needs to consider this. If you do, you need to flip the bytes. Otherwise, it just stores to a backing byte array, and splits the short value into bytes like: byte1 = (byte)(myShort >> 8); byte2 = (byte)(myShort >> 0);

Terje
  • 1,753
  • 10
  • 13