1 /* 2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 import java.io.File; 26 import java.net.InetAddress; 27 28 import sun.misc.IoTraceContext; 29 30 public class IoTraceBase implements IoTraceListener { 31 32 protected static final IoTraceContext my_context = new IoTraceContext() { 33 }; 34 35 private String path; 36 private long bytesRead; 37 private long bytesWritten; 38 private IoTraceContext context; 39 private InetAddress address; 40 private int port; 41 private int timeout; 42 43 protected void clear() { 44 context = null; 45 bytesRead = 0; 46 bytesWritten = 0; 47 address = null; 48 port = 0; 49 timeout = 0; 50 path = null; 51 } 52 53 @Override 54 public IoTraceContext fileWriteBegin(String p) { 55 path = p; 56 return my_context; 57 } 58 59 @Override 60 public void fileWriteEnd(IoTraceContext ctx, long bw) { 61 context = ctx; 62 bytesWritten = bw; 63 } 64 65 @Override 66 public IoTraceContext fileReadBegin(String p) { 67 path = p; 68 return my_context; 69 } 70 71 @Override 72 public void fileReadEnd(IoTraceContext ctx, long br) { 73 context = ctx; 74 bytesRead = br; 75 } 76 77 @Override 78 public IoTraceContext socketReadBegin(InetAddress address, int port, 79 int timeout) { 80 this.address = address; 81 this.port = port; 82 this.timeout = timeout; 83 return my_context; 84 } 85 86 @Override 87 public void socketReadEnd(IoTraceContext context, long bytesRead) { 88 this.context = context; 89 this.bytesRead = bytesRead; 90 } 91 92 @Override 93 public IoTraceContext socketWriteBegin(InetAddress address, int port) { 94 this.address = address; 95 this.port = port; 96 return my_context; 97 } 98 99 @Override 100 public void socketWriteEnd(IoTraceContext context, long bytesWritten) { 101 this.context = context; 102 this.bytesWritten = bytesWritten; 103 } 104 105 protected void expectFileRead(long br, File f) throws Exception { 106 expectFile(0, br, f); 107 } 108 109 protected void expectFileWrite(long bw, File f) throws Exception { 110 expectFile(bw, 0, f); 111 } 112 113 protected void expectFile(long bw, long br, File f) throws Exception { 114 if (context != my_context) { 115 throw new Exception("Wrong context: " + context); 116 } 117 if (bytesWritten != bw) { 118 throw new Exception("Expected " + bw + " byte to be read, got: " 119 + bytesWritten); 120 } 121 if (bytesRead != br) { 122 throw new Exception("Expected " + br + " byte to be read, got: " 123 + bytesWritten); 124 } 125 if (!path.equals(f.getPath())) { 126 throw new Exception("Incorrect path: " + path + ". Expected: " 127 + f.getPath()); 128 } 129 } 130 131 protected void expectSocket(int br, int bw, InetAddress ia, int p, int t) 132 throws Exception { 133 if (context != my_context) { 134 throw new Exception("Wrong context: " + context); 135 } 136 if (bytesWritten != bw) { 137 throw new Exception("Expected " + bw + " byte to be written, got: " 138 + bytesWritten); 139 } 140 if (bytesRead != br) { 141 throw new Exception("Expected " + br + " byte to be read, got: " 142 + bytesWritten); 143 } 144 if (!address.equals(ia)) { 145 throw new Exception("Incorrect address: " + address 146 + ". Expected: " + ia); 147 } 148 if (port != p) { 149 throw new Exception("Expected " + p + " port, got: " + port); 150 } 151 if (timeout != t) { 152 throw new Exception("Expected " + t + " timeout, got: " + timeout); 153 } 154 } 155 }