### Question:
XmlUtil { public static void rewriteEntities(Reader reader, Writer writer) throws IOException { int curChar = -1; StringBuffer ent; char[] entBuf; if (reader == null) { throw new IllegalArgumentException("null reader arg"); } else if (writer == null) { throw new IllegalArgumentException("null writer arg"); } ent = new StringBuffer(50); entBuf = new char[50]; while ((curChar = reader.read()) != -1) { if (curChar == '&') { if (ent.length() > 0) { writer.write(ent.toString()); ent.setLength(0); } ent.append((char) curChar); } else if (curChar == ';' && ent.length() > 0) { int entLen = ent.length(); if (entLen > 1) { if (ent.charAt(1) == '#') { ent.getChars(0, ent.length(), entBuf, 0); writer.write(entBuf, 0, ent.length()); writer.write(';'); } else { Character character = HTMLEntityLookup .getCharacterCode(ent.substring(1)); if (character != null) { writer.write("&#"); writer.write(String.valueOf((int) character .charValue())); writer.write(";"); } else { writer.write(ent.toString()); writer.write(curChar); } } } else { writer.write("&;"); } ent.setLength(0); } else if (ent.length() > 0) { ent.append((char) curChar); } else { writer.write(curChar); } } if (ent.length() > 0) { writer.write(ent.toString()); } }  static boolean isXMLReservedNamespace(String namespace); static void removeEntities(Reader reader, Writer writer); static String removeEntities(String string); static void rewriteEntities(Reader reader, Writer writer); static Document parseStream(InputStream stream, VALIDATION_TYPE validation,
                                       boolean expandEntityRefs); static Document parseStream(InputStream stream,
                                       EntityResolver entityResolver, VALIDATION_TYPE validation,
                                       boolean expandEntityRefs); static Document parseStream(Reader stream,
                                       EntityResolver entityResolver, VALIDATION_TYPE validation,
                                       boolean expandEntityRefs); static Document parseStream(InputStream stream); static Document parseStream(Reader stream); static Document parseStream(Reader stream, final ErrorHandler errorHandler); static NodeList getNodeList(Node node, String xpath); static Node getNode(Node node, String xpath); static String getString(Node node, String xpath); static Element createElement(String localPart); static Element createElementNS(String namespace, String localPart); static String serialize(NodeList nodeList); static String serialize(final Node node, boolean format); static void serialize(final Node node, boolean format, Writer writer); static String serialize(NodeList nodeList, boolean format); static void serialize(NodeList nodeList, boolean format, Writer writer); static String indent(String xml, int indent); static boolean isTextNode(Node node); static void encodeTextValue(char[] characters, int offset, int length, Writer writer); static void encodeAttributeValue(char[] characters, int offset, int length, Writer writer); static final DocumentBuilder documentBuilder; static final char[] LT; static final char[] GT; static final char[] AMP; static final char[] QUOT; static final char[] APOS; }### Answer:
@Test public void testRewriteEntities_01() { try { StringReader reader = new StringReader( "a&yen;a &#171; hh & ; jgha& &amp; gaa &j"); StringWriter writer = new StringWriter(); XmlUtil.rewriteEntities(reader, writer); assertEquals("a&#165;a &#171; hh & ; jgha& &#38; gaa &j", writer .toString()); } catch (Exception excep) { fail("Unhandled exception: " + excep.getMessage()); } }