Example usage for java.util LinkedList removeFirst

List of usage examples for java.util LinkedList removeFirst

Introduction

In this page you can find the example usage for java.util LinkedList removeFirst.

Prototype

public E removeFirst() 

Source Link

Document

Removes and returns the first element from this list.

Usage

From source file:com.trsst.Command.java

public int doPost(Client client, CommandLine commands, LinkedList<String> arguments, PrintStream out,
        InputStream in) {/*from  w  ww . j av a2 s. c  om*/

    String id = null;

    if (arguments.size() == 0 && commands.getArgList().size() == 0) {
        printPostUsage();
        return 127; // "command not found"
    }

    if (arguments.size() > 0) {
        id = arguments.removeFirst();
        System.err.println("Obtaining keys for feed id: " + id);
    } else {
        System.err.println("Generating new feed id... ");
    }

    // read input text
    String subject = commands.getOptionValue("s");
    String verb = commands.getOptionValue("v");
    String base = commands.getOptionValue("b");
    String body = commands.getOptionValue("c");
    String name = commands.getOptionValue("n");
    String email = commands.getOptionValue("m");
    String uri = commands.getOptionValue("uri");
    String title = commands.getOptionValue("t");
    String subtitle = commands.getOptionValue("subtitle");
    String icon = commands.getOptionValue("i");
    if (icon == null && commands.hasOption("i")) {
        icon = "-";
    }
    String logo = commands.getOptionValue("l");
    if (logo == null && commands.hasOption("l")) {
        logo = "-";
    }
    String attach = commands.getOptionValue("a");
    if (attach == null && commands.hasOption("a")) {
        attach = "-";
    }
    String[] recipients = commands.getOptionValues("e");
    String[] mentions = commands.getOptionValues("r");
    String[] tags = commands.getOptionValues("g");
    String url = commands.getOptionValue("u");
    String vanity = commands.getOptionValue("vanity");

    // obtain password
    char[] password = null;
    String pass = commands.getOptionValue("p");
    if (pass != null) {
        password = pass.toCharArray();
    } else {
        try {
            Console console = System.console();
            if (console != null) {
                password = console.readPassword("Password: ");
            } else {
                log.info("No console detected for password input.");
            }
        } catch (Throwable t) {
            log.error("Unexpected error while reading password", t);
        }
    }
    if (password == null) {
        log.error("Password is required to post.");
        return 127; // "command not found"
    }
    if (password.length < 6) {
        System.err.println("Password must be at least six characters in length.");
        return 127; // "command not found"
    }

    // obtain keys
    KeyPair signingKeys = null;
    KeyPair encryptionKeys = null;
    String keyPath = commands.getOptionValue("k");

    // if id was not specified from the command line
    if (id == null) {

        // if password was not specified from command line
        if (pass == null) {
            try {
                // verify password
                char[] verify = null;
                Console console = System.console();
                if (console != null) {
                    verify = console.readPassword("Re-type Password: ");
                } else {
                    log.info("No console detected for password verification.");
                }
                if (verify == null || verify.length != password.length) {
                    System.err.println("Passwords do not match.");
                    return 127; // "command not found"
                }
                for (int i = 0; i < verify.length; i++) {
                    if (verify[i] != password[i]) {
                        System.err.println("Passwords do not match.");
                        return 127; // "command not found"
                    }
                    verify[i] = 0;
                }
            } catch (Throwable t) {
                log.error("Unexpected error while verifying password: " + t.getMessage(), t);
            }
        }

        // create new account
        if (base == null) {
            // default to trsst hub
            base = "https://home.trsst.com/feed";
        }

        // generate vanity id if required
        if (vanity != null) {
            System.err.println("Searching for vanity feed id prefix: " + vanity);
            switch (vanity.length()) {
            case 0:
            case 1:
                break;
            case 2:
                System.err.println("This may take several minutes.");
                break;
            case 3:
                System.err.println("This may take several hours.");
                break;
            case 4:
                System.err.println("This may take several days.");
                break;
            case 5:
                System.err.println("This may take several months.");
                break;
            default:
                System.err.println("This may take several years.");
                break;
            }
            System.err.println("Started: " + new Date());
            System.err.println("^C to exit");
        }
        do {
            signingKeys = Common.generateSigningKeyPair();
            id = Common.toFeedId(signingKeys.getPublic());
        } while (vanity != null && !id.startsWith(vanity));
        if (vanity != null) {
            System.err.println("Finished: " + new Date());
        }

        encryptionKeys = Common.generateEncryptionKeyPair();
        System.err.println("New feed id created: " + id);

        File keyFile;
        if (keyPath != null) {
            keyFile = new File(keyPath, id + Common.KEY_EXTENSION);
        } else {
            keyFile = new File(Common.getClientRoot(), id + Common.KEY_EXTENSION);
        }

        // persist to keystore
        writeSigningKeyPair(signingKeys, id, keyFile, password);
        writeEncryptionKeyPair(encryptionKeys, id, keyFile, password);

    } else {

        File keyFile;
        if (keyPath != null) {
            keyFile = new File(Common.getClientRoot(), keyPath);
        } else {
            keyFile = new File(Common.getClientRoot(), id + Common.KEY_EXTENSION);
        }

        if (keyFile.exists()) {
            System.err.println("Using existing account id: " + id);

        } else {
            System.err.println("Cannot locate keys for account id: " + id);
            return 78; // "configuration error"
        }

        signingKeys = readSigningKeyPair(id, keyFile, password);
        if (signingKeys != null) {
            encryptionKeys = readEncryptionKeyPair(id, keyFile, password);
            if (encryptionKeys == null) {
                encryptionKeys = signingKeys;
            }
        }
    }

    // clear password chars
    for (int i = 0; i < password.length; i++) {
        password[i] = 0;
    }
    if (signingKeys == null) {
        System.err.println("Could not obtain keys for signing.");
        return 73; // "can't create output error"
    }

    String[] recipientIds = null;
    if (recipients != null) {
        LinkedList<String> keys = new LinkedList<String>();
        for (int i = 0; i < recipients.length; i++) {
            if ("-".equals(recipients[i])) {
                // "-" is shorthand for encrypt for mentioned ids
                if (mentions != null) {
                    for (String mention : mentions) {
                        if (Common.isFeedId(mention)) {
                            keys.add(mention);
                        }
                    }
                }
            } else if (Common.isFeedId(recipients[i])) {
                keys.add(recipients[i]);
            } else {
                log.warn("Could not parse recipient id: " + recipients[i]);
            }
        }
        recipientIds = keys.toArray(new String[0]);
    }

    // handle binary attachment
    String mimetype = null;
    byte[] attachment = null;
    if (attach != null) {
        InputStream input = null;
        try {
            if ("-".equals(attach)) {
                input = new BufferedInputStream(in);
            } else {
                File file = new File(attach);
                input = new BufferedInputStream(new FileInputStream(file));
                System.err.println("Attaching: " + file.getCanonicalPath());
            }
            attachment = Common.readFully(input);
            mimetype = new Tika().detect(attachment);
            System.err.println("Detected type: " + mimetype);
        } catch (Throwable t) {
            log.error("Could not read attachment: " + attach, t);
            return 73; // "can't create output error"
        } finally {
            try {
                input.close();
            } catch (IOException ioe) {
                // suppress any futher error on closing
            }
        }
    }

    Object result;
    try {
        EntryOptions options = new EntryOptions();
        options.setStatus(subject);
        options.setVerb(verb);
        if (mentions != null) {
            options.setMentions(mentions);
        }
        if (tags != null) {
            options.setTags(tags);
        }
        options.setBody(body);
        if (attachment != null) {
            options.addContentData(attachment, mimetype);
        } else if (url != null) {
            options.setContentUrl(url);
        }
        FeedOptions feedOptions = new FeedOptions();
        feedOptions.setAuthorEmail(email);
        feedOptions.setAuthorName(name);
        feedOptions.setAuthorUri(uri);
        feedOptions.setTitle(title);
        feedOptions.setSubtitle(subtitle);
        feedOptions.setBase(base);
        if (icon != null) {
            if ("-".equals(icon)) {
                feedOptions.setAsIcon(true);
            } else {
                feedOptions.setIconURL(icon);
            }
        }
        if (logo != null) {
            if ("-".equals(logo)) {
                feedOptions.setAsLogo(true);
            } else {
                feedOptions.setLogoURL(logo);
            }
        }
        if (recipientIds != null) {
            EntryOptions publicEntry = new EntryOptions().setStatus("Encrypted content").setVerb("encrypt");
            // TODO: add duplicate mentions to outside of envelope
            options.encryptFor(recipientIds, publicEntry);
        }
        result = client.post(signingKeys, encryptionKeys, options, feedOptions);
    } catch (IllegalArgumentException e) {
        log.error("Invalid request: " + id + " : " + e.getMessage(), e);
        return 76; // "remote error"
    } catch (IOException e) {
        log.error("Error connecting to service for id: " + id, e);
        return 76; // "remote error"
    } catch (org.apache.abdera.security.SecurityException e) {
        log.error("Error generating signatures for id: " + id, e);
        return 73; // "can't create output error"
    } catch (Exception e) {
        log.error("General security error for id: " + id, e);
        return 74; // "general io error"
    }

    if (result != null) {
        if (format) {
            out.println(Common.formatXML(result.toString()));
        } else {
            out.println(result.toString());
        }
    }

    return 0; // "OK"
}

From source file:net.jenet.Host.java

Event dispatchIncomingCommands() {
    Event result = new Event();
    Peer currentPeer;/*from   w w  w . j  a va 2  s.  c  om*/

    LinkedList<Peer> peersList = new LinkedList<Peer>(peers.values());

    if (peers.size() == 0)
        return result;

    /* 
     * Simply calling containsKey( lastServicedPeer.getIncomingPeerId() ) will 
     * not be sufficient because the peerID of lastServicedPeer may have been 
     * reassigned.  The get operation is quicker than containsValue because 
     * it does not have to search through all the peers. 
     * 
     * lastServicedPeer.isDisconnected() may be sufficient, but this feels more robust.
     */
    if (lastServicedPeer == null || peers.get(lastServicedPeer.getIncomingPeerID()) != lastServicedPeer)
        lastServicedPeer = peersList.getFirst();
    else
        while (peersList.getLast() != lastServicedPeer)
            peersList.addLast(peersList.removeFirst());

    do {
        currentPeer = peersList.removeFirst();
        peersList.addLast(currentPeer);

        if (currentPeer.isZombie()) {
            recalculateBandwithLimits = true;
            currentPeer.reset();
            result.setType(Event.TYPE.DISCONNECTED);
            result.setPeer(currentPeer);
            lastServicedPeer = currentPeer;
            return result;
        }

        if (!currentPeer.isConnected())
            continue;

        for (byte channelID : currentPeer.getChannels().keySet()) {
            Channel channel = currentPeer.getChannels().get(channelID);
            if (channel.getIncomingReliableCommands().isEmpty()
                    && channel.getIncomingUnreliableCommands().isEmpty())
                continue;
            Packet packet = currentPeer.receive(channelID);
            result.setPacket(packet);
            if (packet == null)
                continue;
            result.setType(Event.TYPE.RECEIVED);
            result.setPeer(currentPeer);
            result.setChannelID(channelID);
            result.setPacket(packet);
            lastServicedPeer = currentPeer;

            return result;
        }
    } while (currentPeer != lastServicedPeer);

    return result;
}

From source file:org.pf9.pangu.app.act.rest.diagram.services.ProcessInstanceHighlightsResource.java

/**
 * getHighlightedFlows/* w  w w .j a  v a  2s.  c o m*/
 * 
 * code logic: 1. Loop all activities by id asc order; 2. Check each activity's outgoing transitions and eventBoundery outgoing transitions, if
 * outgoing transitions's destination.id is in other executed activityIds, add this transition to highLightedFlows List; 3. But if activity is not
 * a parallelGateway or inclusiveGateway, only choose the earliest flow.
 * 
 * @param activityList
 * @param hisActInstList
 * @param highLightedFlows
 */
private void getHighlightedFlows(List<ActivityImpl> activityList,
        LinkedList<HistoricActivityInstance> hisActInstList, List<String> highLightedFlows) {

    //check out startEvents in activityList
    List<ActivityImpl> startEventActList = new ArrayList<ActivityImpl>();
    Map<String, ActivityImpl> activityMap = new HashMap<String, ActivityImpl>(activityList.size());
    for (ActivityImpl activity : activityList) {

        activityMap.put(activity.getId(), activity);

        String actType = (String) activity.getProperty("type");
        if (actType != null && actType.toLowerCase().indexOf("startevent") >= 0) {
            startEventActList.add(activity);
        }
    }

    //These codes is used to avoid a bug: 
    //ACT-1728 If the process instance was started by a callActivity, it will be not have the startEvent activity in ACT_HI_ACTINST table 
    //Code logic:
    //Check the first activity if it is a startEvent, if not check out the startEvent's highlight outgoing flow.
    HistoricActivityInstance firstHistActInst = hisActInstList.getFirst();
    String firstActType = (String) firstHistActInst.getActivityType();
    if (firstActType != null && firstActType.toLowerCase().indexOf("startevent") < 0) {
        PvmTransition startTrans = getStartTransaction(startEventActList, firstHistActInst);
        if (startTrans != null) {
            highLightedFlows.add(startTrans.getId());
        }
    }

    while (!hisActInstList.isEmpty()) {
        HistoricActivityInstance histActInst = hisActInstList.removeFirst();
        ActivityImpl activity = activityMap.get(histActInst.getActivityId());
        if (activity != null) {
            boolean isParallel = false;
            String type = histActInst.getActivityType();
            if ("parallelGateway".equals(type) || "inclusiveGateway".equals(type)) {
                isParallel = true;
            } else if ("subProcess".equals(histActInst.getActivityType())) {
                getHighlightedFlows(activity.getActivities(), hisActInstList, highLightedFlows);
            }

            List<PvmTransition> allOutgoingTrans = new ArrayList<PvmTransition>();
            allOutgoingTrans.addAll(activity.getOutgoingTransitions());
            allOutgoingTrans.addAll(getBoundaryEventOutgoingTransitions(activity));
            List<String> activityHighLightedFlowIds = getHighlightedFlows(allOutgoingTrans, hisActInstList,
                    isParallel);
            highLightedFlows.addAll(activityHighLightedFlowIds);
        }
    }
}

From source file:com.hipu.bdb.util.FileUtils.java

/**
 * Retrieve a number of lines from the file around the given 
 * position, as when paging forward or backward through a file. 
 * //from  w w  w .  j a v  a  2 s .  c o  m
 * @param file File to retrieve lines
 * @param position offset to anchor lines
 * @param signedDesiredLineCount lines requested; if negative, 
 *        want this number of lines ending with a line containing
 *        the position; if positive, want this number of lines,
 *        all starting at or after position. 
 * @param lines List<String> to insert found lines
 * @param lineEstimate int estimate of line size, 0 means use default
 *        of 128
 * @return LongRange indicating the file offsets corresponding to 
 *         the beginning of the first line returned, and the point
 *         after the end of the last line returned
 * @throws IOException
 */
@SuppressWarnings("unchecked")
public static LongRange pagedLines(File file, long position, int signedDesiredLineCount, List<String> lines,
        int lineEstimate) throws IOException {
    // consider negative positions as from end of file; -1 = last byte
    if (position < 0) {
        position = file.length() + position;
    }

    // calculate a reasonably sized chunk likely to have all desired lines
    if (lineEstimate == 0) {
        lineEstimate = 128;
    }
    int desiredLineCount = Math.abs(signedDesiredLineCount);
    long startPosition;
    long fileEnd = file.length();
    int bufferSize = (desiredLineCount + 5) * lineEstimate;
    if (signedDesiredLineCount > 0) {
        // reading forward; include previous char in case line-end
        startPosition = position - 1;
    } else {
        // reading backward
        startPosition = position - bufferSize + (2 * lineEstimate);
    }
    if (startPosition < 0) {
        startPosition = 0;
    }
    if (startPosition + bufferSize > fileEnd) {
        bufferSize = (int) (fileEnd - startPosition);
    }

    // read that reasonable chunk
    FileInputStream fis = new FileInputStream(file);
    fis.getChannel().position(startPosition);
    byte[] buf = new byte[bufferSize];
    IOUtils.closeQuietly(fis);

    // find all line starts fully in buffer
    // (positions after a line-end, per line-end definition in 
    // BufferedReader.readLine)
    LinkedList<Integer> lineStarts = new LinkedList<Integer>();
    if (startPosition == 0) {
        lineStarts.add(0);
    }
    boolean atLineEnd = false;
    boolean eatLF = false;
    int i;
    for (i = 0; i < bufferSize; i++) {
        if ((char) buf[i] == '\n' && eatLF) {
            eatLF = false;
            continue;
        }
        if (atLineEnd) {
            atLineEnd = false;
            lineStarts.add(i);
            if (signedDesiredLineCount < 0 && startPosition + i > position) {
                // reached next line past position, read no more
                break;
            }
        }
        if ((char) buf[i] == '\r') {
            atLineEnd = true;
            eatLF = true;
            continue;
        }
        if ((char) buf[i] == '\n') {
            atLineEnd = true;
        }
    }
    if (startPosition + i == fileEnd) {
        // add phantom lineStart after end
        lineStarts.add(bufferSize);
    }
    int foundFullLines = lineStarts.size() - 1;

    // if found no lines
    if (foundFullLines < 1) {
        if (signedDesiredLineCount > 0) {
            if (startPosition + bufferSize == fileEnd) {
                // nothing more to read: return nothing
                return new LongRange(fileEnd, fileEnd);
            } else {
                // retry with larger lineEstimate
                return pagedLines(file, position, signedDesiredLineCount, lines,
                        Math.max(bufferSize, lineEstimate));
            }

        } else {
            // try again with much larger line estimate
            // TODO: fail gracefully before growing to multi-MB buffers
            return pagedLines(file, position, signedDesiredLineCount, lines, bufferSize);
        }
    }

    // trim unneeded lines
    while (signedDesiredLineCount > 0 && startPosition + lineStarts.getFirst() < position) {
        // discard lines starting before desired position
        lineStarts.removeFirst();
    }
    while (lineStarts.size() > desiredLineCount + 1) {
        if (signedDesiredLineCount < 0 && (startPosition + lineStarts.get(1) <= position)) {
            // discard from front until reach line containing target position
            lineStarts.removeFirst();
        } else {
            lineStarts.removeLast();
        }
    }
    int firstLine = lineStarts.getFirst();
    int partialLine = lineStarts.getLast();
    LongRange range = new LongRange(startPosition + firstLine, startPosition + partialLine);
    List<String> foundLines = IOUtils
            .readLines(new ByteArrayInputStream(buf, firstLine, partialLine - firstLine));

    if (foundFullLines < desiredLineCount && signedDesiredLineCount < 0 && startPosition > 0) {
        // if needed and reading backward, read more lines from earlier
        range = expandRange(range, pagedLines(file, range.getMinimumLong() - 1,
                signedDesiredLineCount + foundFullLines, lines, bufferSize / foundFullLines));

    }

    lines.addAll(foundLines);

    if (signedDesiredLineCount < 0 && range.getMaximumLong() < position) {
        // did not get line containining start position
        range = expandRange(range, pagedLines(file, partialLine, 1, lines, bufferSize / foundFullLines));
    }

    if (signedDesiredLineCount > 0 && foundFullLines < desiredLineCount && range.getMaximumLong() < fileEnd) {
        // need more forward lines
        range = expandRange(range, pagedLines(file, range.getMaximumLong(), desiredLineCount - foundFullLines,
                lines, bufferSize / foundFullLines));
    }

    return range;
}

From source file:psiprobe.controllers.logs.FollowController.java

@Override
protected ModelAndView handleLogFile(HttpServletRequest request, HttpServletResponse response,
        LogDestination logDest) throws Exception {

    ModelAndView mv = new ModelAndView(getViewName());
    File file = logDest.getFile();

    if (file.exists()) {
        LinkedList<String> lines = new LinkedList<>();
        long actualLength = file.length();
        long lastKnownLength = ServletRequestUtils.getLongParameter(request, "lastKnownLength", 0);
        long currentLength = ServletRequestUtils.getLongParameter(request, "currentLength", actualLength);
        long maxReadLines = ServletRequestUtils.getLongParameter(request, "maxReadLines", 0);

        if (lastKnownLength > currentLength || lastKnownLength > actualLength || currentLength > actualLength) {

            // file length got reset
            lastKnownLength = 0;//from w w  w . j  a  v a2s .  c om
            lines.add(" ------------- THE FILE HAS BEEN TRUNCATED --------------");
        }

        try (BackwardsFileStream bfs = new BackwardsFileStream(file, currentLength)) {
            BackwardsLineReader br;
            if (logDest.getEncoding() != null) {
                br = new BackwardsLineReader(bfs, logDest.getEncoding());
            } else {
                br = new BackwardsLineReader(bfs);
            }
            long readSize = 0;
            long totalReadSize = currentLength - lastKnownLength;
            String line;
            while (readSize < totalReadSize && (line = br.readLine()) != null) {
                if (!line.isEmpty()) {
                    lines.addFirst(line);
                    readSize += line.length();
                } else {
                    readSize++;
                }
                if (maxReadLines != 0 && lines.size() >= maxReadLines) {
                    break;
                }
            }

            if (lastKnownLength != 0 && readSize > totalReadSize) {
                lines.removeFirst();
            }
        }

        mv.addObject("lines", lines);
    }
    return mv;
}

From source file:de.tudarmstadt.ukp.wikipedia.parser.mediawiki.ModularParser.java

private DefinitionList buildDefinitionList(SpanManager sm, ContentElementParsingParameters cepp,
        LinkedList<Span> lineSpans) {
    List<ContentElement> content = new ArrayList<ContentElement>();

    Span s = lineSpans.removeFirst();

    int temp = sm.indexOf(":", s);
    if (temp == -1) {
        content.add(parseContentElement(sm, cepp, new Span(s.getStart() + 1, s.getEnd())));
    } else {/*ww w  .j a v  a  2  s  .  c  o  m*/
        content.add(parseContentElement(sm, cepp, new Span(temp + 1, s.getEnd())));
        content.add(0, parseContentElement(sm, cepp, new Span(s.getStart() + 1, temp)));
    }

    while (!lineSpans.isEmpty()) {
        Span ns = lineSpans.getFirst();
        if (sm.charAt(ns.getStart()) != ':') {
            break;
        }
        lineSpans.removeFirst();
        content.add(parseContentElement(sm, cepp, new Span(ns.getStart() + 1, ns.getEnd())));
    }

    DefinitionList result = new DefinitionList(content);

    if (calculateSrcSpans) {
        result.setSrcSpan(
                new SrcSpan(sm.getSrcPos(s.getStart()), content.get(content.size() - 1).getSrcSpan().getEnd()));
    }

    return result;
}

From source file:org.ejbca.ui.cli.infrastructure.parameter.ParameterHandler.java

/**
 * This method takes the parameters given by the command line and returns them as a map keyed to the flags
 * //from   w  w  w  . j  ava 2 s  .c o  m
 * @param arguments the parameters as given by the command line
 * @return a map of parameters and their values, null if command cannot run.
 */
public ParameterContainer parseParameters(String... arguments) {
    ParameterContainer result = new ParameterContainer();
    List<String> argumentList = new ArrayList<String>();
    boolean verbose = false;
    for (int i = 0; i < arguments.length; i++) {
        String argument = arguments[i];
        //Glue together any quotes that may be mismatched due to spaces
        if ((argument.startsWith("'") && !argument.endsWith("'"))
                || (argument.startsWith("\"") && !argument.endsWith("\""))) {
            final String quoteType = argument.substring(0, 1);
            for (int j = i + 1; j < arguments.length; j++) {
                if (arguments[j].startsWith(quoteType)) {
                    log.error("ERROR: Unclosed quote: " + argument);
                    return null;
                } else if (arguments[j].endsWith(quoteType)) {
                    argument += " " + arguments[j];
                    i = j;
                    break;
                } else {
                    argument += " " + arguments[j];
                }
            }
        }
        if (argument.equals(VERBOSE_KEY)) {
            verbose = true;
        } else {
            argumentList.add(argument);
        }
        if (log.isDebugEnabled()) {
            log.debug("ARGUMENT: " + argument);
        }
    }

    List<String> unknownArguments = new ArrayList<String>();
    List<Parameter> missingArguments = new ArrayList<Parameter>();
    LinkedList<String> standaloneParametersDefensiveCopy = new LinkedList<String>(standaloneParameters);
    //Get a list of all parameters
    for (int i = 0; i < argumentList.size(); i++) {
        boolean isStandalone = false;
        String parameterString = argumentList.get(i);
        //Handle the case where the command line may have split up an argument with a a space that's in quotes

        //Handle the case of -argument=value, but ignore if in quotes
        if (parameterString.matches("^-.*=.*")) {
            //If so, split the switch and value up.
            int valueIndex = parameterString.indexOf("=") + 1;
            argumentList.add(i + 1, (valueIndex >= parameterString.length() ? ""
                    : parameterString.substring(valueIndex, parameterString.length())));
            parameterString = parameterString.substring(0, valueIndex - 1);
        }
        if (result.containsKey(parameterString)) {
            log.info("ERROR: Multiple parameters of type " + parameterString + " encountered.");
            return null;
        }
        Parameter parameter = parameterMap.get(parameterString);
        String value;
        if (parameter == null) {
            //Presume that it might be a standalone argument
            if (standaloneParametersDefensiveCopy.size() > 0 && !parameterString.startsWith("-")) {
                value = parameterString;
                parameterString = standaloneParametersDefensiveCopy.removeFirst();
                isStandalone = true;
            } else {
                unknownArguments.add(parameterString);
                continue;
            }
        } else {
            if (parameter.getParameterMode() == ParameterMode.ARGUMENT) {
                //Check that the following argument exists and isn't a switch (ignoring negative numbers)
                if ((i + 1) >= argumentList.size() || argumentList.get(i + 1).matches("^-[A-z]+$")) {
                    log.info("ERROR: Missing argument.");
                    log.info(TAB + parameterString + " is an argument and requires a parameter following it.");
                    continue;
                } else {
                    value = argumentList.get(i + 1);
                    i++;
                }
            } else if (parameter.getParameterMode() == ParameterMode.FLAG) {
                value = "";
            } else if (parameter.getParameterMode() == ParameterMode.INPUT) {
                log.info("Enter value for " + parameterString + ": ");
                value = System.console().readLine();
            } else if (parameter.getParameterMode() == ParameterMode.PASSWORD) {
                log.info("Enter password for parameter (" + parameterString + "): ");
                value = new String(System.console().readPassword());
            } else {
                throw new IllegalStateException(
                        parameter.getParameterMode().name() + " was an unknown parameter type.");
            }
        }
        if (verbose) {
            if (!StringUtils.isEmpty(value)
                    && (parameter == null || (parameter.getParameterMode() != ParameterMode.PASSWORD
                            && parameter.getParameterMode() != ParameterMode.FLAG))) {
                log.error("SETTING: " + parameterString + " as " + value);
            }
        }

        //Lastly, strip any quotes 
        if ((value.startsWith("'") && value.endsWith("'"))
                || (value.startsWith("\"") && value.endsWith("\""))) {
            value = value.substring(1, value.length() - 1);
        }

        result.put(parameterString, value, isStandalone);
    }

    if (result.containsKey(HELP_KEY)) {
        //Do not validate if help was requested
        return result;
    }

    //Check for mandatory parameters
    for (final String mandatoryParameter : mandatoryParameters) {
        if (!result.containsKey(mandatoryParameter)) {
            missingArguments.add(parameterMap.get(mandatoryParameter));
        }
    }
    if (unknownArguments.size() == 0 && missingArguments.size() == 0) {
        return result;
    } else {
        StringBuilder sb = new StringBuilder();
        sb.append("ERROR: Incorrect parameter usage.");
        if (unknownArguments.size() > 0) {
            sb.append("\n");
            sb.append(tab(1) + "The following arguments are unknown:\n");
            for (String unknownParameter : unknownArguments) {
                sb.append(tab(2) + unknownParameter + "\n");
            }
        }
        if (missingArguments.size() > 0) {
            sb.append("\n");
            sb.append(tab(1) + "The following mandatory arguments are missing or poorly formed:\n");
            int longestKeyWord = 0;
            for (Parameter missingParameter : missingArguments) {
                if (missingParameter.getKeyWord().length() > longestKeyWord) {
                    longestKeyWord = missingParameter.getKeyWord().length();
                }
            }
            String indent = tab(longestKeyWord / TAB.length() + 2);
            for (Parameter missingParameter : missingArguments) {
                sb.append(tab(2) + missingParameter.getKeyWord()
                        + indent.substring(missingParameter.getKeyWord().length()));
                List<String> lines = CommandBase.splitStringIntoLines(missingParameter.getInstruction(),
                        120 - indent.length());
                if (lines.size() > 0) {
                    sb.append(lines.get(0) + "\n");
                    for (int i = 1; i < lines.size(); i++) {
                        sb.append(tab(2) + indent + lines.get(i) + "\n");
                    }
                }

            }
        }
        sb.append(CR + "Run command with \"" + HELP_KEY + "\" to see full manual page.");
        log.info(sb);
        return null;
    }
}

From source file:org.archive.util.FileUtils.java

/**
 * Retrieve a number of lines from the file around the given 
 * position, as when paging forward or backward through a file. 
 * /*  ww w. j ava  2 s  . c  o m*/
 * @param file File to retrieve lines
 * @param position offset to anchor lines
 * @param signedDesiredLineCount lines requested; if negative, 
 *        want this number of lines ending with a line containing
 *        the position; if positive, want this number of lines,
 *        all starting at or after position. 
 * @param lines List<String> to insert found lines
 * @param lineEstimate int estimate of line size, 0 means use default
 *        of 128
 * @return LongRange indicating the file offsets corresponding to 
 *         the beginning of the first line returned, and the point
 *         after the end of the last line returned
 * @throws IOException
 */
@SuppressWarnings("unchecked")
public static LongRange pagedLines(File file, long position, int signedDesiredLineCount, List<String> lines,
        int lineEstimate) throws IOException {
    // consider negative positions as from end of file; -1 = last byte
    if (position < 0) {
        position = file.length() + position;
    }

    // calculate a reasonably sized chunk likely to have all desired lines
    if (lineEstimate == 0) {
        lineEstimate = 128;
    }
    int desiredLineCount = Math.abs(signedDesiredLineCount);
    long startPosition;
    long fileEnd = file.length();
    int bufferSize = (desiredLineCount + 5) * lineEstimate;
    if (signedDesiredLineCount > 0) {
        // reading forward; include previous char in case line-end
        startPosition = position - 1;
    } else {
        // reading backward
        startPosition = position - bufferSize + (2 * lineEstimate);
    }
    if (startPosition < 0) {
        startPosition = 0;
    }
    if (startPosition + bufferSize > fileEnd) {
        bufferSize = (int) (fileEnd - startPosition);
    }

    // read that reasonable chunk
    FileInputStream fis = new FileInputStream(file);
    fis.getChannel().position(startPosition);
    byte[] buf = new byte[bufferSize];
    ArchiveUtils.readFully(fis, buf);
    IOUtils.closeQuietly(fis);

    // find all line starts fully in buffer
    // (positions after a line-end, per line-end definition in 
    // BufferedReader.readLine)
    LinkedList<Integer> lineStarts = new LinkedList<Integer>();
    if (startPosition == 0) {
        lineStarts.add(0);
    }
    boolean atLineEnd = false;
    boolean eatLF = false;
    int i;
    for (i = 0; i < bufferSize; i++) {
        if ((char) buf[i] == '\n' && eatLF) {
            eatLF = false;
            continue;
        }
        if (atLineEnd) {
            atLineEnd = false;
            lineStarts.add(i);
            if (signedDesiredLineCount < 0 && startPosition + i > position) {
                // reached next line past position, read no more
                break;
            }
        }
        if ((char) buf[i] == '\r') {
            atLineEnd = true;
            eatLF = true;
            continue;
        }
        if ((char) buf[i] == '\n') {
            atLineEnd = true;
        }
    }
    if (startPosition + i == fileEnd) {
        // add phantom lineStart after end
        lineStarts.add(bufferSize);
    }
    int foundFullLines = lineStarts.size() - 1;

    // if found no lines
    if (foundFullLines < 1) {
        if (signedDesiredLineCount > 0) {
            if (startPosition + bufferSize == fileEnd) {
                // nothing more to read: return nothing
                return new LongRange(fileEnd, fileEnd);
            } else {
                // retry with larger lineEstimate
                return pagedLines(file, position, signedDesiredLineCount, lines,
                        Math.max(bufferSize, lineEstimate));
            }

        } else {
            // try again with much larger line estimate
            // TODO: fail gracefully before growing to multi-MB buffers
            return pagedLines(file, position, signedDesiredLineCount, lines, bufferSize);
        }
    }

    // trim unneeded lines
    while (signedDesiredLineCount > 0 && startPosition + lineStarts.getFirst() < position) {
        // discard lines starting before desired position
        lineStarts.removeFirst();
    }
    while (lineStarts.size() > desiredLineCount + 1) {
        if (signedDesiredLineCount < 0 && (startPosition + lineStarts.get(1) <= position)) {
            // discard from front until reach line containing target position
            lineStarts.removeFirst();
        } else {
            lineStarts.removeLast();
        }
    }
    int firstLine = lineStarts.getFirst();
    int partialLine = lineStarts.getLast();
    LongRange range = new LongRange(startPosition + firstLine, startPosition + partialLine);
    List<String> foundLines = IOUtils
            .readLines(new ByteArrayInputStream(buf, firstLine, partialLine - firstLine));

    if (foundFullLines < desiredLineCount && signedDesiredLineCount < 0 && startPosition > 0) {
        // if needed and reading backward, read more lines from earlier
        range = expandRange(range, pagedLines(file, range.getMinimumLong() - 1,
                signedDesiredLineCount + foundFullLines, lines, bufferSize / foundFullLines));

    }

    lines.addAll(foundLines);

    if (signedDesiredLineCount < 0 && range.getMaximumLong() < position) {
        // did not get line containining start position
        range = expandRange(range, pagedLines(file, partialLine, 1, lines, bufferSize / foundFullLines));
    }

    if (signedDesiredLineCount > 0 && foundFullLines < desiredLineCount && range.getMaximumLong() < fileEnd) {
        // need more forward lines
        range = expandRange(range, pagedLines(file, range.getMaximumLong(), desiredLineCount - foundFullLines,
                lines, bufferSize / foundFullLines));
    }

    return range;
}

From source file:de.tudarmstadt.ukp.wikipedia.parser.mediawiki.ModularParser.java

private Paragraph buildParagraph(SpanManager sm, ContentElementParsingParameters cepp,
        LinkedList<Span> lineSpans, lineType paragraphType) {

    LinkedList<Span> paragraphSpans = new LinkedList<Span>();
    Paragraph result = new Paragraph();
    Span s = lineSpans.removeFirst();
    paragraphSpans.add(s);// w w  w . ja va2  s  .  c  o  m

    switch (paragraphType) {
    case PARAGRAPH:
        result.setType(Paragraph.type.NORMAL);
        while (!lineSpans.isEmpty()) {
            if (paragraphType != getLineType(sm, lineSpans.getFirst())) {
                break;
            }
            paragraphSpans.add(lineSpans.removeFirst());
        }
        break;

    case PARAGRAPH_BOXED:
        result.setType(Paragraph.type.BOXED);
        while (!lineSpans.isEmpty()) {
            lineType lt = getLineType(sm, lineSpans.getFirst());
            if (paragraphType != lt && lineType.EMPTYLINE != lt) {
                break;
            }
            paragraphSpans.add(lineSpans.removeFirst());
        }
        break;

    case PARAGRAPH_INDENTED:
        result.setType(Paragraph.type.INDENTED);
        s.trim(sm.setCharAt(s.getStart(), ' '));
        break;

    default:
        return null;
    }

    parseContentElement(sm, cepp, paragraphSpans, result);

    return result;
}

From source file:net.timewalker.ffmq4.storage.data.impl.journal.JournalRecovery.java

private int recoverFromJournalFile(File journalFile) throws JournalException {
    log.debug("[" + baseName + "] Processing " + journalFile.getAbsolutePath());

    DataInputStream in;//from w ww.j a va 2s  .com
    try {
        // Create a buffered data input stream from file
        in = new DataInputStream(new BufferedInputStream(new FileInputStream(journalFile)));
    } catch (IOException e) {
        throw new JournalException("Cannot open journal file : " + journalFile.getAbsolutePath(), e);
    }

    int replayedOperations = 0;
    int replayedTransactions = 0;
    long currentTransactionId = -1;
    int newBlockCount = -1;
    LinkedList<AbstractJournalOperation> transactionQueue = new LinkedList<>();
    try {
        AbstractJournalOperation op;
        while ((op = readJournalOperation(in)) != null) {
            // Check transaction id
            if (currentTransactionId == -1)
                currentTransactionId = op.getTransactionId();
            else if (currentTransactionId != op.getTransactionId())
                throw new IllegalStateException("Transaction id inconsistency : " + currentTransactionId
                        + " -> " + op.getTransactionId());

            if (op instanceof CommitOperation) {
                // Check transaction size
                int opCount = ((CommitOperation) op).getOperationsCount();
                if (transactionQueue.size() != opCount) {
                    throw new IllegalStateException("Transaction size mismatch (expected " + opCount + ", got "
                            + transactionQueue.size() + ")");
                } else {
                    // Everything looks fine, proceed ...
                    log.trace("[" + baseName + "] Replaying transaction #" + currentTransactionId + " ("
                            + transactionQueue.size() + " operation(s))");
                    replayedOperations += transactionQueue.size();
                    replayedTransactions++;
                    newBlockCount = applyOperations(transactionQueue);
                    currentTransactionId = -1;
                }
            } else
                transactionQueue.addLast(op);
        }

        if (transactionQueue.size() > 0) {
            op = transactionQueue.removeFirst();
            log.warn("[" + baseName + "] Dropping incomplete transaction : #" + op.getTransactionId());
        }

        syncStore();

        log.warn("[" + baseName + "] Recovery complete. (Replayed " + replayedTransactions
                + " transaction(s) and " + replayedOperations + " operation(s))");
    } finally {
        try {
            in.close();
        } catch (IOException e) {
            throw new JournalException("Cannot close journal file : " + journalFile.getAbsolutePath(), e);
        }
    }

    return newBlockCount;
}