jdk/src/share/classes/sun/nio/ch/FileChannelImpl.java
changeset 2441 228c040622a2
parent 2057 3acf8e5e2ca0
child 2594 3755ecdb395d
equal deleted inserted replaced
2436:ae4667371301 2441:228c040622a2
   126         ensureOpen();
   126         ensureOpen();
   127         if (!readable)
   127         if (!readable)
   128             throw new NonReadableChannelException();
   128             throw new NonReadableChannelException();
   129         synchronized (positionLock) {
   129         synchronized (positionLock) {
   130             int n = 0;
   130             int n = 0;
   131             int ti = threads.add();
   131             int ti = -1;
   132             try {
   132             try {
   133                 begin();
   133                 begin();
       
   134                 ti = threads.add();
   134                 if (!isOpen())
   135                 if (!isOpen())
   135                     return 0;
   136                     return 0;
   136                 do {
   137                 do {
   137                     n = IOUtil.read(fd, dst, -1, nd, positionLock);
   138                     n = IOUtil.read(fd, dst, -1, nd, positionLock);
   138                 } while ((n == IOStatus.INTERRUPTED) && isOpen());
   139                 } while ((n == IOStatus.INTERRUPTED) && isOpen());
   149         ensureOpen();
   150         ensureOpen();
   150         if (!readable)
   151         if (!readable)
   151             throw new NonReadableChannelException();
   152             throw new NonReadableChannelException();
   152         synchronized (positionLock) {
   153         synchronized (positionLock) {
   153             long n = 0;
   154             long n = 0;
   154             int ti = threads.add();
   155             int ti = -1;
   155             try {
   156             try {
   156                 begin();
   157                 begin();
       
   158                 ti = threads.add();
   157                 if (!isOpen())
   159                 if (!isOpen())
   158                     return 0;
   160                     return 0;
   159                 do {
   161                 do {
   160                     n = IOUtil.read(fd, dsts, nd);
   162                     n = IOUtil.read(fd, dsts, nd);
   161                 } while ((n == IOStatus.INTERRUPTED) && isOpen());
   163                 } while ((n == IOStatus.INTERRUPTED) && isOpen());
   181         ensureOpen();
   183         ensureOpen();
   182         if (!writable)
   184         if (!writable)
   183             throw new NonWritableChannelException();
   185             throw new NonWritableChannelException();
   184         synchronized (positionLock) {
   186         synchronized (positionLock) {
   185             int n = 0;
   187             int n = 0;
   186             int ti = threads.add();
   188             int ti = -1;
   187             try {
   189             try {
   188                 begin();
   190                 begin();
       
   191                 ti = threads.add();
   189                 if (!isOpen())
   192                 if (!isOpen())
   190                     return 0;
   193                     return 0;
   191                 do {
   194                 do {
   192                     n = IOUtil.write(fd, src, -1, nd, positionLock);
   195                     n = IOUtil.write(fd, src, -1, nd, positionLock);
   193                 } while ((n == IOStatus.INTERRUPTED) && isOpen());
   196                 } while ((n == IOStatus.INTERRUPTED) && isOpen());
   204         ensureOpen();
   207         ensureOpen();
   205         if (!writable)
   208         if (!writable)
   206             throw new NonWritableChannelException();
   209             throw new NonWritableChannelException();
   207         synchronized (positionLock) {
   210         synchronized (positionLock) {
   208             long n = 0;
   211             long n = 0;
   209             int ti = threads.add();
   212             int ti = -1;
   210             try {
   213             try {
   211                 begin();
   214                 begin();
       
   215                 ti = threads.add();
   212                 if (!isOpen())
   216                 if (!isOpen())
   213                     return 0;
   217                     return 0;
   214                 do {
   218                 do {
   215                     n = IOUtil.write(fd, srcs, nd);
   219                     n = IOUtil.write(fd, srcs, nd);
   216                 } while ((n == IOStatus.INTERRUPTED) && isOpen());
   220                 } while ((n == IOStatus.INTERRUPTED) && isOpen());
   237 
   241 
   238     public long position() throws IOException {
   242     public long position() throws IOException {
   239         ensureOpen();
   243         ensureOpen();
   240         synchronized (positionLock) {
   244         synchronized (positionLock) {
   241             long p = -1;
   245             long p = -1;
   242             int ti = threads.add();
   246             int ti = -1;
   243             try {
   247             try {
   244                 begin();
   248                 begin();
       
   249                 ti = threads.add();
   245                 if (!isOpen())
   250                 if (!isOpen())
   246                     return 0;
   251                     return 0;
   247                 do {
   252                 do {
   248                     p = position0(fd, -1);
   253                     p = position0(fd, -1);
   249                 } while ((p == IOStatus.INTERRUPTED) && isOpen());
   254                 } while ((p == IOStatus.INTERRUPTED) && isOpen());
   260         ensureOpen();
   265         ensureOpen();
   261         if (newPosition < 0)
   266         if (newPosition < 0)
   262             throw new IllegalArgumentException();
   267             throw new IllegalArgumentException();
   263         synchronized (positionLock) {
   268         synchronized (positionLock) {
   264             long p = -1;
   269             long p = -1;
   265             int ti = threads.add();
   270             int ti = -1;
   266             try {
   271             try {
   267                 begin();
   272                 begin();
       
   273                 ti = threads.add();
   268                 if (!isOpen())
   274                 if (!isOpen())
   269                     return null;
   275                     return null;
   270                 do {
   276                 do {
   271                     p  = position0(fd, newPosition);
   277                     p  = position0(fd, newPosition);
   272                 } while ((p == IOStatus.INTERRUPTED) && isOpen());
   278                 } while ((p == IOStatus.INTERRUPTED) && isOpen());
   281 
   287 
   282     public long size() throws IOException {
   288     public long size() throws IOException {
   283         ensureOpen();
   289         ensureOpen();
   284         synchronized (positionLock) {
   290         synchronized (positionLock) {
   285             long s = -1;
   291             long s = -1;
   286             int ti = threads.add();
   292             int ti = -1;
   287             try {
   293             try {
   288                 begin();
   294                 begin();
       
   295                 ti = threads.add();
   289                 if (!isOpen())
   296                 if (!isOpen())
   290                     return -1;
   297                     return -1;
   291                 do {
   298                 do {
   292                     s = nd.size(fd);
   299                     s = nd.size(fd);
   293                 } while ((s == IOStatus.INTERRUPTED) && isOpen());
   300                 } while ((s == IOStatus.INTERRUPTED) && isOpen());
   309         if (!writable)
   316         if (!writable)
   310             throw new NonWritableChannelException();
   317             throw new NonWritableChannelException();
   311         synchronized (positionLock) {
   318         synchronized (positionLock) {
   312             int rv = -1;
   319             int rv = -1;
   313             long p = -1;
   320             long p = -1;
   314             int ti = threads.add();
   321             int ti = -1;
   315             try {
   322             try {
   316                 begin();
   323                 begin();
       
   324                 ti = threads.add();
   317                 if (!isOpen())
   325                 if (!isOpen())
   318                     return null;
   326                     return null;
   319 
   327 
   320                 // get current position
   328                 // get current position
   321                 do {
   329                 do {
   348     }
   356     }
   349 
   357 
   350     public void force(boolean metaData) throws IOException {
   358     public void force(boolean metaData) throws IOException {
   351         ensureOpen();
   359         ensureOpen();
   352         int rv = -1;
   360         int rv = -1;
   353         int ti = threads.add();
   361         int ti = -1;
   354         try {
   362         try {
   355             begin();
   363             begin();
       
   364             ti = threads.add();
   356             if (!isOpen())
   365             if (!isOpen())
   357                 return;
   366                 return;
   358             do {
   367             do {
   359                 rv = nd.force(fd, metaData);
   368                 rv = nd.force(fd, metaData);
   360             } while ((rv == IOStatus.INTERRUPTED) && isOpen());
   369             } while ((rv == IOStatus.INTERRUPTED) && isOpen());
   404         int targetFDVal = IOUtil.fdVal(targetFD);
   413         int targetFDVal = IOUtil.fdVal(targetFD);
   405         if (thisFDVal == targetFDVal) // Not supported on some configurations
   414         if (thisFDVal == targetFDVal) // Not supported on some configurations
   406             return IOStatus.UNSUPPORTED;
   415             return IOStatus.UNSUPPORTED;
   407 
   416 
   408         long n = -1;
   417         long n = -1;
   409         int ti = threads.add();
   418         int ti = -1;
   410         try {
   419         try {
   411             begin();
   420             begin();
       
   421             ti = threads.add();
   412             if (!isOpen())
   422             if (!isOpen())
   413                 return -1;
   423                 return -1;
   414             do {
   424             do {
   415                 n = transferTo0(thisFDVal, position, icount, targetFDVal);
   425                 n = transferTo0(thisFDVal, position, icount, targetFDVal);
   416             } while ((n == IOStatus.INTERRUPTED) && isOpen());
   426             } while ((n == IOStatus.INTERRUPTED) && isOpen());
   610             throw new IllegalArgumentException("Negative position");
   620             throw new IllegalArgumentException("Negative position");
   611         if (!readable)
   621         if (!readable)
   612             throw new NonReadableChannelException();
   622             throw new NonReadableChannelException();
   613         ensureOpen();
   623         ensureOpen();
   614         int n = 0;
   624         int n = 0;
   615         int ti = threads.add();
   625         int ti = -1;
   616         try {
   626         try {
   617             begin();
   627             begin();
       
   628             ti = threads.add();
   618             if (!isOpen())
   629             if (!isOpen())
   619                 return -1;
   630                 return -1;
   620             do {
   631             do {
   621                 n = IOUtil.read(fd, dst, position, nd, positionLock);
   632                 n = IOUtil.read(fd, dst, position, nd, positionLock);
   622             } while ((n == IOStatus.INTERRUPTED) && isOpen());
   633             } while ((n == IOStatus.INTERRUPTED) && isOpen());
   635             throw new IllegalArgumentException("Negative position");
   646             throw new IllegalArgumentException("Negative position");
   636         if (!writable)
   647         if (!writable)
   637             throw new NonWritableChannelException();
   648             throw new NonWritableChannelException();
   638         ensureOpen();
   649         ensureOpen();
   639         int n = 0;
   650         int n = 0;
   640         int ti = threads.add();
   651         int ti = -1;
   641         try {
   652         try {
   642             begin();
   653             begin();
       
   654             ti = threads.add();
   643             if (!isOpen())
   655             if (!isOpen())
   644                 return -1;
   656                 return -1;
   645             do {
   657             do {
   646                 n = IOUtil.write(fd, src, position, nd, positionLock);
   658                 n = IOUtil.write(fd, src, position, nd, positionLock);
   647             } while ((n == IOStatus.INTERRUPTED) && isOpen());
   659             } while ((n == IOStatus.INTERRUPTED) && isOpen());
   729             throw new NonWritableChannelException();
   741             throw new NonWritableChannelException();
   730         if (!readable)
   742         if (!readable)
   731             throw new NonReadableChannelException();
   743             throw new NonReadableChannelException();
   732 
   744 
   733         long addr = -1;
   745         long addr = -1;
   734         int ti = threads.add();
   746         int ti = -1;
   735         try {
   747         try {
   736             begin();
   748             begin();
       
   749             ti = threads.add();
   737             if (!isOpen())
   750             if (!isOpen())
   738                 return null;
   751                 return null;
   739             if (size() < position + size) { // Extend file size
   752             if (size() < position + size) { // Extend file size
   740                 if (!writable) {
   753                 if (!writable) {
   741                     throw new IOException("Channel not open for writing " +
   754                     throw new IOException("Channel not open for writing " +
   898             throw new NonWritableChannelException();
   911             throw new NonWritableChannelException();
   899         FileLockImpl fli = new FileLockImpl(this, position, size, shared);
   912         FileLockImpl fli = new FileLockImpl(this, position, size, shared);
   900         FileLockTable flt = fileLockTable();
   913         FileLockTable flt = fileLockTable();
   901         flt.add(fli);
   914         flt.add(fli);
   902         boolean i = true;
   915         boolean i = true;
   903         int ti = threads.add();
   916         int ti = -1;
   904         try {
   917         try {
   905             begin();
   918             begin();
       
   919             ti = threads.add();
   906             if (!isOpen())
   920             if (!isOpen())
   907                 return null;
   921                 return null;
   908             int result = nd.lock(fd, true, position, size, shared);
   922             int result = nd.lock(fd, true, position, size, shared);
   909             if (result == FileDispatcher.RET_EX_LOCK) {
   923             if (result == FileDispatcher.RET_EX_LOCK) {
   910                 assert shared;
   924                 assert shared;