idx
int64
0
41.2k
question
stringlengths
74
4.21k
target
stringlengths
5
888
41,200
public static Dictionary read ( URL dictURL ) throws IOException { final URL expectedMetadataURL ; try { String external = dictURL . toExternalForm ( ) ; expectedMetadataURL = new URL ( DictionaryMetadata . getExpectedMetadataFileName ( external ) ) ; } catch ( MalformedURLException e ) { throw new IOException ( "Couldn't construct relative feature map URL for: " + dictURL , e ) ; } try ( InputStream fsaStream = dictURL . openStream ( ) ; InputStream metadataStream = expectedMetadataURL . openStream ( ) ) { return read ( fsaStream , metadataStream ) ; } }
Attempts to load a dictionary using the URL to the FSA file and the expected metadata extension .
41,201
public static Dictionary read ( InputStream fsaStream , InputStream metadataStream ) throws IOException { return new Dictionary ( FSA . read ( fsaStream ) , DictionaryMetadata . read ( metadataStream ) ) ; }
Attempts to load a dictionary from opened streams of FSA dictionary data and associated metadata . Input streams are not closed automatically .
41,202
public void set ( final int i , final int j , final int val ) { p [ ( j - i + editDistance + 1 ) * rowLength + j ] = val ; }
Set an item in hMatrix . No checking for i &amp ; j is done . They must be correct .
41,203
private final ByteBuffer advance ( ) { if ( position == 0 ) { return null ; } while ( position > 0 ) { final int lastIndex = position - 1 ; final int arc = arcs [ lastIndex ] ; if ( arc == 0 ) { position -- ; continue ; } arcs [ lastIndex ] = fsa . getNextArc ( arc ) ; final int bufferLength = this . buffer . length ; if ( lastIndex >= bufferLength ) { this . buffer = Arrays . copyOf ( buffer , bufferLength + EXPECTED_MAX_STATES ) ; this . bufferWrapper = ByteBuffer . wrap ( buffer ) ; } buffer [ lastIndex ] = fsa . getArcLabel ( arc ) ; if ( ! fsa . isArcTerminal ( arc ) ) { pushNode ( fsa . getEndNode ( arc ) ) ; } if ( fsa . isArcFinal ( arc ) ) { bufferWrapper . clear ( ) ; bufferWrapper . limit ( lastIndex + 1 ) ; return bufferWrapper ; } } return null ; }
Advances to the next available final state .
41,204
private void pushNode ( int node ) { if ( position == arcs . length ) { arcs = Arrays . copyOf ( arcs , arcs . length + EXPECTED_MAX_STATES ) ; } arcs [ position ++ ] = fsa . getFirstArc ( node ) ; }
Descends to a given node adds its arcs to the stack to be traversed .
41,205
public static FSAHeader read ( InputStream in ) throws IOException { if ( in . read ( ) != ( ( FSA_MAGIC >>> 24 ) ) || in . read ( ) != ( ( FSA_MAGIC >>> 16 ) & 0xff ) || in . read ( ) != ( ( FSA_MAGIC >>> 8 ) & 0xff ) || in . read ( ) != ( ( FSA_MAGIC ) & 0xff ) ) { throw new IOException ( "Invalid file header, probably not an FSA." ) ; } int version = in . read ( ) ; if ( version == - 1 ) { throw new IOException ( "Truncated file, no version number." ) ; } return new FSAHeader ( ( byte ) version ) ; }
Read FSA header and version from a stream consuming read bytes .
41,206
public static void write ( OutputStream os , byte version ) throws IOException { os . write ( FSA_MAGIC >> 24 ) ; os . write ( FSA_MAGIC >> 16 ) ; os . write ( FSA_MAGIC >> 8 ) ; os . write ( FSA_MAGIC ) ; os . write ( version ) ; }
Writes FSA magic bytes and version information .
41,207
static final int decodeFromBytes ( final byte [ ] arcs , final int start , final int n ) { int r = 0 ; for ( int i = n ; -- i >= 0 ; ) { r = r << 8 | ( arcs [ start + i ] & 0xff ) ; } return r ; }
Returns an n - byte integer encoded in byte - packed representation .
41,208
protected static void main ( String [ ] args , CliTool ... commands ) { if ( commands . length == 1 ) { main ( args , commands [ 0 ] ) ; } else { JCommander jc = new JCommander ( ) ; for ( CliTool command : commands ) { jc . addCommand ( command ) ; } jc . addConverterFactory ( new CustomParameterConverters ( ) ) ; jc . setProgramName ( "" ) ; ExitStatus exitStatus = ExitStatus . SUCCESS ; try { jc . parse ( args ) ; final String commandName = jc . getParsedCommand ( ) ; if ( commandName == null ) { helpDisplayCommandOptions ( System . err , jc ) ; } else { List < Object > objects = jc . getCommands ( ) . get ( commandName ) . getObjects ( ) ; if ( objects . size ( ) != 1 ) { throw new RuntimeException ( ) ; } CliTool command = CliTool . class . cast ( objects . get ( 0 ) ) ; exitStatus = command . call ( ) ; if ( command . callSystemExit ) { System . exit ( exitStatus . code ) ; } } } catch ( ExitStatusException e ) { System . err . println ( e . getMessage ( ) ) ; if ( e . getCause ( ) != null ) { e . getCause ( ) . printStackTrace ( System . err ) ; } exitStatus = e . exitStatus ; } catch ( MissingCommandException e ) { System . err . println ( "Invalid argument: " + e ) ; System . err . println ( ) ; helpDisplayCommandOptions ( System . err , jc ) ; exitStatus = ExitStatus . ERROR_INVALID_ARGUMENTS ; } catch ( ParameterException e ) { System . err . println ( "Invalid argument: " + e . getMessage ( ) ) ; System . err . println ( ) ; if ( jc . getParsedCommand ( ) == null ) { helpDisplayCommandOptions ( System . err , jc ) ; } else { helpDisplayCommandOptions ( System . err , jc . getParsedCommand ( ) , jc ) ; } exitStatus = ExitStatus . ERROR_INVALID_ARGUMENTS ; } catch ( Throwable t ) { System . err . println ( "An unhandled exception occurred. Stack trace below." ) ; t . printStackTrace ( System . err ) ; exitStatus = ExitStatus . ERROR_OTHER ; } } }
Parse and execute one of the commands .
41,209
protected static void main ( String [ ] args , CliTool command ) { JCommander jc = new JCommander ( command ) ; jc . addConverterFactory ( new CustomParameterConverters ( ) ) ; jc . setProgramName ( command . getClass ( ) . getAnnotation ( Parameters . class ) . commandNames ( ) [ 0 ] ) ; ExitStatus exitStatus = ExitStatus . SUCCESS ; try { jc . parse ( args ) ; if ( command . help ) { helpDisplayCommandOptions ( System . err , jc ) ; } else { exitStatus = command . call ( ) ; } } catch ( ExitStatusException e ) { System . err . println ( e . getMessage ( ) ) ; if ( e . getCause ( ) != null ) { e . getCause ( ) . printStackTrace ( System . err ) ; } exitStatus = e . exitStatus ; } catch ( MissingCommandException e ) { System . err . println ( "Invalid argument: " + e ) ; System . err . println ( ) ; helpDisplayCommandOptions ( System . err , jc ) ; exitStatus = ExitStatus . ERROR_INVALID_ARGUMENTS ; } catch ( ParameterException e ) { System . err . println ( "Invalid argument: " + e . getMessage ( ) ) ; System . err . println ( ) ; if ( jc . getParsedCommand ( ) == null ) { helpDisplayCommandOptions ( System . err , jc ) ; } else { helpDisplayCommandOptions ( System . err , jc . getParsedCommand ( ) , jc ) ; } exitStatus = ExitStatus . ERROR_INVALID_ARGUMENTS ; } catch ( Throwable t ) { System . err . println ( "An unhandled exception occurred. Stack trace below." ) ; t . printStackTrace ( System . err ) ; exitStatus = ExitStatus . ERROR_OTHER ; } if ( command . callSystemExit ) { System . exit ( exitStatus . code ) ; } }
Parse and execute a single command .
41,210
static String byteAsChar ( byte v ) { int chr = v & 0xff ; return String . format ( Locale . ROOT , "%s (0x%02x)" , ( Character . isWhitespace ( chr ) || chr > 127 ) ? "[non-printable]" : Character . toString ( ( char ) chr ) , v & 0xFF ) ; }
Convert a byte to an informative string .
41,211
public static FSA read ( InputStream stream ) throws IOException { final FSAHeader header = FSAHeader . read ( stream ) ; switch ( header . version ) { case FSA5 . VERSION : return new FSA5 ( stream ) ; case CFSA . VERSION : return new CFSA ( stream ) ; case CFSA2 . VERSION : return new CFSA2 ( stream ) ; default : throw new IOException ( String . format ( Locale . ROOT , "Unsupported automaton version: 0x%02x" , header . version & 0xFF ) ) ; } }
A factory for reading automata in any of the supported versions .
41,212
public static < T extends FSA > T read ( InputStream stream , Class < ? extends T > clazz ) throws IOException { FSA fsa = read ( stream ) ; if ( ! clazz . isInstance ( fsa ) ) { throw new IOException ( String . format ( Locale . ROOT , "Expected FSA type %s, but read an incompatible type %s." , clazz . getName ( ) , fsa . getClass ( ) . getName ( ) ) ) ; } return clazz . cast ( fsa ) ; }
A factory for reading a specific FSA subclass including proper casting .
41,213
private static int forAllLines ( InputStream is , byte separator , LineConsumer lineConsumer ) throws IOException { int lines = 0 ; byte [ ] buffer = new byte [ 0 ] ; int b , pos = 0 ; while ( ( b = is . read ( ) ) != - 1 ) { if ( b == separator ) { buffer = lineConsumer . process ( buffer , pos ) ; pos = 0 ; lines ++ ; } else { if ( pos >= buffer . length ) { buffer = java . util . Arrays . copyOf ( buffer , buffer . length + Math . max ( 10 , buffer . length / 10 ) ) ; } buffer [ pos ++ ] = ( byte ) b ; } } if ( pos > 0 ) { lineConsumer . process ( buffer , pos ) ; lines ++ ; } return lines ; }
Read all byte - separated sequences .
41,214
public static NtlmPasswordAuthentication authenticate ( HttpServletRequest req , HttpServletResponse resp , byte [ ] challenge ) throws IOException , ServletException { String msg = req . getHeader ( "Authorization" ) ; if ( msg != null && msg . startsWith ( "NTLM " ) ) { byte [ ] src = Base64 . decode ( msg . substring ( 5 ) ) ; if ( src [ 8 ] == 1 ) { Type1Message type1 = new Type1Message ( src ) ; Type2Message type2 = new Type2Message ( type1 , challenge , null ) ; msg = Base64 . encode ( type2 . toByteArray ( ) ) ; resp . setHeader ( "WWW-Authenticate" , "NTLM " + msg ) ; } else if ( src [ 8 ] == 3 ) { Type3Message type3 = new Type3Message ( src ) ; byte [ ] lmResponse = type3 . getLMResponse ( ) ; if ( lmResponse == null ) lmResponse = new byte [ 0 ] ; byte [ ] ntResponse = type3 . getNTResponse ( ) ; if ( ntResponse == null ) ntResponse = new byte [ 0 ] ; return new NtlmPasswordAuthentication ( type3 . getDomain ( ) , type3 . getUser ( ) , challenge , lmResponse , ntResponse ) ; } } else { resp . setHeader ( "WWW-Authenticate" , "NTLM" ) ; } resp . setStatus ( HttpServletResponse . SC_UNAUTHORIZED ) ; resp . setContentLength ( 0 ) ; resp . flushBuffer ( ) ; return null ; }
Performs NTLM authentication for the servlet request .
41,215
boolean verify ( byte [ ] data , int offset , ServerMessageBlock response ) { update ( macSigningKey , 0 , macSigningKey . length ) ; int index = offset ; update ( data , index , ServerMessageBlock . SIGNATURE_OFFSET ) ; index += ServerMessageBlock . SIGNATURE_OFFSET ; byte [ ] sequence = new byte [ 8 ] ; ServerMessageBlock . writeInt4 ( response . signSeq , sequence , 0 ) ; update ( sequence , 0 , sequence . length ) ; index += 8 ; if ( response . command == ServerMessageBlock . SMB_COM_READ_ANDX ) { SmbComReadAndXResponse raxr = ( SmbComReadAndXResponse ) response ; int length = response . length - raxr . dataLength ; update ( data , index , length - ServerMessageBlock . SIGNATURE_OFFSET - 8 ) ; update ( raxr . b , raxr . off , raxr . dataLength ) ; } else { update ( data , index , response . length - ServerMessageBlock . SIGNATURE_OFFSET - 8 ) ; } byte [ ] signature = digest ( ) ; for ( int i = 0 ; i < 8 ; i ++ ) { if ( signature [ i ] != data [ offset + ServerMessageBlock . SIGNATURE_OFFSET + i ] ) { if ( log . level >= 2 ) { log . println ( "signature verification failure" ) ; Hexdump . hexdump ( log , signature , 0 , 8 ) ; Hexdump . hexdump ( log , data , offset + ServerMessageBlock . SIGNATURE_OFFSET , 8 ) ; } return response . verifyFailed = true ; } } return response . verifyFailed = false ; }
Performs MAC signature verification . This calculates the signature of the SMB and compares it to the signature field on the SMB itself .
41,216
public static String encode ( byte [ ] bytes ) { int length = bytes . length ; if ( length == 0 ) return "" ; StringBuffer buffer = new StringBuffer ( ( int ) Math . ceil ( ( double ) length / 3d ) * 4 ) ; int remainder = length % 3 ; length -= remainder ; int block ; int i = 0 ; while ( i < length ) { block = ( ( bytes [ i ++ ] & 0xff ) << 16 ) | ( ( bytes [ i ++ ] & 0xff ) << 8 ) | ( bytes [ i ++ ] & 0xff ) ; buffer . append ( ALPHABET . charAt ( block >>> 18 ) ) ; buffer . append ( ALPHABET . charAt ( ( block >>> 12 ) & 0x3f ) ) ; buffer . append ( ALPHABET . charAt ( ( block >>> 6 ) & 0x3f ) ) ; buffer . append ( ALPHABET . charAt ( block & 0x3f ) ) ; } if ( remainder == 0 ) return buffer . toString ( ) ; if ( remainder == 1 ) { block = ( bytes [ i ] & 0xff ) << 4 ; buffer . append ( ALPHABET . charAt ( block >>> 6 ) ) ; buffer . append ( ALPHABET . charAt ( block & 0x3f ) ) ; buffer . append ( "==" ) ; return buffer . toString ( ) ; } block = ( ( ( bytes [ i ++ ] & 0xff ) << 8 ) | ( ( bytes [ i ] ) & 0xff ) ) << 2 ; buffer . append ( ALPHABET . charAt ( block >>> 12 ) ) ; buffer . append ( ALPHABET . charAt ( ( block >>> 6 ) & 0x3f ) ) ; buffer . append ( ALPHABET . charAt ( block & 0x3f ) ) ; buffer . append ( "=" ) ; return buffer . toString ( ) ; }
Base - 64 encodes the supplied block of data . Line wrapping is not applied on output .
41,217
public static byte [ ] decode ( String string ) { int length = string . length ( ) ; if ( length == 0 ) return new byte [ 0 ] ; int pad = ( string . charAt ( length - 2 ) == '=' ) ? 2 : ( string . charAt ( length - 1 ) == '=' ) ? 1 : 0 ; int size = length * 3 / 4 - pad ; byte [ ] buffer = new byte [ size ] ; int block ; int i = 0 ; int index = 0 ; while ( i < length ) { block = ( ALPHABET . indexOf ( string . charAt ( i ++ ) ) & 0xff ) << 18 | ( ALPHABET . indexOf ( string . charAt ( i ++ ) ) & 0xff ) << 12 | ( ALPHABET . indexOf ( string . charAt ( i ++ ) ) & 0xff ) << 6 | ( ALPHABET . indexOf ( string . charAt ( i ++ ) ) & 0xff ) ; buffer [ index ++ ] = ( byte ) ( block >>> 16 ) ; if ( index < size ) buffer [ index ++ ] = ( byte ) ( ( block >>> 8 ) & 0xff ) ; if ( index < size ) buffer [ index ++ ] = ( byte ) ( block & 0xff ) ; } return buffer ; }
Decodes the supplied Base - 64 encoded string .
41,218
public Post reblog ( String blogName , Map < String , ? > options ) { return client . postReblog ( blogName , id , reblog_key , options ) ; }
Reblog this post
41,219
public void setDate ( Date date ) { DateFormat df = new SimpleDateFormat ( "yyyy/MM/dd HH:mm:ss" ) ; df . setTimeZone ( TimeZone . getTimeZone ( "GMT" ) ) ; setDate ( df . format ( date ) ) ; }
Set the date as a date
41,220
public void addTag ( String tag ) { if ( this . tags == null ) { tags = new ArrayList < String > ( ) ; } this . tags . add ( tag ) ; }
Add a tag
41,221
public void save ( ) throws IOException { if ( id == null ) { this . id = client . postCreate ( blog_name , detail ( ) ) ; } else { client . postEdit ( blog_name , id , detail ( ) ) ; } }
Save this post
41,222
protected Map < String , Object > detail ( ) { final Map < String , Object > map = new HashMap < String , Object > ( ) ; map . put ( "state" , state ) ; map . put ( "tags" , getTagString ( ) ) ; map . put ( "format" , format ) ; map . put ( "slug" , slug ) ; map . put ( "date" , date ) ; map . put ( "type" , getType ( ) . getValue ( ) ) ; return map ; }
Detail for this post
41,223
public void setPhoto ( Photo photo ) { PhotoType type = photo . getType ( ) ; if ( postType != null && ! postType . equals ( type ) ) { throw new IllegalArgumentException ( "Photos must all be the same type (source or data)" ) ; } else if ( postType == PhotoType . SOURCE && pendingPhotos . size ( ) > 0 ) { throw new IllegalArgumentException ( "Only one source URL can be provided" ) ; } pendingPhotos = new ArrayList < Photo > ( ) ; pendingPhotos . add ( photo ) ; this . postType = type ; }
Set the photo for this post
41,224
public List < Blog > getBlogs ( ) { if ( blogs != null ) { for ( Blog blog : blogs ) { blog . setClient ( client ) ; } } return this . blogs ; }
Get the blog List for this user
41,225
private void extractErrors ( JsonObject object ) { JsonObject response ; try { response = object . getAsJsonObject ( "response" ) ; } catch ( ClassCastException ex ) { return ; } if ( response == null ) { return ; } JsonArray e = response . getAsJsonArray ( "errors" ) ; if ( e == null ) { return ; } errors = new ArrayList < String > ( e . size ( ) ) ; for ( int i = 0 ; i < e . size ( ) ; i ++ ) { errors . add ( e . get ( i ) . getAsString ( ) ) ; } }
Pull the errors out of the response if present
41,226
private void extractMessage ( JsonObject object ) { JsonObject meta = object . getAsJsonObject ( "meta" ) ; if ( meta != null ) { JsonPrimitive msg = meta . getAsJsonPrimitive ( "msg" ) ; if ( msg != null ) { this . message = msg . getAsString ( ) ; return ; } } JsonPrimitive error = object . getAsJsonPrimitive ( "error" ) ; if ( error != null ) { this . message = error . getAsString ( ) ; return ; } this . message = "Unknown Error" ; }
Pull the message out of the response
41,227
public void xauth ( final String email , final String password ) { setToken ( this . requestBuilder . postXAuth ( email , password ) ) ; }
Performs an XAuth authentication .
41,228
public List < Post > userDashboard ( Map < String , ? > options ) { return requestBuilder . get ( "/user/dashboard" , options ) . getPosts ( ) ; }
Get the user dashboard for the authenticated User
41,229
public List < Blog > userFollowing ( Map < String , ? > options ) { return requestBuilder . get ( "/user/following" , options ) . getBlogs ( ) ; }
Get the blogs the given user is following