Example usage for java.io StringWriter write

List of usage examples for java.io StringWriter write

Introduction

In this page you can find the example usage for java.io StringWriter write.

Prototype

public void write(String str) 

Source Link

Document

Write a string.

Usage

From source file:org.codelibs.robot.extractor.impl.TikaExtractor.java

@Override
public ExtractData getText(final InputStream inputStream, final Map<String, String> params) {
    if (inputStream == null) {
        throw new RobotSystemException("The inputstream is null.");
    }//from  w ww . j a v  a2s . c  o  m

    File tempFile = null;
    try {
        tempFile = File.createTempFile("tikaExtractor-", ".out");
    } catch (final IOException e) {
        throw new ExtractException("Could not create a temp file.", e);
    }

    try {
        try (OutputStream out = new FileOutputStream(tempFile)) {
            CopyUtil.copy(inputStream, out);
        }

        InputStream in = new FileInputStream(tempFile);

        final PrintStream originalOutStream = System.out;
        final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        System.setOut(new PrintStream(outStream, true));
        final PrintStream originalErrStream = System.err;
        final ByteArrayOutputStream errStream = new ByteArrayOutputStream();
        System.setErr(new PrintStream(errStream, true));
        try {
            final String resourceName = params == null ? null : params.get(TikaMetadataKeys.RESOURCE_NAME_KEY);
            final String contentType = params == null ? null : params.get(HttpHeaders.CONTENT_TYPE);
            String contentEncoding = params == null ? null : params.get(HttpHeaders.CONTENT_ENCODING);

            // password for pdf
            String pdfPassword = params == null ? null : params.get(ExtractData.PDF_PASSWORD);
            if (pdfPassword == null && params != null) {
                pdfPassword = getPdfPassword(params.get(ExtractData.URL), resourceName);
            }

            final Metadata metadata = createMetadata(resourceName, contentType, contentEncoding, pdfPassword);

            final Parser parser = new DetectParser();
            final ParseContext parseContext = new ParseContext();
            parseContext.set(Parser.class, parser);

            final StringWriter writer = new StringWriter(initialBufferSize);
            parser.parse(in, new BodyContentHandler(writer), metadata, parseContext);

            String content = normalizeContent(writer);
            if (StringUtil.isBlank(content)) {
                if (resourceName != null) {
                    IOUtils.closeQuietly(in);
                    if (logger.isDebugEnabled()) {
                        logger.debug("retry without a resource name: {}", resourceName);
                    }
                    in = new FileInputStream(tempFile);
                    final Metadata metadata2 = createMetadata(null, contentType, contentEncoding, pdfPassword);
                    final StringWriter writer2 = new StringWriter(initialBufferSize);
                    parser.parse(in, new BodyContentHandler(writer2), metadata2, parseContext);
                    content = normalizeContent(writer2);
                }
                if (StringUtil.isBlank(content) && contentType != null) {
                    IOUtils.closeQuietly(in);
                    if (logger.isDebugEnabled()) {
                        logger.debug("retry without a content type: {}", contentType);
                    }
                    in = new FileInputStream(tempFile);
                    final Metadata metadata3 = createMetadata(null, null, contentEncoding, pdfPassword);
                    final StringWriter writer3 = new StringWriter(initialBufferSize);
                    parser.parse(in, new BodyContentHandler(writer3), metadata3, parseContext);
                    content = normalizeContent(writer3);
                }

                if (readAsTextIfFailed && StringUtil.isBlank(content)) {
                    IOUtils.closeQuietly(in);
                    if (logger.isDebugEnabled()) {
                        logger.debug("read the content as a text.");
                    }
                    if (contentEncoding == null) {
                        contentEncoding = Constants.UTF_8;
                    }
                    BufferedReader br = null;
                    try {
                        br = new BufferedReader(
                                new InputStreamReader(new FileInputStream(tempFile), contentEncoding));
                        final StringWriter writer4 = new StringWriter(initialBufferSize);
                        String line;
                        while ((line = br.readLine()) != null) {
                            writer4.write(line.replaceAll("\\p{Cntrl}", " ").replaceAll("\\s+", " ").trim());
                            writer4.write(' ');
                        }
                        content = writer4.toString().trim();
                    } catch (final Exception e) {
                        logger.warn("Could not read " + tempFile.getAbsolutePath(), e);
                    } finally {
                        IOUtils.closeQuietly(br);
                    }
                }
            }
            final ExtractData extractData = new ExtractData(content);

            final String[] names = metadata.names();
            Arrays.sort(names);
            for (final String name : names) {
                extractData.putValues(name, metadata.getValues(name));
            }

            if (logger.isDebugEnabled()) {
                logger.debug("Result: metadata: {}", metadata);
            }

            return extractData;
        } catch (final TikaException e) {
            if (e.getMessage().indexOf("bomb") >= 0) {
                throw e;
            }
            final Throwable cause = e.getCause();
            if (cause instanceof SAXException) {
                final Extractor xmlExtractor = robotContainer.getComponent("xmlExtractor");
                if (xmlExtractor != null) {
                    IOUtils.closeQuietly(in);
                    in = new FileInputStream(tempFile);
                    return xmlExtractor.getText(in, params);
                }
            }
            throw e;
        } finally {
            IOUtils.closeQuietly(in);
            if (originalOutStream != null) {
                System.setOut(originalOutStream);
            }
            if (originalErrStream != null) {
                System.setErr(originalErrStream);
            }
            try {
                if (logger.isInfoEnabled()) {
                    final byte[] bs = outStream.toByteArray();
                    if (bs.length != 0) {
                        logger.info(new String(bs, outputEncoding));
                    }
                }
                if (logger.isWarnEnabled()) {
                    final byte[] bs = errStream.toByteArray();
                    if (bs.length != 0) {
                        logger.warn(new String(bs, outputEncoding));
                    }
                }
            } catch (final Exception e) {
                // NOP
            }
        }
    } catch (final Exception e) {
        throw new ExtractException("Could not extract a content.", e);
    } finally {
        if (tempFile != null && !tempFile.delete()) {
            logger.warn("Failed to delete " + tempFile.getAbsolutePath());
        }
    }
}

From source file:org.seasar.robot.extractor.impl.TikaExtractor.java

@Override
public ExtractData getText(final InputStream inputStream, final Map<String, String> params) {
    if (inputStream == null) {
        throw new RobotSystemException("The inputstream is null.");
    }//from  w ww  .  j a  va 2s.  co  m

    File tempFile = null;
    try {
        tempFile = File.createTempFile("tikaExtractor-", ".out");
    } catch (final IOException e) {
        throw new ExtractException("Could not create a temp file.", e);
    }

    try {
        OutputStream out = null;
        try {
            out = new FileOutputStream(tempFile);
            StreamUtil.drain(inputStream, out);
        } finally {
            IOUtils.closeQuietly(out);
        }

        InputStream in = new FileInputStream(tempFile);

        final PrintStream originalOutStream = System.out;
        final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        System.setOut(new PrintStream(outStream, true));
        final PrintStream originalErrStream = System.err;
        final ByteArrayOutputStream errStream = new ByteArrayOutputStream();
        System.setErr(new PrintStream(errStream, true));
        try {
            final String resourceName = params == null ? null : params.get(TikaMetadataKeys.RESOURCE_NAME_KEY);
            final String contentType = params == null ? null : params.get(HttpHeaders.CONTENT_TYPE);
            String contentEncoding = params == null ? null : params.get(HttpHeaders.CONTENT_ENCODING);

            // password for pdf
            String pdfPassword = params == null ? null : params.get(ExtractData.PDF_PASSWORD);
            if (pdfPassword == null && params != null) {
                pdfPassword = getPdfPassword(params.get(ExtractData.URL), resourceName);
            }

            final Metadata metadata = createMetadata(resourceName, contentType, contentEncoding, pdfPassword);

            final Parser parser = new DetectParser();
            final ParseContext parseContext = new ParseContext();
            parseContext.set(Parser.class, parser);

            final StringWriter writer = new StringWriter(initialBufferSize);
            parser.parse(in, new BodyContentHandler(writer), metadata, parseContext);

            String content = normalizeContent(writer);
            if (StringUtil.isBlank(content)) {
                if (resourceName != null) {
                    IOUtils.closeQuietly(in);
                    if (logger.isDebugEnabled()) {
                        logger.debug("retry without a resource name: {}", resourceName);
                    }
                    in = new FileInputStream(tempFile);
                    final Metadata metadata2 = createMetadata(null, contentType, contentEncoding, pdfPassword);
                    final StringWriter writer2 = new StringWriter(initialBufferSize);
                    parser.parse(in, new BodyContentHandler(writer2), metadata2, parseContext);
                    content = normalizeContent(writer2);
                }
                if (StringUtil.isBlank(content) && contentType != null) {
                    IOUtils.closeQuietly(in);
                    if (logger.isDebugEnabled()) {
                        logger.debug("retry without a content type: {}", contentType);
                    }
                    in = new FileInputStream(tempFile);
                    final Metadata metadata3 = createMetadata(null, null, contentEncoding, pdfPassword);
                    final StringWriter writer3 = new StringWriter(initialBufferSize);
                    parser.parse(in, new BodyContentHandler(writer3), metadata3, parseContext);
                    content = normalizeContent(writer3);
                }

                if (readAsTextIfFailed && StringUtil.isBlank(content)) {
                    IOUtils.closeQuietly(in);
                    if (logger.isDebugEnabled()) {
                        logger.debug("read the content as a text.");
                    }
                    if (contentEncoding == null) {
                        contentEncoding = Constants.UTF_8;
                    }
                    BufferedReader br = null;
                    try {
                        br = new BufferedReader(
                                new InputStreamReader(new FileInputStream(tempFile), contentEncoding));
                        final StringWriter writer4 = new StringWriter(initialBufferSize);
                        String line;
                        while ((line = br.readLine()) != null) {
                            writer4.write(line.replaceAll("\\p{Cntrl}", " ").replaceAll("\\s+", " ").trim());
                            writer4.write(' ');
                        }
                        content = writer4.toString().trim();
                    } catch (final Exception e) {
                        logger.warn("Could not read " + tempFile.getAbsolutePath(), e);
                    } finally {
                        IOUtils.closeQuietly(br);
                    }
                }
            }
            final ExtractData extractData = new ExtractData(content);

            final String[] names = metadata.names();
            Arrays.sort(names);
            for (final String name : names) {
                extractData.putValues(name, metadata.getValues(name));
            }

            if (logger.isDebugEnabled()) {
                logger.debug("Result: metadata: {}", metadata);
            }

            return extractData;
        } catch (final TikaException e) {
            if (e.getMessage().indexOf("bomb") >= 0) {
                throw e;
            }
            final Throwable cause = e.getCause();
            if (cause instanceof SAXException) {
                final Extractor xmlExtractor = SingletonS2Container.getComponent("xmlExtractor");
                if (xmlExtractor != null) {
                    IOUtils.closeQuietly(in);
                    in = new FileInputStream(tempFile);
                    return xmlExtractor.getText(in, params);
                }
            }
            throw e;
        } finally {
            IOUtils.closeQuietly(in);
            if (originalOutStream != null) {
                System.setOut(originalOutStream);
            }
            if (originalErrStream != null) {
                System.setErr(originalErrStream);
            }
            try {
                if (logger.isInfoEnabled()) {
                    final byte[] bs = outStream.toByteArray();
                    if (bs.length != 0) {
                        logger.info(new String(bs, outputEncoding));
                    }
                }
                if (logger.isWarnEnabled()) {
                    final byte[] bs = errStream.toByteArray();
                    if (bs.length != 0) {
                        logger.warn(new String(bs, outputEncoding));
                    }
                }
            } catch (final Exception e) {
                // NOP
            }
        }
    } catch (final Exception e) {
        throw new ExtractException("Could not extract a content.", e);
    } finally {
        if (tempFile != null && !tempFile.delete()) {
            logger.warn("Failed to delete " + tempFile.getAbsolutePath());
        }
    }
}

From source file:se.kodapan.io.http.wayback.Wayback.java

public HttpResponse execute(HttpPost post, Long oldestRevision) throws IOException {

    final HttpResponse httpResponse;

    WaybackRequest request = new WaybackRequest();
    request.setMethod("POST");
    request.setURI(post.getURI().toString());

    StringWriter sw = new StringWriter((int) post.getEntity().getContentLength() * 2);
    InputStream in = post.getEntity().getContent();
    byte[] buf = new byte[49152];
    int read;//  ww  w .ja  v a 2s  .  c  o m
    while ((read = in.read(buf)) > 0) {
        if (read == buf.length) {
            sw.write(Hex.encodeHex(buf));
        } else {
            byte[] last = new byte[read];
            System.arraycopy(buf, 0, last, 0, read);
            sw.write(Hex.encodeHex(last));
        }
    }
    in.close();

    request.setContent(sw.toString());

    StringBuilder headers = new StringBuilder();
    for (Header header : post.getAllHeaders()) {
        headers.append(header.getName());
        headers.append(": ");
        if (header.getValue() != null) {
            headers.append(header.getValue());
        }
        headers.append("\n");
    }

    request.setHeaders(headers.toString());

    WaybackResponse waybackResponse = seek(request);

    if (waybackResponse == null || waybackResponse.getRevision() < oldestRevision) {
        waybackResponse = factory(post, request);
    }
    return httpResponseFactory(waybackResponse);
}

From source file:org.commoncrawl.io.shared.NIOHttpHeaders.java

public synchronized String toString() {
    StringWriter writer = new StringWriter();

    for (int i = 0; i < keys.length && i < nkeys; i++) {
        if (keys[i] != null && keys[i].length() != 0) {
            writer.write(keys[i]);
            writer.write(":");
        }/*from  w  w  w  . java2s  . c  om*/
        writer.write(values[i]);
        writer.write("\r\n");
    }
    return writer.toString();
}

From source file:dk.dr.radio.data.EoDiverse.java

/**
   * Unescapes a string containing entity escapes to a string containing the actual Unicode characters corresponding to the escapes. Supports only HTML 3.0 entities.
   * Kilde: http://stackoverflow.com/questions/994331/java-how-to-decode-html-character-entities-in-java-like-httputility-htmldecode
   *///www. j a va 2  s.c o  m
  public static final String unescapeHtml3(final String input) {
      StringWriter writer = null;
      int len = input.length();
      int i = 1;
      int st = 0;
      while (true) {
          // look for '&'
          while (i < len && input.charAt(i - 1) != '&')
              i++;
          if (i >= len)
              break;

          // found '&', look for ';'
          int j = i;
          while (j < len && j < i + MAX_ESCAPE + 1 && input.charAt(j) != ';')
              j++;
          if (j == len || j < i + MIN_ESCAPE || j == i + MAX_ESCAPE + 1) {
              i++;
              continue;
          }

          // found escape
          if (input.charAt(i) == '#') {
              // numeric escape
              int k = i + 1;
              int radix = 10;

              final char firstChar = input.charAt(k);
              if (firstChar == 'x' || firstChar == 'X') {
                  k++;
                  radix = 16;
              }

              try {
                  int entityValue = Integer.parseInt(input.substring(k, j), radix);

                  if (writer == null)
                      writer = new StringWriter(input.length());
                  writer.append(input.substring(st, i - 1));

                  if (entityValue > 0xFFFF) {
                      final char[] chrs = Character.toChars(entityValue);
                      writer.write(chrs[0]);
                      writer.write(chrs[1]);
                  } else {
                      writer.write(entityValue);
                  }

              } catch (NumberFormatException ex) {
                  i++;
                  continue;
              }
          } else {
              // named escape
              CharSequence value = lookupMap.get(input.substring(i, j));
              if (value == null) {
                  i++;
                  continue;
              }

              if (writer == null)
                  writer = new StringWriter(input.length());
              writer.append(input.substring(st, i - 1));

              writer.append(value);
          }

          // skip escape
          st = j + 1;
          i = st;
      }

      if (writer != null) {
          writer.append(input.substring(st, len));
          return writer.toString();
      }
      return input;
  }

From source file:org.commoncrawl.io.NIOHttpHeaders.java

@Override
public synchronized String toString() {
    StringWriter writer = new StringWriter();

    for (int i = 0; i < keys.length && i < nkeys; i++) {
        if (keys[i] != null && keys[i].length() != 0) {
            writer.write(keys[i]);
            writer.write(":");
        }//from w w  w  .ja  v  a  2 s  .  c o  m
        writer.write(values[i]);
        writer.write("\r\n");
    }
    return writer.toString();
}

From source file:com.googlecode.promnetpp.translation.StandardTranslator.java

private void translateGlobalDeclaration(ASTNode globalDeclaration) throws IOException {
    ASTNode simpleDeclaration = globalDeclaration.getFirstChild();
    String typeName = simpleDeclaration.getTypeName();
    String identifier = simpleDeclaration.getName();
    boolean isArray = simpleDeclaration.getValueAsBoolean("isArray");
    String arrayCapacity = null;//from  w  w  w  . j a v  a  2 s. co m
    if (isArray) {
        arrayCapacity = simpleDeclaration.getValueAsString("arrayCapacity");
    }

    StringWriter writer = new StringWriter();
    writer.write(typeName + " " + identifier);
    if (isArray) {
        writer.write("[");
        writer.write(arrayCapacity);
        writer.write("]");
    }
    if (simpleDeclaration.hasMultipleChildren()) {
        ASTNode assignmentValue = simpleDeclaration.getSecondChild();
        writer.write(" = " + assignmentValue.toCppExpression());
    }
    writer.write(";");
    String fullDeclaration = writer.toString();
    StandardTranslatorData.addExternalDeclaration(fullDeclaration);
    globalDeclarations.write(fullDeclaration + "\n");
}

From source file:org.botlibre.util.Utils.java

public static String displayDate(Date date) {
    if (date == null) {
        return "";
    }/* ww  w.  ja v a 2  s  . co  m*/
    StringWriter writer = new StringWriter();
    Calendar today = Calendar.getInstance();
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    if (calendar.get(Calendar.YEAR) == today.get(Calendar.YEAR)
            && calendar.get(Calendar.DAY_OF_YEAR) == today.get(Calendar.DAY_OF_YEAR)) {
        writer.write("Today");
    } else if (calendar.get(Calendar.YEAR) == today.get(Calendar.YEAR)
            && calendar.get(Calendar.DAY_OF_YEAR) == (today.get(Calendar.DAY_OF_YEAR) - 1)) {
        writer.write("Yesterday");
    } else {
        writer.write(calendar.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.US));
        writer.write(" ");
        writer.write(String.valueOf(calendar.get(Calendar.DAY_OF_MONTH)));
        if (calendar.get(Calendar.YEAR) != today.get(Calendar.YEAR)) {
            writer.write(" ");
            writer.write(String.valueOf(calendar.get(Calendar.YEAR)));
        }
    }

    return writer.toString();
}

From source file:org.springframework.web.servlet.resource.AppCacheManifestTransfomer.java

@Override
public Resource transform(HttpServletRequest request, Resource resource,
        ResourceTransformerChain transformerChain) throws IOException {
    resource = transformerChain.transform(request, resource);

    String filename = resource.getFilename();
    if (!this.fileExtension.equals(StringUtils.getFilenameExtension(filename))) {
        return resource;
    }/* w w w.  j av a  2 s.com*/

    byte[] bytes = FileCopyUtils.copyToByteArray(resource.getInputStream());
    String content = new String(bytes, DEFAULT_CHARSET);

    if (!content.startsWith(MANIFEST_HEADER)) {
        if (logger.isTraceEnabled()) {
            logger.trace("AppCache manifest does not start with 'CACHE MANIFEST', skipping: " + resource);
        }
        return resource;
    }

    if (logger.isTraceEnabled()) {
        logger.trace("Transforming resource: " + resource);
    }

    StringWriter contentWriter = new StringWriter();
    HashBuilder hashBuilder = new HashBuilder(content.length());

    Scanner scanner = new Scanner(content);
    SectionTransformer currentTransformer = this.sectionTransformers.get(MANIFEST_HEADER);
    while (scanner.hasNextLine()) {
        String line = scanner.nextLine();
        if (this.sectionTransformers.containsKey(line.trim())) {
            currentTransformer = this.sectionTransformers.get(line.trim());
            contentWriter.write(line + "\n");
            hashBuilder.appendString(line);
        } else {
            contentWriter
                    .write(currentTransformer.transform(line, hashBuilder, resource, transformerChain) + "\n");
        }
    }

    String hash = hashBuilder.build();
    contentWriter.write("\n" + "# Hash: " + hash);
    if (logger.isTraceEnabled()) {
        logger.trace("AppCache file: [" + resource.getFilename() + "] Hash: [" + hash + "]");
    }

    return new TransformedResource(resource, contentWriter.toString().getBytes(DEFAULT_CHARSET));
}

From source file:org.transdroid.daemon.Aria2c.Aria2Adapter.java

@Override
public DaemonTaskResult executeTask(Log log, DaemonTask task) {

    try {// w ww  .j  a  v a 2s  . co  m
        JSONArray params = new JSONArray();

        switch (task.getMethod()) {
        case Retrieve:

            // Request all torrents from server
            // NOTE Since there is no aria2.tellAll (or something) we have to use batch requests
            JSONArray fields = new JSONArray().put("gid").put("status").put("totalLength")
                    .put("completedLength").put("uploadLength").put("downloadSpeed").put("uploadSpeed")
                    .put("numSeeders").put("dir").put("connections").put("errorCode").put("bittorrent")
                    .put("files");
            JSONObject active = buildRequest("aria2.tellActive", new JSONArray().put(fields));
            JSONObject waiting = buildRequest("aria2.tellWaiting",
                    new JSONArray().put(0).put(9999).put(fields));
            JSONObject stopped = buildRequest("aria2.tellStopped",
                    new JSONArray().put(0).put(9999).put(fields));
            params.put(active).put(waiting).put(stopped);

            List<Torrent> torrents = new ArrayList<Torrent>();
            JSONArray lists = makeRequestForArray(log, params.toString());
            for (int i = 0; i < lists.length(); i++) {
                torrents.addAll(parseJsonRetrieveTorrents(lists.getJSONObject(i).getJSONArray("result")));
            }
            return new RetrieveTaskSuccessResult((RetrieveTask) task, torrents, null);

        case GetTorrentDetails:

            // Request file listing of a torrent
            params.put(task.getTargetTorrent().getUniqueID()); // gid
            params.put(new JSONArray().put("bittorrent").put("errorCode"));

            JSONObject dinfo = makeRequest(log, buildRequest("aria2.tellStatus", params).toString());
            return new GetTorrentDetailsTaskSuccessResult((GetTorrentDetailsTask) task,
                    parseJsonTorrentDetails(dinfo.getJSONObject("result")));

        case GetFileList:

            // Request file listing of a torrent
            params.put(task.getTargetTorrent().getUniqueID()); // torrent_id

            JSONObject finfo = makeRequest(log, buildRequest("aria2.getFiles", params).toString());
            return new GetFileListTaskSuccessResult((GetFileListTask) task,
                    parseJsonFileListing(finfo.getJSONArray("result"), task.getTargetTorrent()));

        case AddByFile:

            // Encode the .torrent file's data
            String file = ((AddByFileTask) task).getFile();
            InputStream in = new Base64.InputStream(new FileInputStream(new File(URI.create(file))),
                    Base64.ENCODE);
            StringWriter writer = new StringWriter();
            int c;
            while ((c = in.read()) != -1) {
                writer.write(c);
            }
            in.close();

            // Request to add a torrent by local .torrent file
            params.put(writer.toString());
            makeRequest(log, buildRequest("aria2.addTorrent", params).toString());
            return new DaemonTaskSuccessResult(task);

        case AddByUrl:

            // Request to add a torrent by URL
            String url = ((AddByUrlTask) task).getUrl();
            params.put(new JSONArray().put(url));

            makeRequest(log, buildRequest("aria2.addUri", params).toString());
            return new DaemonTaskSuccessResult(task);

        case AddByMagnetUrl:

            // Request to add a magnet link by URL
            String magnet = ((AddByMagnetUrlTask) task).getUrl();
            params.put(new JSONArray().put(magnet));

            makeRequest(log, buildRequest("aria2.addUri", params).toString());
            return new DaemonTaskSuccessResult(task);

        case Remove:

            // Remove a torrent
            RemoveTask removeTask = (RemoveTask) task;
            makeRequest(log,
                    buildRequest(removeTask.includingData() ? "aria2.removeDownloadResult" : "aria2.remove",
                            params.put(removeTask.getTargetTorrent().getUniqueID())).toString());
            return new DaemonTaskSuccessResult(task);

        case Pause:

            // Pause a torrent
            PauseTask pauseTask = (PauseTask) task;
            makeRequest(log, buildRequest("aria2.pause", params.put(pauseTask.getTargetTorrent().getUniqueID()))
                    .toString());
            return new DaemonTaskSuccessResult(task);

        case PauseAll:

            // Resume all torrents
            makeRequest(log, buildRequest("aria2.pauseAll", null).toString());
            return new DaemonTaskSuccessResult(task);

        case Resume:

            // Resume a torrent
            ResumeTask resumeTask = (ResumeTask) task;
            makeRequest(log,
                    buildRequest("aria2.unpause", params.put(resumeTask.getTargetTorrent().getUniqueID()))
                            .toString());
            return new DaemonTaskSuccessResult(task);

        case ResumeAll:

            // Resume all torrents
            makeRequest(log, buildRequest("aria2.unpauseAll", null).toString());
            return new DaemonTaskSuccessResult(task);

        case SetTransferRates:

            // Request to set the maximum transfer rates
            SetTransferRatesTask ratesTask = (SetTransferRatesTask) task;
            JSONObject options = new JSONObject();
            options.put("max-overall-download-limit",
                    (ratesTask.getDownloadRate() == null ? -1 : ratesTask.getDownloadRate()));
            options.put("max-overall-upload-limit",
                    (ratesTask.getUploadRate() == null ? -1 : ratesTask.getUploadRate()));

            makeRequest(log, buildRequest("aria2.changeGlobalOption", params.put(options)).toString());
            return new DaemonTaskSuccessResult(task);

        default:
            return new DaemonTaskFailureResult(task, new DaemonException(ExceptionType.MethodUnsupported,
                    task.getMethod() + " is not supported by " + getType()));
        }
    } catch (JSONException e) {
        return new DaemonTaskFailureResult(task,
                new DaemonException(ExceptionType.ParsingFailed, e.toString()));
    } catch (DaemonException e) {
        return new DaemonTaskFailureResult(task, e);
    } catch (FileNotFoundException e) {
        return new DaemonTaskFailureResult(task,
                new DaemonException(ExceptionType.FileAccessError, e.toString()));
    } catch (IOException e) {
        return new DaemonTaskFailureResult(task,
                new DaemonException(ExceptionType.FileAccessError, e.toString()));
    }
}