Search in sources :

Example 26 with BufferedOutputStream

use of java.io.BufferedOutputStream in project jersey by jersey.

the class JarUtils method createJarFile.

public static File createJarFile(final String name, final Suffix s, final String base, final Map<String, String> entries) throws IOException {
    final File tempJar = File.createTempFile(name, "." + s);
    tempJar.deleteOnExit();
    final JarOutputStream jos = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(tempJar)), new Manifest());
    final Set<String> usedSegments = new HashSet<String>();
    for (final Map.Entry<String, String> entry : entries.entrySet()) {
        for (final String path : getPaths(entry.getValue())) {
            if (usedSegments.contains(path)) {
                continue;
            }
            usedSegments.add(path);
            final JarEntry e = new JarEntry(path);
            jos.putNextEntry(e);
            jos.closeEntry();
        }
        final JarEntry e = new JarEntry(entry.getValue());
        jos.putNextEntry(e);
        final InputStream f = new BufferedInputStream(new FileInputStream(base + entry.getKey()));
        final byte[] buf = new byte[1024];
        int read = 1024;
        while ((read = f.read(buf, 0, read)) != -1) {
            jos.write(buf, 0, read);
        }
        jos.closeEntry();
    }
    jos.close();
    return tempJar;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) JarOutputStream(java.util.jar.JarOutputStream) Manifest(java.util.jar.Manifest) JarEntry(java.util.jar.JarEntry) FileInputStream(java.io.FileInputStream) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 27 with BufferedOutputStream

use of java.io.BufferedOutputStream in project k-9 by k9mail.

the class ImapConnection method setUpStreamsAndParser.

private void setUpStreamsAndParser(InputStream input, OutputStream output) {
    inputStream = new PeekableInputStream(new BufferedInputStream(input, BUFFER_SIZE));
    responseParser = new ImapResponseParser(inputStream);
    outputStream = new BufferedOutputStream(output, BUFFER_SIZE);
}
Also used : BufferedInputStream(java.io.BufferedInputStream) PeekableInputStream(com.fsck.k9.mail.filter.PeekableInputStream) BufferedOutputStream(java.io.BufferedOutputStream)

Example 28 with BufferedOutputStream

use of java.io.BufferedOutputStream in project jna by java-native-access.

the class TlbImp method writeTextFile.

private void writeTextFile(String filename, String str) throws IOException {
    String file = this.comRootDir + File.separator + filename;
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
    bos.write(str.getBytes());
    bos.close();
}
Also used : FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream)

Example 29 with BufferedOutputStream

use of java.io.BufferedOutputStream in project hadoop by apache.

the class TestContainerLogsPage method testContainerLogPageAccess.

@Test(timeout = 10000)
public void testContainerLogPageAccess() throws IOException {
    // SecureIOUtils require Native IO to be enabled. This test will run
    // only if it is enabled.
    assumeTrue(NativeIO.isAvailable());
    String user = "randomUser" + System.currentTimeMillis();
    File absLogDir = null, appDir = null, containerDir = null, syslog = null;
    try {
        // target log directory
        absLogDir = new File("target", TestContainerLogsPage.class.getSimpleName() + "LogDir").getAbsoluteFile();
        absLogDir.mkdir();
        Configuration conf = new Configuration();
        conf.set(YarnConfiguration.NM_LOG_DIRS, absLogDir.toURI().toString());
        conf.set(CommonConfigurationKeysPublic.HADOOP_SECURITY_AUTHENTICATION, "kerberos");
        UserGroupInformation.setConfiguration(conf);
        NodeHealthCheckerService healthChecker = createNodeHealthCheckerService(conf);
        healthChecker.init(conf);
        LocalDirsHandlerService dirsHandler = healthChecker.getDiskHandler();
        // Add an application and the corresponding containers
        RecordFactory recordFactory = RecordFactoryProvider.getRecordFactory(conf);
        long clusterTimeStamp = 1234;
        ApplicationId appId = BuilderUtils.newApplicationId(recordFactory, clusterTimeStamp, 1);
        Application app = mock(Application.class);
        when(app.getAppId()).thenReturn(appId);
        // Making sure that application returns a random user. This is required
        // for SecureIOUtils' file owner check.
        when(app.getUser()).thenReturn(user);
        ApplicationAttemptId appAttemptId = BuilderUtils.newApplicationAttemptId(appId, 1);
        ContainerId container1 = BuilderUtils.newContainerId(recordFactory, appId, appAttemptId, 0);
        // Testing secure read access for log files
        // Creating application and container directory and syslog file.
        appDir = new File(absLogDir, appId.toString());
        appDir.mkdir();
        containerDir = new File(appDir, container1.toString());
        containerDir.mkdir();
        syslog = new File(containerDir, "syslog");
        syslog.createNewFile();
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(syslog));
        out.write("Log file Content".getBytes());
        out.close();
        Context context = mock(Context.class);
        ConcurrentMap<ApplicationId, Application> appMap = new ConcurrentHashMap<ApplicationId, Application>();
        appMap.put(appId, app);
        when(context.getApplications()).thenReturn(appMap);
        ConcurrentHashMap<ContainerId, Container> containers = new ConcurrentHashMap<ContainerId, Container>();
        when(context.getContainers()).thenReturn(containers);
        when(context.getLocalDirsHandler()).thenReturn(dirsHandler);
        MockContainer container = new MockContainer(appAttemptId, new AsyncDispatcher(), conf, user, appId, 1);
        container.setState(ContainerState.RUNNING);
        context.getContainers().put(container1, container);
        ContainersLogsBlock cLogsBlock = new ContainersLogsBlock(context);
        Map<String, String> params = new HashMap<String, String>();
        params.put(YarnWebParams.CONTAINER_ID, container1.toString());
        params.put(YarnWebParams.CONTAINER_LOG_TYPE, "syslog");
        Injector injector = WebAppTests.testPage(ContainerLogsPage.class, ContainersLogsBlock.class, cLogsBlock, params, (Module[]) null);
        PrintWriter spyPw = WebAppTests.getPrintWriter(injector);
        verify(spyPw).write("Exception reading log file. Application submitted by '" + user + "' doesn't own requested log file : syslog");
    } finally {
        if (syslog != null) {
            syslog.delete();
        }
        if (containerDir != null) {
            containerDir.delete();
        }
        if (appDir != null) {
            appDir.delete();
        }
        if (absLogDir != null) {
            absLogDir.delete();
        }
    }
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) YarnConfiguration(org.apache.hadoop.yarn.conf.YarnConfiguration) NodeHealthCheckerService(org.apache.hadoop.yarn.server.nodemanager.NodeHealthCheckerService) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Container(org.apache.hadoop.yarn.server.nodemanager.containermanager.container.Container) ContainerId(org.apache.hadoop.yarn.api.records.ContainerId) Injector(com.google.inject.Injector) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) BufferedOutputStream(java.io.BufferedOutputStream) PrintWriter(java.io.PrintWriter) NMContext(org.apache.hadoop.yarn.server.nodemanager.NodeManager.NMContext) Context(org.apache.hadoop.yarn.server.nodemanager.Context) ContainersLogsBlock(org.apache.hadoop.yarn.server.nodemanager.webapp.ContainerLogsPage.ContainersLogsBlock) ApplicationAttemptId(org.apache.hadoop.yarn.api.records.ApplicationAttemptId) LocalDirsHandlerService(org.apache.hadoop.yarn.server.nodemanager.LocalDirsHandlerService) RecordFactory(org.apache.hadoop.yarn.factories.RecordFactory) AsyncDispatcher(org.apache.hadoop.yarn.event.AsyncDispatcher) FileOutputStream(java.io.FileOutputStream) ApplicationId(org.apache.hadoop.yarn.api.records.ApplicationId) Module(com.google.inject.Module) File(java.io.File) Application(org.apache.hadoop.yarn.server.nodemanager.containermanager.application.Application) Test(org.junit.Test)

Example 30 with BufferedOutputStream

use of java.io.BufferedOutputStream in project hadoop by apache.

the class SwiftNativeOutputStream method partUpload.

/**
   * Upload a single partition. This deletes the local backing-file,
   * and re-opens it to create a new one.
   * @param closingUpload is this the final upload of an upload
   * @throws IOException on IO problems
   */
@SuppressWarnings("IOResourceOpenedButNotSafelyClosed")
private void partUpload(boolean closingUpload) throws IOException {
    if (backupStream != null) {
        backupStream.close();
    }
    if (closingUpload && partUpload && backupFile.length() == 0) {
        //skipping the upload if
        // - it is close time
        // - the final partition is 0 bytes long
        // - one part has already been written
        SwiftUtils.debug(LOG, "skipping upload of 0 byte final partition");
        delete(backupFile);
    } else {
        partUpload = true;
        boolean uploadSuccess = false;
        int attempt = 0;
        while (!uploadSuccess) {
            try {
                ++attempt;
                bytesUploaded += uploadFilePartAttempt(attempt);
                uploadSuccess = true;
            } catch (IOException e) {
                LOG.info("Upload failed " + e, e);
                if (attempt > ATTEMPT_LIMIT) {
                    throw e;
                }
            }
        }
        delete(backupFile);
        partNumber++;
        blockOffset = 0;
        if (!closingUpload) {
            //if not the final upload, create a new output stream
            backupFile = newBackupFile();
            backupStream = new BufferedOutputStream(new FileOutputStream(backupFile));
        }
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream)

Aggregations

BufferedOutputStream (java.io.BufferedOutputStream)2418 FileOutputStream (java.io.FileOutputStream)1659 IOException (java.io.IOException)1239 File (java.io.File)1060 OutputStream (java.io.OutputStream)791 BufferedInputStream (java.io.BufferedInputStream)527 InputStream (java.io.InputStream)374 FileInputStream (java.io.FileInputStream)332 DataOutputStream (java.io.DataOutputStream)242 ByteArrayOutputStream (java.io.ByteArrayOutputStream)228 ZipEntry (java.util.zip.ZipEntry)212 ZipOutputStream (java.util.zip.ZipOutputStream)209 FileNotFoundException (java.io.FileNotFoundException)191 ZipFile (java.util.zip.ZipFile)115 ArrayList (java.util.ArrayList)107 URL (java.net.URL)101 ObjectOutputStream (java.io.ObjectOutputStream)99 PrintStream (java.io.PrintStream)98 Test (org.junit.Test)94 ByteArrayInputStream (java.io.ByteArrayInputStream)89