Question: Please help me implement this method! UTF 8 . java doesn't need to fixed but the methods I wrote do need to be used with

Please help me implement this method! UTF8.java doesn't need to fixed but the methods I wrote do need to be used with the method I need help on. Thanks!
UTF8.java:
import java.util.Arrays;
public class UTF8{
public static void main(String[] args){
System.out.println(testByteCount(false));
System.out.println(testToBytes(false));
}
/**
* Returns the number of bytes that are used
* to encode a code point when using UTF-8.
*
* @param codePoint
* @return the number of bytes that encode codePoint in UTF-8
*/
public static int byteCount(int codePoint){
if (codePoint <0){
return 0;
}
// Four cases which return 1,2,3, and 4, respectively...
else if (codePoint <=0x7F){
return 1;
} else if (codePoint <=0x7FF){
return 2;
} else if (codePoint <=0xFFFF){
return 3;
} else if (codePoint <=0x10FFFF){
return 4;
} else {
return 0; // For large code points which cannot be encoded.
}
}
/**
* Returns the bytes that are used
* to encode a code point when using UTF-8.
*
* @param codePoint the code point to encode
* @return the bytes that encode codePoint in UTF-8
*/
public static byte[] toBytes(int codePoint){
// Calculates the number of bytes required
int count = byteCount(codePoint);
byte[] b = new byte[count];
// If the code point only requires one byte, we can store the code point directly into the byte array b
if (count ==1){
b[0]=(byte) codePoint;
}
// If the code point requires more than one byte, we will iteratively fill the byte array b
else {
for (int i = count -1; i >0; i--){
b[i]=(byte)(0x80|(codePoint & 0x3F));
codePoint >>=6;
}
// Since we are dealing with 4 cases...
switch (count){
case 2:
b[0]=(byte)(0xC0| codePoint);
break;
case 3:
b[0]=(byte)(0xE0| codePoint);
break;
case 4:
b[0]=(byte)(0xF0| codePoint);
break;
}
}
return b;
}
Method I need help on:
/*
There's a method called Help.UTF16BE_to_UTF8. Its signature is
public static byte[] UTF16BE_to_UTF8(byte[] bytes).
It converts the bytes used to encode a collection of unicode characters in UTF-16BE
to the bytes used to encode the same collection of unicode characters un UTF-8.
It uses the methods you defined in UTF8.java. Therefore, to perform correctly,
it requires you to have completed the first part of the HW.
. Use this method to make 'bytes' reference a new array of bytes ready for writing.
. Open an output filestream. If a FileNotFoundException is thrown,
say to the user... "{output filename} could not be opened for writing.
Make sure that you did not specify the name of a directory."
*/
/**
* Converts the bytes used to encode a collection of Unicode characters in UTF-16BE to the bytes used to encode the same collection of Unicode characters in UTF-8.
*
* @param bytes The bytes to convert.
* @return The converted bytes.
*/
public static byte[] UTF16BE_to_UTF8(byte[] bytes){
// TODO: Implement this method using the methods you defined in UTF8.java
// Hint: You can use a ByteBuffer to convert the bytes from UTF-16BE to UTF-16LE, then from UTF-16LE to UTF-8.
// Make sure to handle the case where the input bytes are not a valid UTF-16BE encoded file.
throw new UnsupportedOperationException("Not implemented yet");
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!