< prev index next >

src/java.net.http/share/classes/jdk/internal/net/http/RequestPublishers.java

Print this page


   1 /*
   2  * Copyright (c) 2016, 2019, 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


 254                 sm.checkPermission(readPermission);
 255                 filePermission = readPermission;
 256             }
 257 
 258             // existence check must be after permission checks
 259             if (Files.notExists(path))
 260                 throw new FileNotFoundException(path + " not found");
 261 
 262             return new FilePublisher(path, filePermission);
 263         }
 264 
 265         private FilePublisher(Path name, FilePermission filePermission) {
 266             assert filePermission != null ? filePermission.getActions().equals("read") : true;
 267             file = name.toFile();
 268             this.filePermissions = filePermission == null ? EMPTY_FILE_PERMISSIONS
 269                     : new FilePermission[] { filePermission };
 270         }
 271 
 272         @Override
 273         public void subscribe(Flow.Subscriber<? super ByteBuffer> subscriber) {
 274             InputStream is = null;
 275             Throwable t = null;
 276             if (System.getSecurityManager() == null) {
 277                 try {
 278                     is = new FileInputStream(file);
 279                 } catch (IOException ioe) {
 280                     t = ioe;
 281                 }
 282             } else {
 283                 try {
 284                     PrivilegedExceptionAction<FileInputStream> pa =
 285                             () -> new FileInputStream(file);
 286                     is = AccessController.doPrivileged(pa, null, filePermissions);
 287                 } catch (PrivilegedActionException pae) {
 288                     t = pae.getCause();
 289                 }
 290             }
 291             final InputStream fis = is;
 292             PullPublisher<ByteBuffer> publisher;
 293             if (t == null) {
 294                 publisher = new PullPublisher<>(() -> new StreamIterator(fis));
 295             } else {
 296                 publisher = new PullPublisher<>(null, t);
 297             }
 298             publisher.subscribe(subscriber);
 299         }
 300 
 301         @Override
 302         public long contentLength() {
 303             if (System.getSecurityManager() == null) {
 304                 return file.length();
 305             } else {
 306                 PrivilegedAction<Long> pa = () -> file.length();
 307                 return AccessController.doPrivileged(pa, null, filePermissions);
 308             }
 309         }
 310     }
 311 
 312     /**
 313      * Reads one buffer ahead all the time, blocking in hasNext()
 314      */
 315     public static class StreamIterator implements Iterator<ByteBuffer> {
 316         final InputStream is;
 317         final Supplier<? extends ByteBuffer> bufSupplier;


   1 /*
   2  * Copyright (c) 2016, 2018, 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


 254                 sm.checkPermission(readPermission);
 255                 filePermission = readPermission;
 256             }
 257 
 258             // existence check must be after permission checks
 259             if (Files.notExists(path))
 260                 throw new FileNotFoundException(path + " not found");
 261 
 262             return new FilePublisher(path, filePermission);
 263         }
 264 
 265         private FilePublisher(Path name, FilePermission filePermission) {
 266             assert filePermission != null ? filePermission.getActions().equals("read") : true;
 267             file = name.toFile();
 268             this.filePermissions = filePermission == null ? EMPTY_FILE_PERMISSIONS
 269                     : new FilePermission[] { filePermission };
 270         }
 271 
 272         @Override
 273         public void subscribe(Flow.Subscriber<? super ByteBuffer> subscriber) {
 274             InputStream is;

 275             if (System.getSecurityManager() == null) {
 276                 try {
 277                     is = new FileInputStream(file);
 278                 } catch (IOException ioe) {
 279                     throw new UncheckedIOException(ioe);
 280                 }
 281             } else {
 282                 try {
 283                     PrivilegedExceptionAction<FileInputStream> pa =
 284                             () -> new FileInputStream(file);
 285                     is = AccessController.doPrivileged(pa, null, filePermissions);
 286                 } catch (PrivilegedActionException pae) {
 287                     throw new UncheckedIOException((IOException) pae.getCause());
 288                 }
 289             }
 290             PullPublisher<ByteBuffer> publisher =
 291                     new PullPublisher<>(() -> new StreamIterator(is));





 292             publisher.subscribe(subscriber);
 293         }
 294 
 295         @Override
 296         public long contentLength() {
 297             if (System.getSecurityManager() == null) {
 298                 return file.length();
 299             } else {
 300                 PrivilegedAction<Long> pa = () -> file.length();
 301                 return AccessController.doPrivileged(pa, null, filePermissions);
 302             }
 303         }
 304     }
 305 
 306     /**
 307      * Reads one buffer ahead all the time, blocking in hasNext()
 308      */
 309     public static class StreamIterator implements Iterator<ByteBuffer> {
 310         final InputStream is;
 311         final Supplier<? extends ByteBuffer> bufSupplier;


< prev index next >