...
Code Block | ||
---|---|---|
| ||
public boolean sendPage(Socket socket, String pageName) { // No synchronization Page targetPage = getPage(pageName); if (targetPage == null) return false; return deliverPage(socket, targetPage); } private synchronized Page getPage(String pageName) { // Requires synchronization Page targetPage = null; for (Page p : pageBuff) { if (p.getName().equals(pageName)) { targetPage = p; } } return targetPage; } // Return false if an error occurs, true if successful public boolean deliverPage(Socket socket, Page page) { ObjectOutputStream out = null; boolean result = true; try { // Get the output stream to write the Page to out = new ObjectOutputStream(socket.getOutputStream()); // Send the Page to the client out.writeObject(page); } catch (IOException io) { // If recovery is not possible return false return false; result = false; } finally { if (out != null) { try { out.flush(); out.close(); } catch (IOException e) { //result If recovery is not possible return false return = false; } } } return trueresult; } |
Exceptions
EX1: Classes that provide an appropriate termination mechanism to callers are allowed to violate this guideline (see CON24-J. Ensure that threads and tasks performing blocking operations can be terminated).
...